graphql / graphql/graphql-spec
Proposal: Shorthand for variables/arguments on an operation
- Dominant language
- JavaScript
- Stars
- 14.6k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
### Background
This is a very initial idea for allowing a shorthand for using/matching variables and arguments on a selection set.
In many projects we (The Guild) worked with, we saw this repeating pattern of using the same name for variables and arguments.
### Proposal
Today we are doing the following in order to declare a variable, and match it with an argument:
```graphql
query user($id: ID!) {
user(id: $id) {
# ...
}
}
```
But, if the variable and the argument have the same name, we should be able to do the following:
```graphql
query user($id: ID!) {
user($id) {
# ...
}
}
```
So if the `$id` is used as-is, without an explicit argument name, it will match the `id` argument.
I think there is no real need to provide the argument name if it's the same as the variable name, and it will simplify the usage.
This is similar to the JS shorthand while working with objects, you can do `{ foo }` instead of `{ foo: foo }`.
I think this kind of shorthand can be implemented by changing a few lines of code in the `parser` / `lexer` implementation: if there is a variable instead of an argument - we can just use the variable name as the argument name and the rest remains the same.
Contributor guide
Assessment
This issue has not been assessed yet.