Why AuthPayload have to be resolved? It looks like it works without it.
same question
Thanks for opening this issue @steida! There was in fact a bug which I just fixed, because the AuthPayload resolver was never included in the resolvers that was passed to the GraphQLServer.
To answer your question though why it's necessary. The short version is that in this example, it actually doesn't matter because the use case is so simple and there are no relations on the User type. However, if you have a relation on the User type and would query data from that relation via the AuthPayload type, the related data couldn't be resolved.
I created an example that extends the auth example here. You can run the example per instructions in the README. To observe the behaviour that I described above, you first need to signup with a mutation, e.g.:
mutation {
signup(email: "[email protected]", password: "asd") {
token
user {
id
}
}
}
Then create a Post for that User (against the Prisma API):
mutation {
createPost(data: {
title: "ASD"
author: {
connect: {
id: "__USER_ID__"
}
}
}) {
id
}
}
Now, you can observe the behaviour by sending a login mutation in which you're querying posts of the User via the AuthPayload type:
mutation {
login(email: "[email protected]", password: "asd") {
token
user {
id
email
posts {
id
title
}
}
}
}
If you add the AuthPayload resolver to the resolvers object again (here), then the login mutation successfully returns the Post. Otherwise it returns the error message: "Cannot return null for non-nullable field User.posts."
To learn more about _why_ that is be sure to check out this blog post: GraphQL Server Basics: Demystifying the info Argument in GraphQL Resolvers
Most helpful comment
Thanks for opening this issue @steida! There was in fact a bug which I just fixed, because the
AuthPayloadresolver was never included in theresolversthat was passed to theGraphQLServer.To answer your question though why it's necessary. The short version is that in this example, it actually doesn't matter because the use case is so simple and there are no relations on the
Usertype. However, if you have a relation on theUsertype and would query data from that relation via theAuthPayloadtype, the related data couldn't be resolved.I created an example that extends the
authexample here. You can run the example per instructions in the README. To observe the behaviour that I described above, you first need to signup with a mutation, e.g.:Then create a
Postfor thatUser(against the Prisma API):Now, you can observe the behaviour by sending a
loginmutation in which you're queryingpostsof theUservia theAuthPayloadtype:If you add the
AuthPayloadresolver to theresolversobject again (here), then theloginmutation successfully returns thePost. Otherwise it returns the error message:"Cannot return null for non-nullable field User.posts."To learn more about _why_ that is be sure to check out this blog post: GraphQL Server Basics: Demystifying the
infoArgument in GraphQL Resolvers