Skip to content

Commit bc17161

Browse files
authored
Add field aliasing (#376)
Added @genqlient(alias) directive to customize field names without GraphQL aliases (fixes #367)
1 parent 86db6f0 commit bc17161

7 files changed

+119
-0
lines changed

docs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ When releasing a new version:
2424

2525
### New features:
2626

27+
- Added `@genqlient(alias)` directive to customize field names without requiring GraphQL aliases (fixes #367)
28+
2729
### Bug fixes:
2830

2931
- fixed documentation link in `introduction.md`

docs/genqlient_directive.graphql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ directive genqlient(
151151
# of MyField; what if we got back the other type?).
152152
flatten: Boolean
153153

154+
# If set, this field will use the provided name as the Go field name,
155+
# without creating an alias in the GraphQL query.
156+
#
157+
# For example, given a query like
158+
# query MyQuery {
159+
# # @genqlient(alias: "MyGreatName")
160+
# myField
161+
# }
162+
# genqlient will generate:
163+
# type MyQueryResponse struct {
164+
# MyGreatName <type> `json:"myField"`
165+
# }
166+
#
167+
# This is similar to the GraphQL alias syntax (e.g. `myGreatName: myField`),
168+
# but it only affects the Go field name, not the GraphQL query. This is
169+
# especially useful when working with GraphQL servers that limit the number
170+
# of aliases you can use in a query.
171+
alias: String
172+
154173
# If set, this argument or field will use the given Go type instead of a
155174
# genqlient-generated type.
156175
#

generate/convert.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,10 @@ func (g *generator) convertField(
918918
}
919919

920920
goName := upperFirst(field.Alias)
921+
if fieldOptions.Alias != "" {
922+
goName = upperFirst(fieldOptions.Alias)
923+
}
924+
921925
namePrefix = nextPrefix(namePrefix, field)
922926

923927
fieldGoType, err := g.convertType(

generate/genqlient_directive.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type genqlientDirective struct {
1818
Flatten *bool
1919
Bind string
2020
TypeName string
21+
Alias string
2122
// FieldDirectives contains the directives to be
2223
// applied to specific fields via the "for" option.
2324
// Map from type-name -> field-name -> directive.
@@ -52,6 +53,9 @@ func (dir *genqlientDirective) argsString() string {
5253
if dir.TypeName != "" {
5354
parts = append(parts, fmt.Sprintf("typename: %v", dir.TypeName))
5455
}
56+
if dir.Alias != "" {
57+
parts = append(parts, fmt.Sprintf("alias: %v", dir.Alias))
58+
}
5559
return strings.Join(parts, ", ")
5660
}
5761

@@ -170,6 +174,8 @@ func (dir *genqlientDirective) add(graphQLDirective *ast.Directive, pos *ast.Pos
170174
err = setString("bind", &dir.Bind, arg.Value, pos)
171175
case "typename":
172176
err = setString("typename", &dir.TypeName, arg.Value, pos)
177+
case "alias":
178+
err = setString("alias", &dir.Alias, arg.Value, pos)
173179
case "for":
174180
// handled above
175181
default:
@@ -441,6 +447,7 @@ func (dir *genqlientDirective) mergeOperationDirective(
441447
// typename isn't settable on the operation (when set there it replies to
442448
// the response-type).
443449
fillDefaultString(&dir.TypeName, forField.TypeName)
450+
fillDefaultString(&dir.Alias, forField.Alias, operationDirective.Alias)
444451
}
445452

446453
// parsePrecedingComment looks at the comment right before this node, and
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
query AliasDirective {
2+
# @genqlient(alias: "MyUser")
3+
user {
4+
# @genqlient(alias: "UserID")
5+
id
6+
# @genqlient(alias: "UserName")
7+
name
8+
}
9+
}

generate/testdata/snapshots/TestGenerate-AliasDirective.graphql-AliasDirective.graphql.go

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"operations": [
3+
{
4+
"operationName": "AliasDirective",
5+
"query": "\nquery AliasDirective {\n\tuser {\n\t\tid\n\t\tname\n\t}\n}\n",
6+
"sourceLocation": "testdata/queries/AliasDirective.graphql"
7+
}
8+
]
9+
}

0 commit comments

Comments
 (0)