0
votes

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.

1
Out of curiosity, how are you importing resolvers? If you console.log(resolvers); at the point where you call new ApolloServer is it the object you expect? - loganfsmyth
@loganfsmyth bizarrely it is out of date? It's in a ./resolvers/index.ts which I reference like import { 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
Weird, not sure what to say there. Maybe your build is caching something wrong somehow. - loganfsmyth

1 Answers

0
votes

Could you please share a sample of how you are calling this mutation? Seems like from the error message that you are not passing in the required fields.

These should be passed in :

email: String!
password: String!