Skip to content

validate constant directive values #3927

@jbellenger

Description

@jbellenger

Consider this query, which uses a variable in a directive that is itself applied to a variable definition:

query ($v:Int @dir(arg:$v)) { __typename }

The spec's grammar for VariableDefinition implies that directives applied to variable definitions are constant and that this usage is invalid.

This pattern is not rejected by the graphql-java validator when it should be. For comparison, graphql-js rejects this query:

import { buildSchema, parse, validate } from 'graphql';

const schemaString = `
  directive @dir(arg:Int) on VARIABLE_DEFINITION | FIELD
  type Query { x: Int }
`;

const documentString = `query ($v:Int @dir(arg:$v)) { x @dir(arg:$v) }`;
const errors = validate(buildSchema(schemaString), parse(documentString));
errors.forEach((err) => console.log(`${err.message}`));


// running the above script throws this error:
GraphQLError: Syntax Error: Unexpected variable "$v" in constant value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Stalekeep-openTells Stale Bot to keep PRs and issues open

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions