Prisma1: resolve the `AuthPayload` type, why?

Created on 26 Jan 2018  路  2Comments  路  Source: prisma/prisma1

I don't get https://github.com/graphcool/prisma/blob/03c8957c2dd8239c0aca1119ff06a3c7204bc889/examples/auth/src/auth.js#L5

Why AuthPayload have to be resolved? It looks like it works without it.

Most helpful comment

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

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dohomi picture dohomi  路  3Comments

schickling picture schickling  路  3Comments

jannone picture jannone  路  3Comments

sedubois picture sedubois  路  3Comments

schickling picture schickling  路  3Comments