Add support for concrete-typed named fragments (#75)
## Summary:
In previous commits I added support to genqlient for interfaces and
inline fragments. This means the only query structures that remain are
named fragments and their spreads, e.g.
```
fragment MyFragment on MyType { myField }
query MyQuery { getMyType { ...MyFragment } }
```
Other than mere completionism, these are potentially useful for code
sharing: you can spread the same fragment multiple places; and then
genqlient can notice that and generate the same type for each. (They
can even be shared between different queries in the same package.)
In this commit I add support for named fragments of concrete
(object/struct, not interface) type, spread into either concrete or
abstract scope. For genqlient's purposes, these are a new "root"
type-name, just like each operation, and are then embedded into the
appropriate struct. (Using embeds allows their fields to be referenced
as fields of the containing type, if convenient. Further design
considerations are discussed in DESIGN.md.)
This requires new code in two main places (plus miscellaneous glue),
both nontrivial but neither particularly complex:
- We need to actually traverse both structures and generate the types
(in `convert.go`).
- We need to decide which fragments from this package to send to the
server, both for good hyigene and because GraphQL requires we send
only ones this query uses (in `generate.go`).
- We need a little new wiring for options -- because fragments can be
shared between queries they get their own toplevel options, rather
than inheriting the query's options.
Finally, this required slightly subtler changes to how we do
unmarshaling (in `types.go` and `unmarshal.go.tmpl`). Basically,
because embedded fields' methods, including `UnmarshalJSON`, get
promoted to the parent type, and because the JSON library ignores their
fields when shadowed by those of the parent type, we need a little bit
of special logic in each such parent type to do its own unmarshal and
then delegate to each embed. This is similar (and much simpler) to
what we did for interfaces, although it required some changes to the
"method-hiding" trick (used for both). It's only really necessary in
certain specific cases (namely when an embedded type has an
`UnmarshalJSON` method or a field with the same name as the embedder),
but it's easier to just generate it always. This is all described in
more detail inline.
This does not support fragments of abstract type, which have their own
complexities. I'll address those, which are now the only remaining
piece of #8, in a future commit.
Issue: https://github.com/Khan/genqlient/issues/8
## Test plan:
make check
Author: benjaminjkraft
Reviewers: dnerdy, benjaminjkraft, aberkan, MiguelCastillo
Required Reviewers:
Approved By: dnerdy
Checks: ✅ Lint, ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Test (1.13), ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Test (1.13), ✅ Lint, ✅ Test (1.17), ✅ Test (1.16), ✅ Test (1.15), ✅ Test (1.14), ✅ Test (1.13), ✅ Lint
Pull Request URL: https://github.com/Khan/genqlient/pull/75
This commit is contained in:
+75
-2
@@ -330,7 +330,12 @@ func (g *generator) convertSelectionSet(
|
||||
}
|
||||
fields = append(fields, field)
|
||||
case *ast.FragmentSpread:
|
||||
return nil, errorf(selection.Position, "not implemented: %T", selection)
|
||||
maybeField, err := g.convertFragmentSpread(selection, containingTypedef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if maybeField != nil {
|
||||
fields = append(fields, maybeField)
|
||||
}
|
||||
case *ast.InlineFragment:
|
||||
// (Note this will return nil, nil if the fragment doesn't apply to
|
||||
// this type.)
|
||||
@@ -354,7 +359,9 @@ func (g *generator) convertSelectionSet(
|
||||
// GraphQL (and, effectively, JSON) requires that all fields with the
|
||||
// same alias (JSON-name) must be the same (i.e. refer to the same
|
||||
// field), so that's how we deduplicate.
|
||||
if fieldNames[field.JSONName] {
|
||||
// It's fine to have duplicate embeds (i.e. via named fragments), even
|
||||
// ones with complicated overlaps, since they are separate types to us.
|
||||
if field.JSONName != "" && fieldNames[field.JSONName] {
|
||||
// GraphQL (and, effectively, JSON) forbids you from having two
|
||||
// fields with the same alias (JSON-name) that refer to different
|
||||
// GraphQL fields. But it does allow you to have the same field
|
||||
@@ -444,6 +451,72 @@ func (g *generator) convertInlineFragment(
|
||||
containingTypedef, queryOptions)
|
||||
}
|
||||
|
||||
// convertFragmentSpread converts a single GraphQL fragment-spread
|
||||
// (`...MyFragment`) into a Go struct-field. If the fragment does not apply to
|
||||
// this type, returns nil.
|
||||
//
|
||||
// containingTypedef is as described in convertInlineFragment, above.
|
||||
func (g *generator) convertFragmentSpread(
|
||||
fragmentSpread *ast.FragmentSpread,
|
||||
containingTypedef *ast.Definition,
|
||||
) (*goStructField, error) {
|
||||
if !fragmentMatches(containingTypedef, fragmentSpread.Definition.Definition) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
typ, ok := g.typeMap[fragmentSpread.Name]
|
||||
if !ok {
|
||||
// If we haven't yet, convert the fragment itself. Note that fragments
|
||||
// aren't allowed to have cycles, so this won't recurse forever.
|
||||
var err error
|
||||
typ, err = g.convertNamedFragment(fragmentSpread.Definition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &goStructField{GoName: "" /* i.e. embedded */, GoType: typ}, nil
|
||||
}
|
||||
|
||||
// convertNamedFragment converts a single GraphQL named fragment-definition
|
||||
// (`fragment MyFragment on MyType { ... }`) into a Go struct.
|
||||
func (g *generator) convertNamedFragment(fragment *ast.FragmentDefinition) (goType, error) {
|
||||
typ := g.schema.Types[fragment.TypeCondition]
|
||||
if !g.Config.AllowBrokenFeatures &&
|
||||
(typ.Kind == ast.Interface || typ.Kind == ast.Union) {
|
||||
return nil, errorf(fragment.Position, "not implemented: abstract-typed fragments")
|
||||
}
|
||||
|
||||
description, directive, err := g.parsePrecedingComment(fragment, fragment.Position)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// If the user included a comment, use that. Else make up something
|
||||
// generic; there's not much to say though.
|
||||
if description == "" {
|
||||
description = fmt.Sprintf(
|
||||
"%v includes the GraphQL fields of %v requested by the fragment %v.",
|
||||
fragment.Name, fragment.TypeCondition, fragment.Name)
|
||||
}
|
||||
|
||||
fields, err := g.convertSelectionSet(
|
||||
newPrefixList(fragment.Name), fragment.SelectionSet, typ, directive)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
goType := &goStructType{
|
||||
GoName: fragment.Name,
|
||||
Description: description,
|
||||
GraphQLName: fragment.TypeCondition,
|
||||
Fields: fields,
|
||||
Incomplete: false,
|
||||
}
|
||||
g.typeMap[fragment.Name] = goType
|
||||
return goType, nil
|
||||
}
|
||||
|
||||
// convertField converts a single GraphQL operation-field into a Go
|
||||
// struct-field (and its type).
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user