Skip to content

Commit 1e3a23a

Browse files
committed
feat: add description support for OperationDefinition
Implements GraphQL September 2025 spec for operation descriptions. Operations can now have string or block string descriptions. Ref: graphql/graphql-spec#1170
1 parent 88ac8b8 commit 1e3a23a

File tree

5 files changed

+180
-1
lines changed

5 files changed

+180
-1
lines changed

v2/pkg/ast/ast_operation_definition.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (t OperationType) Name() string {
3434
}
3535

3636
type OperationDefinition struct {
37+
Description Description // optional, describes the operation
3738
OperationType OperationType // one of query, mutation, subscription
3839
OperationTypeLiteral position.Position // position of the operation type literal, if present
3940
Name ByteSliceReference // optional, user defined name of the operation
@@ -63,6 +64,14 @@ func (d *Document) OperationDefinitionNameString(ref int) string {
6364
return unsafebytes.BytesToString(d.Input.ByteSlice(d.OperationDefinitions[ref].Name))
6465
}
6566

67+
func (d *Document) OperationDefinitionDescriptionBytes(ref int) ByteSlice {
68+
return d.Input.ByteSlice(d.OperationDefinitions[ref].Description.Content)
69+
}
70+
71+
func (d *Document) OperationDefinitionDescriptionString(ref int) string {
72+
return unsafebytes.BytesToString(d.Input.ByteSlice(d.OperationDefinitions[ref].Description.Content))
73+
}
74+
6675
func (d *Document) AddOperationDefinitionToRootNodes(definition OperationDefinition) Node {
6776
d.OperationDefinitions = append(d.OperationDefinitions, definition)
6877
node := Node{Kind: NodeKindOperationDefinition, Ref: len(d.OperationDefinitions) - 1}

v2/pkg/astparser/parser.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,8 +751,10 @@ func (p *Parser) parseRootDescription() {
751751
p.parseExtension()
752752
case identkeyword.SCHEMA:
753753
p.parseSchemaDefinition(&description)
754+
case identkeyword.QUERY, identkeyword.MUTATION, identkeyword.SUBSCRIPTION:
755+
p.parseOperationDefinitionWithDescription(&description)
754756
default:
755-
p.errUnexpectedIdentKey(p.read(), next, identkeyword.TYPE, identkeyword.INPUT, identkeyword.SCALAR, identkeyword.INTERFACE, identkeyword.UNION, identkeyword.ENUM, identkeyword.DIRECTIVE)
757+
p.errUnexpectedIdentKey(p.read(), next, identkeyword.TYPE, identkeyword.INPUT, identkeyword.SCALAR, identkeyword.INTERFACE, identkeyword.UNION, identkeyword.ENUM, identkeyword.DIRECTIVE, identkeyword.QUERY, identkeyword.MUTATION, identkeyword.SUBSCRIPTION)
756758
}
757759
}
758760

@@ -1465,9 +1467,17 @@ func (p *Parser) parseTypeCondition() (typeCondition ast.TypeCondition) {
14651467
}
14661468

14671469
func (p *Parser) parseOperationDefinition() {
1470+
p.parseOperationDefinitionWithDescription(nil)
1471+
}
1472+
1473+
func (p *Parser) parseOperationDefinitionWithDescription(description *ast.Description) {
14681474

14691475
var operationDefinition ast.OperationDefinition
14701476

1477+
if description != nil {
1478+
operationDefinition.Description = *description
1479+
}
1480+
14711481
next, literal := p.peekLiteral()
14721482
switch next {
14731483
case keyword.IDENT:

v2/pkg/astparser/parser_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,122 @@ func TestParser_Parse(t *testing.T) {
19861986
role#comment
19871987
}#comment`, parse, false)
19881988
})
1989+
1990+
t.Run("query with description", func(t *testing.T) {
1991+
run(`"Fetches a user by ID" query GetUser($id: ID!) { user(id: $id) { id name } }`, parse, false,
1992+
func(doc *ast.Document, extra interface{}) {
1993+
query := doc.OperationDefinitions[0]
1994+
if query.OperationType != ast.OperationTypeQuery {
1995+
panic("want OperationTypeQuery")
1996+
}
1997+
if !query.Description.IsDefined {
1998+
panic("want description to be defined")
1999+
}
2000+
description := doc.Input.ByteSliceString(query.Description.Content)
2001+
if description != "Fetches a user by ID" {
2002+
panic(fmt.Errorf("want 'Fetches a user by ID', got '%s'", description))
2003+
}
2004+
if doc.Input.ByteSliceString(query.Name) != "GetUser" {
2005+
panic("want GetUser")
2006+
}
2007+
})
2008+
})
2009+
2010+
t.Run("query with block string description", func(t *testing.T) {
2011+
run(`"""
2012+
Fetches a user by their unique identifier.
2013+
This operation returns detailed user information.
2014+
"""
2015+
query GetUser($id: ID!) { user(id: $id) { id name } }`, parse, false,
2016+
func(doc *ast.Document, extra interface{}) {
2017+
query := doc.OperationDefinitions[0]
2018+
if query.OperationType != ast.OperationTypeQuery {
2019+
panic("want OperationTypeQuery")
2020+
}
2021+
if !query.Description.IsDefined {
2022+
panic("want description to be defined")
2023+
}
2024+
if !query.Description.IsBlockString {
2025+
panic("want block string description")
2026+
}
2027+
description := doc.Input.ByteSliceString(query.Description.Content)
2028+
expected := "Fetches a user by their unique identifier.\nThis operation returns detailed user information."
2029+
if description != expected {
2030+
panic(fmt.Errorf("want '%s', got '%s'", expected, description))
2031+
}
2032+
})
2033+
})
2034+
2035+
t.Run("mutation with description", func(t *testing.T) {
2036+
run(`"Creates a new user" mutation CreateUser($input: UserInput!) { createUser(input: $input) { id } }`, parse, false,
2037+
func(doc *ast.Document, extra interface{}) {
2038+
mutation := doc.OperationDefinitions[0]
2039+
if mutation.OperationType != ast.OperationTypeMutation {
2040+
panic("want OperationTypeMutation")
2041+
}
2042+
if !mutation.Description.IsDefined {
2043+
panic("want description to be defined")
2044+
}
2045+
description := doc.Input.ByteSliceString(mutation.Description.Content)
2046+
if description != "Creates a new user" {
2047+
panic(fmt.Errorf("want 'Creates a new user', got '%s'", description))
2048+
}
2049+
})
2050+
})
2051+
2052+
t.Run("subscription with description", func(t *testing.T) {
2053+
run(`"""Subscribes to user updates""" subscription UserUpdated($userId: ID!) { userUpdated(userId: $userId) { id } }`, parse, false,
2054+
func(doc *ast.Document, extra interface{}) {
2055+
subscription := doc.OperationDefinitions[0]
2056+
if subscription.OperationType != ast.OperationTypeSubscription {
2057+
panic("want OperationTypeSubscription")
2058+
}
2059+
if !subscription.Description.IsDefined {
2060+
panic("want description to be defined")
2061+
}
2062+
description := doc.Input.ByteSliceString(subscription.Description.Content)
2063+
if description != "Subscribes to user updates" {
2064+
panic(fmt.Errorf("want 'Subscribes to user updates', got '%s'", description))
2065+
}
2066+
})
2067+
})
2068+
2069+
t.Run("multiple operations with and without descriptions", func(t *testing.T) {
2070+
run(`
2071+
"Get user query"
2072+
query GetUser { user { id } }
2073+
2074+
mutation CreateUser { createUser { id } }
2075+
2076+
"Subscribe to updates"
2077+
subscription Updates { updates { id } }
2078+
`, parse, false,
2079+
func(doc *ast.Document, extra interface{}) {
2080+
// First operation with description
2081+
query := doc.OperationDefinitions[0]
2082+
if !query.Description.IsDefined {
2083+
panic("want first operation description to be defined")
2084+
}
2085+
if doc.Input.ByteSliceString(query.Description.Content) != "Get user query" {
2086+
panic("want 'Get user query'")
2087+
}
2088+
2089+
// Second operation without description
2090+
mutation := doc.OperationDefinitions[1]
2091+
if mutation.Description.IsDefined {
2092+
panic("want second operation description to not be defined")
2093+
}
2094+
2095+
// Third operation with description
2096+
subscription := doc.OperationDefinitions[2]
2097+
if !subscription.Description.IsDefined {
2098+
panic("want third operation description to be defined")
2099+
}
2100+
if doc.Input.ByteSliceString(subscription.Description.Content) != "Subscribe to updates" {
2101+
panic("want 'Subscribe to updates'")
2102+
}
2103+
})
2104+
})
19892105
})
19902106
t.Run("variable definition", func(t *testing.T) {
19912107
t.Run("simple", func(t *testing.T) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
Fetches a user by their unique identifier.
3+
This operation returns detailed user information including profile data.
4+
"""
5+
query GetUser($id: ID!) {
6+
user(id: $id) {
7+
id
8+
name
9+
email
10+
}
11+
}
12+
13+
"Creates a new user account with the provided information"
14+
mutation CreateUser($input: UserInput!) {
15+
createUser(input: $input) {
16+
id
17+
name
18+
}
19+
}
20+
21+
"""
22+
Subscribes to real-time user updates.
23+
Receives notifications when user data changes.
24+
"""
25+
subscription UserUpdated($userId: ID!) {
26+
userUpdated(userId: $userId) {
27+
id
28+
name
29+
status
30+
}
31+
}
32+
33+
# Operation without description for backward compatibility
34+
query GetAllUsers {
35+
users {
36+
id
37+
name
38+
}
39+
}

v2/pkg/astprinter/astprinter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ func (p *printVisitor) LeaveArgument(ref int) {
276276

277277
func (p *printVisitor) EnterOperationDefinition(ref int) {
278278

279+
if p.document.OperationDefinitions[ref].Description.IsDefined {
280+
p.must(p.document.PrintDescription(p.document.OperationDefinitions[ref].Description, nil, 0, p.out))
281+
p.write(literal.LINETERMINATOR)
282+
}
283+
279284
hasName := p.document.OperationDefinitions[ref].Name.Length() > 0
280285
hasVariables := p.document.OperationDefinitions[ref].HasVariableDefinitions
281286

0 commit comments

Comments
 (0)