Skip to content

parameterized query variable values aren't passed to parseLiteral of GraphQLScalarTypes #433

Closed
@jedwards1211

Description

@jedwards1211

When I call my login method like this, it calls my Password type's parseLiteral method which throws an error as expected:

query {
  login(username:"andy", password:"aaaaaaaaa") {
    authToken
  }
}

Result:

{ errors: 
   [ [Error: Password did not met strength requirements:
     The password must be at least 10 characters long.
     The password may not contain sequences of three or more repeated characters.] ] }

But if I change that to a parameterized query with the same values in the variables, it never calls my Password type's parseLiteral, and it succeeds. Am I misunderstanding something, or is this a bug? I couldn't find anything in the documentation about different validation behavior for parameterized queries.

query ($username: Username, $email: Email, $password: Password!) {
  login(username: $username, email: $email, password: $password) {
    authToken
  }
}

Variables:

{
  "username": "andy",
  "password": "aaaaaaaaa"
}

Result:

{
  "data": {
    "login": {
      "authToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjFlMzM3MzliLTlhYjktNDczZi04NTMxLThmYjhkOWFjMjYyZCIsImlhdCI6MTQ2ODQ2NTc2MywiZXhwIjoxNDY5MDcwNTYzfQ.pcedaOASV8U2-fBHnGxQlHs26SyhuTzLZC1T3EVgndI"
    }
  }
}

Here is my Password type:

export const GraphQLPasswordType = new GraphQLScalarType({
  name: 'Password',
  serialize: value => String(value),
  parseValue: value => String(value),
  parseLiteral: ast => {
    if (ast.kind !== Kind.STRING) {
      throw new GraphQLError(`Password is not a string, it is a: ${ast.kind}`, [ast])
    }
    const result = testPassword(ast.value)
    if (result.requiredTestErrors && result.requiredTestErrors.length) {
      throw new GraphQLError(`Password did not met strength requirements:
${result.requiredTestErrors.join('\n')}`, [ast])
    }
    return String(ast.value)
  }
})

And the login query schema:

  login: {
    type: UserWithAuthToken,
    args: {
      username: {type: GraphQLUsernameType},
      email: {type: GraphQLEmailType},
      password: {type: new GraphQLNonNull(GraphQLPasswordType)}
    },
    async resolve(source, args) {
      ...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions