-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reject operation or argument names that are Go keywords (#195)
GraphQL is pretty restrictive about its identifiers, so for the most part we can and do safely use GraphQL identifiers in the Go we generate with attention only to conflicts with other such identifiers. But we do need to check one thing, which is that the identifier isn't a Go keyword. (If it is, the generated code will almost certainly fail to compile, but often with a confusing error message.) In this commit I add such checks. The most likely place to run into trouble here is argument names, which are often one word and are used as-is. Operation names, if unexported, can also be keywords, although in practice they're usually multiword. Field names are always exported, thus safe. Generated type names are always prefixed, camel-cased, with the operation name, so they always contain an uppercase letter (even if the operation name is lowercase), but type-names specified by `typename` may collide, so we check those. In theory we could check type-names specified by `bind`, but these must be defined by the user, so their code will already fail to compile, so I didn't bother. I think that's all the places to consider, although it's hard to be sure. In summary, we check argument names, operation names, and user-specified type names. Test plan: make check
- Loading branch information
1 parent
482a59b
commit 39cd158
Showing
13 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
query KeywordArgumentName($type: String) { | ||
f(type: $type) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type Query { f(type: String): String } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
query type { f } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
type Query { f: String } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
query KeywordTypeName { | ||
# @genqlient(typename: "type") | ||
f { | ||
g | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
type Query { f: T } | ||
type T { g: String } |
1 change: 1 addition & 0 deletions
1
generate/testdata/snapshots/TestGenerateErrors-KeywordArgumentName-graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testdata/errors/KeywordArgumentName.graphql:1: variable name must not be a go keyword |
1 change: 1 addition & 0 deletions
1
generate/testdata/snapshots/TestGenerateErrors-KeywordOperationName-graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testdata/errors/KeywordOperationName.graphql:1: operation name must not be a go keyword |
1 change: 1 addition & 0 deletions
1
generate/testdata/snapshots/TestGenerateErrors-KeywordTypeName-graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testdata/errors/KeywordTypeName.schema.graphql:1: typename option must not be a go keyword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters