Search code examples
graphqlintegration-testingsupertest

graphql supertest , error message: 'Cannot return null for non-nullable field Mutation


consider this simple resolver for user creation and update:

@Resolver(() => User)
export class UserResolver {
  constructor(private userService: UserService) {}

  @Mutation(() => User)
  async CreateUser(@Args('payload') payload: CreateUserInput) {
    return this.userService.createUser(payload);
  }

  @Mutation(() => User)
  async UpdateUser(@Args('payload') payload: UpdateUserInput) {
    return this.userService.updateUser(payload);
  }

when I create and update using grapqhl playground, it works nice :

correct form In running tests, the same query is ok for creation(as logged in the image) but has error with update mutation which is same in both queries:

export const UpdateUserMutation = `
mutation UpdateUser( $id:String!, $email:String ) {
  UpdateUser(payload: { _id: $id, email: $email }) {
    _id
    phone
    email
  }
}
`;

error messaage

so am i missing somthing?


Solution

  • In my tests, I was using beforeEach and afterEach to reset the DB and the server. by using beforeAll and afterAll the created user were kept so that user was updated successfully.