-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Infer arguments and their types #268
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
Comments
Personally, I like the clarity of the variable names, types and defaults at the top of the operation. As a reader, I can quickly see whether the variables I'm sending to the server match the ones required by the operation. |
I thought of this, but wouldn't you then also want to replace queries like this:
with something like this:
so you would quickly see if the types coming in from the server match the ones you use in client side code? Seems to me like this kind of explicit type annotations should be optional, not required. |
The point of the type annotations is to help client tooling. Clients may easily see which variables an operation requires and enforce those variables. It also allows for a user to define default values and enforce non-nullability even when a variable is going to a non-null field. For example: query ($nonNullArgument: String!, $defaultValueArgument: Int = 42) {
field(
nullableArgument: $nonNullArgument
defaultValueArgument: $defaultValueArgument
)
} A type error may also be easily caught by the GraphQL server if we are trying to pass say a Also, in a world where fragments have variable definitions, these benefits may further be seen. I like the explicit nature of the variable definitions, but if this is something you don’t like then there is nothing stopping you from writing your own GraphQL validation rules in your server implementation of choice 😊 |
True, but it still seems inconsistent to me:
Wouldn't it also help client tooling if queries that receive data were fully annotated? |
The difference here is input vs. output. It makes sense to annotate input so that the server knows exactly what the client is trying to do. Output, on the other hand, is completely defined by the server and consistent across requests. |
I think these should be optional - they are useful to do validation or code generation, but when you're using GraphQL with a dynamically typed language where nothing is actually going to validate those arguments for you, it seems unnecessary to require these annotations. Also, if you have tooling like Flow or TypeScript, couldn't that infer the variable types from the field arguments? |
This issue seems like a duplicate of #251 |
Indeed. I will close this one and follow the other one. Thanks! |
It seems to me like there is needless repetition in queries like this:
whereas using the schema, the same information could be derived from something like this:
If I leave any arguments undeclared, GraphQL tells me which ones I need to declare.
If I declare them with types that don't match the schema, GraphQL tells me what the types should be.
So basically, when writing queries like these, GraphQL is asking the user for information it already knows and punishes them (with errors) if they get any of it wrong.
Is there a good reason why it works this way instead of inferring the arguments and/or their types automatically?
The text was updated successfully, but these errors were encountered: