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

Validation of variable values #3169

Open
dkbarn opened this issue Jun 11, 2021 · 5 comments
Open

Validation of variable values #3169

dkbarn opened this issue Jun 11, 2021 · 5 comments

Comments

@dkbarn
Copy link

dkbarn commented Jun 11, 2021

I'm curious why it's not possible to validate the values of variables using the validate framework.

For example, suppose I have a very simple validator that wants to assert the value for argument name contains only ascii characters, given this schema:

type Query {
    hello(name: String!): String!
}

I can implement this validator and it will work when the incoming request is this:

{
    'query': '{ hello(name: "foo") }'
}

but when the request is the following, I have no way of seeing the value for the VariableNode $myName from inside the validator:

{
    'query': 'query myQuery($myName: String!) { hello(name: $myName) }',
    'variables': {'myName': 'foo'}
}

It looks like it comes down to this bit of the code:
https://github.com/graphql/graphql-js/blob/main/src/graphql.ts#L118-L122

Is there any reason why variableValues cannot also be passed in and made available to the validate framework?

@glasser
Copy link
Contributor

glasser commented Jul 16, 2021

My understanding is that the whole concept behind "validation" is that it is specifically a function of "operation + schema". So for example, you can cache the result of parsing and validation and not run it multiple times for the same operation and schema if only variables are changing. (Apollo Server does this!)

That said, I would love to be able to separate the getVariableValues stage out of execute so that we can report them with different kinds of error codes (and plugin callbacks!) in our request processing pipeline. @IvanGoncharov do you think that sort of change would be a reasonable PR? Would love to brainstorm exactly how to structure it. (We'd similarly want to pull out the "make sure an operation name is provided if needed and that it matches an operation in the document if provided" step.)

@ArchitectKatherineGraham
Copy link

@yaacovCR
Copy link
Contributor

@glasser @IvanGoncharov

i think that is accomplished by #3193 where creating an object of the Executor class with execution args throws with GraphQLErrors if those args contain problems like a “valid” but non-executable document or problems with variables. execute in #3193 catches those errors and reports them early, but Apollo could do whatever it wants with them.

#3193 is a larger refactor than you need, to support what you need, graphql js could just export the buildExecutionContext function and the functions that execute based on that. Having said that, another use case for #3193 and the supporting PR stack is a very welcome start to the day.

@glasser
Copy link
Contributor

glasser commented Jul 20, 2021

Sure, #3193 seems quite attractive from this perspective.

(If we needed to have graphql 15 support, we could polyfill Executor and just have the optimal error handling only work with the non-polyfilled version.)

@yaacovCR
Copy link
Contributor

Update: #3302 supersedes #3193.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants