Graphiti icon indicating copy to clipboard operation
Graphiti copied to clipboard

Weird error when loading a dynamic property of a type

Open Dracks opened this issue 1 year ago • 5 comments

Hello,

I'm trying to migrate my project MrScrooge from nestjs with graphql into swift with vapor, graphqlkit (which uses graphiti), And seems I've got some issue, not sure if it's my error or is an error on graphiti.

When I add a dynamic field into a type, to load only when I've got the request, I do it as following:

Type(MyProfile.self){
   Field("group", MyProfile.groups
}
MyProfile{
   func groups(request: Request, Parent: MyProfile){}
}

You can see this here: https://github.com/Dracks/mr-scrooge/commit/27862abb0d6008fb3e6b8e545565bf4afc85a2f5#diff-86900d9223aa878afd6fd2f942cdd076fa63c7a147fae5e22db15769e0d2ec95R63 and here:

https://github.com/Dracks/mr-scrooge/commit/27862abb0d6008fb3e6b8e545565bf4afc85a2f5#diff-86900d9223aa878afd6fd2f942cdd076fa63c7a147fae5e22db15769e0d2ec95R136-R141

it ends crashing in some weird error, the error is the following:

{
  "errors": [
    {
      "message": "keyNotFound(CodingKeys(stringValue: \"id\", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: \"No value associated with key CodingKeys(stringValue: \\\"id\\\", intValue: nil) (\\\"id\\\").\", underlyingError: nil))",
      "locations": [
        {
          "line": 6,
          "column": 7
        }
      ],
      "path": [
        "me",
        "groups"
      ]
    }
  ]
}```

I look with xcode, it seem to be some error parsing the session. Any Idea of what it can be? It happens in multiple places I've got the dynamic field. 

Do you need some small example? I can try to create it. 

Thanks, 
Dracks

Dracks avatar Aug 30 '24 17:08 Dracks

Hi, this is a pretty typical Swift error when you try to decode a type that doesn't have a required field. I'd double check that the data coming back from your database has the id field populated so it matches your codable model definitions.

I might be able to give use some more direct guidance if you provide the GraphQL query that failed

NeedleInAJayStack avatar Aug 31 '24 19:08 NeedleInAJayStack

Hey, thanks for your answord.

The query was the following one:

query {
  me{
    __typename
    ... on MyProfile {
      username
      groups{
        name
        id
      }
    }
  }
}

But the debugger with breakpoints never reached the dynamic load function. And when I don't require the groups, everything work fine

Thanks, Jaume

Dracks avatar Sep 01 '24 07:09 Dracks

If everything worked okay when you didn't include groups, I'd try dropping a breakpoint in the groups resolver before you load the object from the database or whatever data source you're using. Since this is a decoding error, it is likely thrown when you load the group object from the underlying data source. That is, it is likely not an issue with the GraphQL setup, instead it is likely an underlying data issue, where a group that is being loaded doesn't have an id field.

NeedleInAJayStack avatar Sep 05 '24 03:09 NeedleInAJayStack

I'm pretty sure, I already tried and never reach the group resolver. But I will try it again.

Dracks avatar Sep 05 '24 05:09 Dracks

Hey, I tested, it never reach the resolver of the subElement, I generated a small repo that it's a minimum reproducible error (I think)

You can see it here: https://github.com/Dracks/GraphqlVaporSample

Simply run the tests, there is 2 tests, one includes the call to the groups, the other doesn't, the first one fails the other works fine.

Dracks avatar Sep 05 '24 16:09 Dracks