Here's one thing I haven't found much documentatio...
# graphql-nexus
s
Here's one thing I haven't found much documentation about and maybe someone can help shed some light on it: with nexus-plugin-prisma I have a total of four engines generating typescript-definitions: prisma for the db client, nexus for the graphql schema, nexus-plugin-prisma for some helpers and graphql-codegen for my frontend code (same next.js codebase). Essentially the defined types are highly similar with some nuances. I have a hard time selecting the types from the right packages and oftentimes I fight with small discrepancies in type definitions from e.g. a FieldResolver around nullability. Any hints which could make my life a bit easier?
r
Hey @Stefan N ๐Ÿ‘‹ GraphQL Codegen would be best for the frontend where youโ€™re making your GraphQL queries presumably via Apollo Client. This will automatically map to the
schema.graphql
created by Nexus, and also will autogenerate queries for you based on the schema and queries that you provide. The types that Prisma generate can be used in the backend to map to your database schema. This is helpful sometimes in your resolver or in your external business logic. Finally the types that
nexus-plugin-prisma
generates is used specifically for the resolvers and to help specify what type must be returned and used including arguments. So basically on the frontend, GraphQL Codegen is your friend and will help you write all the necessary queries and the rest can be used in the backend as mentioned above ๐Ÿ™‚
s
Thanks Ryan. I figured out the frontend part (after initially using
@prisma/client
there as well, which of course is a pitfall better not to fall into. When working with db queries and results it's also clear. I guess where I'm struggling the most is with the resolvers, where I'm not quite certain how to best leverage both the
nexus.ts
as well as the
nexus-plugin-prisma
types. Some additional docs around FieldResolver, NexusGenRootTypes and NexusGenFieldTypes, etc. could help.
r
The types that you define in the
args
and the return type of the resolver. For e.g.
Copy code
t.nullable.field('publish', {
  type: 'Post',
  args: { id: intArg() },
  async resolve(_parent, { id }, ctx) {
    const newPost = await ctx.prisma.post.update({
      where: { id },
      data: { published: true },
    })

    ctx.pubsub.publish('latestPost', newPost)
    return newPost
  },
})
In this resolver, you are using the TypeScript type
Post
in the field where
type: 'Post'
. All the
inputObjectType
and
objectType
that you create are generated as TS types and are available to use directly in the resolver.
Some additional docs around FieldResolver, NexusGenRootTypes and NexusGenFieldTypes, etc. could help.
Yes this needs to be explained more in the docs as that is not mentioned clearly there ๐Ÿ™‚