Search code examples
javascriptnode.jstypescriptgraphqltypeorm

Mutation return null on signup


I'm building an API with graphql, typegraphql and typeorm using typescript and I have my signup mutation who returns null when I return the user but it return the errors without problem. I precise that when I use the signup mutation the user is correctly saved in the databse (I use postgres) and I can console.log the result of the query

signup mutation:

@Mutation((_) => UserResponse)
  async signup(
    @Arg('email') email: string,
    @Arg('password') password: string
  ): Promise<UserResponse> {
    const errors = await signupValidation(email, password);

    if (errors.length > 0) {
      return { errors };
    }

    const result = await getConnection()
      .createQueryBuilder()
      .insert()
      .into(User)
      .values([{ email, password }])
      .returning('*')
      .execute();
    console.log(result.raw[0])
    return result.raw[0];
  }

the console.log's result enter image description here

And here is me calling the mutation (no errors) enter image description here

And here is the result where there is an error enter image description here


Solution

  • It seems your function is returning a User-shaped object, not a UserResponse (with user and errors properties).

    Change your code to

    return { user: result.raw[0] };
    

    (and maybe try to figure out why TypeScript didn't complain - is result typed as any?)