Resolver
export const resolvers = {
...
Mutation: {
register: async (
_: unknown,
{ input }: { input: RegisterInput },
{ prisma }: GraphQLRequestContext
): Promise<RegisterResult> => {
const { email, password, lastName, firstName } = input
console.log('hello')
...
(some ifs that return {
__typename: 'BadRequest',
message: 'Bad request',
})
...
return { __typename: 'AuthResult', token }
},
},
}
Typedefs
export const typeDefs = gql`
type BadRequest {
message: String!
}
type Mutation {
register(input: RegisterInput!): RegisterResult!
}
input RegisterInput {
email: String!
password: String!
firstName: String
lastName: String
}
type AuthResult {
token: String!
}
union RegisterResult = AuthResult | BadRequest
`
Server
const server = new ApolloServer({
typeDefs,
resolvers,
context: () => ({ prisma }),
})
When making a request with this mutation I get "Cannot return null for non-nullable field Mutation.register."
If I make RegisterResult nullable in the mutation result I get
{
"data": {
"register": null
}
}
The console log in the resolver does not appear in the logs, in either case. The types are generated automatically from the typedefs.
resolvers? If youconsole.log(resolvers);at the point where you callnew ApolloServeris it the object you expect? - loganfsmythimport { resolvers } from './resolvers'. Moving it out of the index file fixes this, importing from the index file at all is giving this problem still, I've never experienced this before - user3287552