37
loading...
This website collects cookies to deliver better user experience
This article was published on 2021-12-19 by Gilad Tidhar @ The Guild Blog
Auto-completion in code editors instead of needing to look up documentation
const resolver = {
Query: {
feed: async (
parent: unknown,
args: {
filter?: string;
skip?: number;
take?: number;
},
context: GraphQLContext
) => {...}
}
}
import { Resolvers } from './generated/graphql'
const resolvers: Resolvers = {
Query: {
feed: async (parent, args, context) => {
// ...
}
}
}
If you are new to GraphQL-Codegen, you can follow a tutorial about how to get started with GraphQL codegen
You can also find here a blog post about how to use GraphQL Code-Generator with the typescript-resolvers
plugin.
type User { ... }
and your Prisma model is using model User { }
, you might have a naming conflict.type Query {
feed(filter: String, skip: Int, take: Int): Feed!
}
filter
, skip
and take
are nullable which means that GraphQL will send them as null if left without value.undefined
, but not null
.null
able), could be null
(null | undefined | T
).mappers
.inputMaybeValue
.inputMaybeValue
, lets you change the types that arguments can be!codegen.yml
!User
, and my GraphQL schema also uses a type User
.schema: http://localhost:3000/graphql
documents: ./src/graphql/*.graphql
generates:
graphql/generated.ts:
plugins:
- typescript-operations
- typescript-resolvers
config:
mappers:
User: .prisma/client#User as UserModel
mappers
, you can see we take the GraphQL User
type, and set it to be using the exported type automatically created by Prisma.UserModel
, so it won't conflict with the GraphQL definition of the GraphQL User
type.inputMaybeValue
is fairly simple to use, just add it under codegen.yml
config file:schema: http://localhost:3000/graphql
documents: ./src/graphql/*.graphql
generates:
graphql/generated.ts:
plugins:
- typescript-operations
- typescript-resolvers
config:
mappers:
User: .prisma/client#User as UserModel
inputMaybeValue: undefined | T
inputMaybe
(The type of nullable arguments) will be either undefined
or T
, leading to an easy type-compatibility between your GraphQL input
arguments and the Prisma SDK requirements.import { Resolvers } from './generated/graphql'
const resolvers: Resolvers = {
Query: {
user: async (parent, args, context) => {
// Codegen will generate Resolvers type, and
// will expect you to return here an object of
// Prisma's User model.
return context.prisma.user.findOne({ id: args.id })
}
},
User: {
name: (user) => {
return `${user.first_name} ${user.last_name}`
}
}
}