Allow creating aliases for builtin types, using typename. (#133)
## Summary:
This lets you write code like:
```
query x {
# @genqlient(typename: "MyString")
someStringField
}
```
and genqlient will do
```
typename MyString string
type x struct {
someStringField MyString
}
```
This was not difficult to implement, though it required introducing a
new identifier type. The main difficulty I had was weird test
failures, that it turns out was due to the tests putting a bunch of
fields on the same line, so that the genqlient directive on the
previous line applied to all of them, accidentally. This became a
problem when `typename` suddenly started being respected for builtin
types! I fixed it by just spreading out the queries a bit.
Fixes #130
## Test plan:
make check
Author: csilvers
Reviewers: dnerdy, StevenACoffman, benjaminjkraft
Required Reviewers:
Approved By: StevenACoffman
Checks: ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Lint, ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Lint
Pull Request URL: https://github.com/Khan/genqlient/pull/133
This commit is contained in:
+12
-1
@@ -289,7 +289,7 @@ func (g *generator) convertDefinition(
|
||||
}, err
|
||||
}
|
||||
goBuiltinName, ok := builtinTypes[def.Name]
|
||||
if ok {
|
||||
if ok && options.TypeName == "" {
|
||||
return &goOpaqueType{GoRef: goBuiltinName, GraphQLName: def.Name}, nil
|
||||
}
|
||||
|
||||
@@ -479,6 +479,17 @@ func (g *generator) convertDefinition(
|
||||
return g.addType(goType, goType.GoName, pos)
|
||||
|
||||
case ast.Scalar:
|
||||
if builtinTypes[def.Name] != "" {
|
||||
// In this case, the user asked for a custom Go type-name
|
||||
// for a built-in type, e.g. `type MyString string`.
|
||||
goType := &goTypenameForBuiltinType{
|
||||
GoTypeName: name,
|
||||
GoBuiltinName: builtinTypes[def.Name],
|
||||
GraphQLName: def.Name,
|
||||
}
|
||||
return g.addType(goType, goType.GoTypeName, pos)
|
||||
}
|
||||
|
||||
// (If you had an entry in bindings, we would have returned it above.)
|
||||
return nil, errorf(
|
||||
pos, `unknown scalar %v: please add it to "bindings" in genqlient.yaml`, def.Name)
|
||||
|
||||
Reference in New Issue
Block a user