Skip to content

Commit 02af0fd

Browse files
committed
[RFC] Make operation name optional.
Based on feedback from those using GraphQL and inspired by RelayQL's syntax, this makes operation names optional. The primary value is the ability to express mutations (or eventually subscriptions) without being required to provide a name if the name adds no value for your use case. (Value typically being query disambiguation and logging). Systems which want to enforce a name for queries should have a separate linting/validation rule - such a rule would probably also want to ban "query shorthand"
1 parent 139a0a0 commit 02af0fd

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

spec/Appendix B -- Grammar Summary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Definition :
9292

9393
OperationDefinition :
9494
- SelectionSet
95-
- OperationType Name VariableDefinitions? Directives? SelectionSet
95+
- OperationType Name? VariableDefinitions? Directives? SelectionSet
9696

9797
OperationType : one of query mutation
9898

spec/Section 2 -- Language.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ contain an operation. However documents which do not contain operations may
178178
still be parsed and validated to allow client to represent a single request
179179
across many documents.
180180

181-
If a query document contains only one query operation, that operation may be
182-
represented in the shorthand form, which omits the query keyword and
181+
If a document contains only one operation, that operation may be unnamed or
182+
represented in the shorthand form, which omits both the query keyword and
183183
operation name. Otherwise, if a GraphQL query document contains multiple
184184
operations, each operation must be named. When submitting a query document with
185185
multiple operations to a GraphQL service, the name of the desired operation to
@@ -189,7 +189,7 @@ be executed must also be provided.
189189
### Operations
190190

191191
OperationDefinition :
192-
- OperationType Name VariableDefinitions? Directives? SelectionSet
192+
- OperationType Name? VariableDefinitions? Directives? SelectionSet
193193
- SelectionSet
194194

195195
OperationType : one of `query` `mutation`
@@ -199,7 +199,20 @@ There are two types of operations that GraphQL models:
199199
* query - a read-only fetch.
200200
* mutation - a write followed by a fetch.
201201

202-
Each operation is represented by an operation name and a selection set.
202+
Each operation is represented by an optional operation name and a selection set.
203+
204+
For example, this mutation operation might "like" a story and then retrieve the
205+
new number of likes:
206+
207+
```
208+
mutation {
209+
likeStory(storyID: 12345) {
210+
story {
211+
likeCount
212+
}
213+
}
214+
}
215+
```
203216

204217
**Query shorthand**
205218

0 commit comments

Comments
 (0)