Problem with Lambda mutation

What I want to do

Make a lambda mutation that creates a user and returns the username and JWT token as a result.

What I did

This is the lambda function

import jwt from 'jsonwebtoken';

async function signupUser({args, graphql}){
    const foundUser = await graphql(`
    mutation (
        $username: String!,
        $email: String!,
        $password: String!
      ){
        addUser(input: [{
            username: $username
            email: $email
            password: $password
            role: USER
            characters: []
        }]){
            user{
                username
            }
        }
      }
    `, {
        "$username": args.username,
        "$email": args.email,
        "$password": args.password
    });
    if(foundUser.data){
        var token = await jwt.sing({
            data: {
                username: foundUser.data.addUser.user[0].username
            }
          }, 'YourSecretKey', { expiresIn: '1h' });

        console.log("Lambda result:");
        console.log(token);
        return {username: foundUser.data.addUser.user[0].username, token};
    }
    console.log(foundUser);
    return {username: "Not found!!!", token: "12345"};
    
};

self.addGraphQLResolvers({
    "Mutation.signup": signupUser,
})

Problem

It wont get any data back from the mutation. If i run the same mutation in insomnia it creates the user correctly.

The message it returns is following:

{ errors: [ { message: 'must be defined', path: [Array] } ] }

Not sure if this is your actual code example, but you’ve misspelled ‘sign’.

Hi @SamSamit , can you please provide the

  1. Schema that you are using
  2. Mutation and variables that you are passing in insomnia.

Thanks

Thease are the types and mutation im using in schema regarding this problem

type User @secret(field: "password") {
  username: String! @id
  email: String!
  characters: [Character] @hasInverse(field: owner)
  role: UserRole! @search
}

enum UserRole {
  USER
  ADMIN
}

type Character
  @auth(
    query: {
      rule: """
      query ($USER: String!) {
          queryCharacter{
              owner(filter:{username: { eq: $USER }}){
                username
              }
          }
      }
      """
    }
  ) {
  id: ID!
  name: String!
  race: String!
  owner: User! @hasInverse(field: characters)
}

type Mutation {
  signup(
    username: String!
    email: String!
    password: String!
    password2: String!
  ): signupPayload! @lambda
}

type signupPayload {
  username: String
  token: String
}

And this is what im having in insomnia:

mutation addUser(
  $user: AddUserInput!,
){
 addUser(input: [$user]){
  user{
    username
    role
  }
} 

variables:

{
	"user": {
		"username": "user",
		"email": "user@email.com",
		"characters": [],
		"password": "123456",
		"role": "USER"
	}
}

Hi @SamSamit , i am not able to reproduce the error that you specified on addUser mutation.
Which version of Dgraph are you using ?
I tried it on master branch and our previous releases 20.11 and 20.07, and it works perfectly fine for me.
i used some part of your schema

type User @secret(field: "password") {
  username: String! @id
  email: String!
  characters: [Character] @hasInverse(field: owner)
  role: UserRole! @search
}

enum UserRole {
  USER
  ADMIN
}

type Character
  @auth(
    query: {
      rule: """
      query ($USER: String!) {
          queryCharacter{
              owner(filter:{username: { eq: $USER }}){
                username
              }
          }
      }
      """
    }
  ) {
  id: ID!
  name: String!
  race: String!
  owner: User! @hasInverse(field: characters)
}

And ran below mutation with variable, it works fine.

Ok as i said i got it to work with insomnia but when its inside the Lambda function it returns this

{ errors: [ { message: 'must be defined', path: [Array] } ] }

So i think there is something wrong with the lambda function but i cant figure out what it is.

ok, I thought you were getting this error while trying in insomnia. Let me try to reproduce it with lamda.

Hi @SamSamit,
Nice to see you on Discuss!

You don’t want to specify $ while specifying variable values in your lambda script. That is not required for GraphQL.

Hope you make good progress with your app.

Thanks

1 Like

Hello @abhimanyusinghgaur !
It is really nice to see you here too :smiley: you just swoop in and fixed my issue. True expert!

So the problem was those dollar signs ($) in front of the variable name. After that it works. Guess you just have to make these mistakes to learn.

1 Like