Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cost_validator raises GraphQLError when used to validate query without required variables being provided #504

Closed
pigletto opened this issue Mar 23, 2021 · 2 comments
Labels
docs Focused on documentation contents
Milestone

Comments

@pigletto
Copy link
Contributor

Problem description

If the query with variables is used when cost_validator is enabled validation fails with the following error:

GraphQLError("Argument 'id' of required type 'Int!' was provided the variable '$id' which was not provided a runtime value.\n\nGraphQL request:1:34\n1 | query Hello($id: Int!){ hello(id:$id) }\n | ^")]

Version

Ariadne 0.13.0

How to reproduce:

from ariadne.validation import cost_validator
from graphql.language import parse
from graphql.validation import validate

type_defs = gql(
    """
    type Query {
        hello(id:Int!): String!
    }
""")

if __name__ == "__main__":
    schema = make_executable_schema(type_defs)
    document = parse('query Hello($id: Int!){ hello(id:$id) }')
    validation_errors = validate(schema, document, [cost_validator(maximum_cost=5)])
    print('errors', validation_errors)

I can make it work by disabling cost_validator or by explicitly setting variables to cost_validator constructor (like below) but this doesn't seem to make any sense for bigger schemas:

validation_errors = validate(schema, document, [cost_validator(maximum_cost=5, variables={'id': 10})])

@rafalp
Copy link
Contributor

rafalp commented Mar 23, 2021

Cost validator requires that query is executable. This means you can't define variables in query but don't provide them to validator, as such query is not executable.

You will have to setup cost validator dynamically for your code to work:

def get_validation_rules(context_value, document, data):
    return [cost_validator(maximum_cost=5, variables=data.get("variables"))]

@rafalp rafalp changed the title cost_validator raises GraphQLError if variables are used in query cost_validator raises GraphQLError when used to validate query without required variables being provided Mar 23, 2021
@rafalp rafalp added the decision needed Sounds like good idea, but will need closer scrutiny for final decision. label Mar 23, 2021
@pigletto
Copy link
Contributor Author

Thanks, this works. I've created PR to have this information in the docs.

@rafalp rafalp added this to the Ariadne 0.14 milestone Aug 24, 2021
@rafalp rafalp closed this as completed Aug 24, 2021
@rafalp rafalp added docs Focused on documentation contents and removed decision needed Sounds like good idea, but will need closer scrutiny for final decision. labels Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Focused on documentation contents
Projects
None yet
Development

No branches or pull requests

2 participants