Nexus-plugin-prisma: Cannot return null for non-nullable field Joke.id

Created on 7 Oct 2019  Â·  9Comments  Â·  Source: graphql-nexus/nexus-plugin-prisma

As asked in the Prisma2 Slack, I'm creating an issue here. I'm using Prisma2.

I have a simple schema.prisma file ⸺

model Joke {
  id      String @default(cuid()) @id
  author  Author
  content String @unique
}

model Author {
  id    String @default(cuid()) @id
  name  String
  jokes Joke[]
}

And this is my Query ⸺

t.list.field('filterJokesByAuthor', {
      type: 'Joke',
      args: {
        name: stringArg(),
      },
      resolve: async (_, { name }, ctx) => {
        const jokesByAuthor = await ctx.photon.authors.findMany({
          where: {
            name,
          },
          select: {
            jokes: true,
          },
        })
        // const jokesByAuthor = await ctx.photon.jokes.findMany({
        //   where: {
        //     author: {
        //       name,
        //     },
        //   },
        // })
        console.log(JSON.stringify(jokesByAuthor))
        return jokesByAuthor
      },
    })

The commented one works while the uncommented doesn't & gives me error "Cannot return null for non-nullable field Joke.id.". Why? I want to access jokes by a particular author by calling ctx.photon.authors. How can I achieve that?

Also, getJokeByAuthor's return type is [Joke]? Where does it get that? Shouldn't it be [Author] now that I'm returning ctx.photon.authors?

Here's a minimal repro → https://github.com/deadcoder0904/jokes-server-error

Most helpful comment

Yes, meaning you're still using nexus-prisma as a prisma generator, but it's no longer one. Check out how it's done in the examples/blog folder

All 9 comments

The latest commit makes it work however it logs 2 different things ⸺

From http://localhost:4000/

{
  "data": {
    "filterJokesByAuthor": [
      {
        "id": "ck1gjx5k505211lv59mrd1olj",
        "name": "Addy Osmani",
        "jokes": [
          {
            "id": "ck1gjx5k505201lv52djpwx87",
            "content": "First do it, then do it right, then do it better."
          }
        ]
      },
      {
        "id": "ck1gjx5ke05251lv5wsjrg9jo",
        "name": "Addy Osmani",
        "jokes": [
          {
            "id": "ck1gjx5ke05241lv50yo17wby",
            "content": "Be humble, communicate clearly, and respect others. It costs nothing to be kind, but the impact is priceless."
          }
        ]
      },
      {
        "id": "ck1gjx5yz07451lv5nbwwr85o",
        "name": "Addy Osmani",
        "jokes": [
          {
            "id": "ck1gjx5yz07441lv5fmi883zv",
            "content": "Really care about the tools you use because they are what make you your best."
          }
        ]
      }
    ]
  }
}

From console.log

[
  {
    "id": "ck1gjx5k505211lv59mrd1olj",
    "name": "Addy Osmani"
  },
  {
    "id": "ck1gjx5ke05251lv5wsjrg9jo",
    "name": "Addy Osmani"
  },
  {
    "id": "ck1gjx5yz07451lv5nbwwr85o",
    "name": "Addy Osmani"
  }
]

From http://localhost:4000/ u get what u ask for but from console.log I only get top-level fields (primitive types) id & name as jokes is of type Joke as seen here -

model Author {
  id    String @default(cuid()) @id
  name  String
  jokes Joke[]
}

Not sure if this is a bug so I'll leave it open if jokes was supposed to be returned from console.log.

Also, Stephen was talking about this use-case here → https://github.com/prisma-labs/nexus-prisma/issues/448

Hey @deadcoder0904 you might want to consider upgrading to then newest nexus-prisma version.

I was able to log as expected from the example app

image

I want to access jokes by a particular author by calling ctx.photon.authors. How can I achieve that?

Seems you figured out your goal but I suppose you could also have done something like:

ctx.photon.jokes.findMany({ where: { author: { id: args.authorID } } })

Hey @deadcoder0904 you might want to consider upgrading to then newest nexus-prisma version.

I updated nexus from 0.12.0-beta.9 to 0.12.0-beta.12 but that doesn't change anything.

ctx.photon.jokes.findMany({ where: { author: { id: args.authorID } } })

I already do:

const jokesByAuthor = await ctx.photon.jokes.findMany({
    where: {
        author: {
            name,
        },
    },
})

Anyways it works as it should. One question tho. It logs only id & name & not jokes array like:

[
  {
    "id": "ck1gjx5k505211lv59mrd1olj",
    "name": "Addy Osmani"
  },
  {
    "id": "ck1gjx5ke05251lv5wsjrg9jo",
    "name": "Addy Osmani"
  },
  {
    "id": "ck1gjx5yz07451lv5nbwwr85o",
    "name": "Addy Osmani"
  }
]

Is this normal? Or should it return jokes too? The query that returns above log is:

const jokesByAuthor = await ctx.photon.authors.findMany({
    where: {
        name,
    },
})

By default, photon only queries the scalars fields of a model. jokes is a relation and is therefore not queried unless you specify it.

const jokesByAuthor = await ctx.photon.authors.findMany({
    where: {
        name,
    },
    include: { jokes: true }
})

When defining include: { jokes: true }, you'll see the type of jokesByAuthor being updated accordingly

Thanks, @Weakky you confirmed my suspicion. I do get the complete data now ⸺

{
  "data": {
    "filterJokesByAuthor": [
      {
        "id": "ck1gjx5k505211lv59mrd1olj",
        "name": "Addy Osmani",
        "jokes": [
          {
            "id": "ck1gjx5k505201lv52djpwx87",
            "content": "First do it, then do it right, then do it better."
          }
        ]
      },
      {
        "id": "ck1gjx5ke05251lv5wsjrg9jo",
        "name": "Addy Osmani",
        "jokes": [
          {
            "id": "ck1gjx5ke05241lv50yo17wby",
            "content": "Be humble, communicate clearly, and respect others. It costs nothing to be kind, but the impact is priceless."
          }
        ]
      },
      {
        "id": "ck1gjx5yz07451lv5nbwwr85o",
        "name": "Addy Osmani",
        "jokes": [
          {
            "id": "ck1gjx5yz07441lv5fmi883zv",
            "content": "Really care about the tools you use because they are what make you your best."
          }
        ]
      }
    ]
  }
}

I updated nexus from 0.12.0-beta.9 to 0.12.0-beta.12 but that doesn't change anything.

I'm referring to nexus-prisma, not nexus (but yes update both). You were still using the generator variant from pre-releases.

Yes, meaning you're still using nexus-prisma as a prisma generator, but it's no longer one. Check out how it's done in the examples/blog folder

I wrote a quick comment on how to update from the generator variant to the latest version of nexus-prisma here :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

macrozone picture macrozone  Â·  3Comments

mateja176 picture mateja176  Â·  5Comments

malekjaroslav picture malekjaroslav  Â·  5Comments

RAW4RMCS picture RAW4RMCS  Â·  3Comments

jasonkuhrt picture jasonkuhrt  Â·  4Comments