Skip to content

Commit

Permalink
Add support for inline fragments
Browse files Browse the repository at this point in the history
In this commit I add support for inline fragments
(`... on MyType { fields }`) to genqlient.  This will make interfaces a
lot more useful!  In future commits I'll add named fragments, for which
we'll generate slightly different types, as discussed in DESIGN.md.

In general, implementing the flattening approach described in DESIGN.md
was... surprisingly easy.  All we have to do is recurse on applicable
fragments when generating our selection-set.  The refactor to
selection-set handling this encouraged was, I think, quite beneficial.
It did reveal two tricky pre-existing issues.

One issue is that GraphQL allows for duplicate selections, as long as
they match.  (In practice, this is only useful in the context of
fragments, although GraphQL allows it even without.) I decided to handle
the simple case (duplicate leaf fields; we just deduplicate) but leave
to the future the complex cases where we need to merge different
sub-selections (now #64).  For now we just forbid that; we can see how
much it comes up.

The other issue is that we are generating type-names incorrectly for
interface types; I had intended to do `MyInterfaceMyFieldMyType` for
shared fields and `MyImplMyFieldMyType` for non-shared ones, but instead
I did `MyFieldMyType`, which is inconsistent already and can result in
conflicts in the presence of fragments.  I'm going to fix this in a
separate commit, though, because it's going to require some refactoring
and is irrelevant to the main logic of this commit; I left some TODOs in
the tests related to this.

Issue: #8

Test plan:
make check

Reviewers: marksandstrom, adam, miguel
  • Loading branch information
benjaminjkraft committed Aug 28, 2021
1 parent e99dced commit ae5c33d
Show file tree
Hide file tree
Showing 19 changed files with 2,267 additions and 66 deletions.
211 changes: 162 additions & 49 deletions generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,38 +200,20 @@ func (g *generator) convertDefinition(

switch def.Kind {
case ast.Object:
fields, err := g.convertSelectionSet(
namePrefix, selectionSet, def, queryOptions)
if err != nil {
return nil, err
}

goType := &goStructType{
GoName: name,
Description: def.Description,
GraphQLName: def.Name,
Fields: make([]*goStructField, len(selectionSet)),
Fields: fields,
Incomplete: true,
}
g.typeMap[name] = goType

for i, selection := range selectionSet {
_, selectionDirective, err := g.parsePrecedingComment(
selection, selection.GetPosition())
if err != nil {
return nil, err
}
selectionOptions := queryOptions.merge(selectionDirective)

switch selection := selection.(type) {
case *ast.Field:
goType.Fields[i], err = g.convertField(
namePrefix, selection, selectionOptions, queryOptions)
if err != nil {
return nil, err
}
case *ast.FragmentSpread:
return nil, errorf(selection.Position, "not implemented: %T", selection)
case *ast.InlineFragment:
return nil, errorf(selection.Position, "not implemented: %T", selection)
default:
return nil, errorf(nil, "invalid selection type: %T", selection)
}
}
return goType, nil

case ast.InputObject:
Expand Down Expand Up @@ -270,38 +252,22 @@ func (g *generator) convertDefinition(
return goType, nil

case ast.Interface, ast.Union:
sharedFields, err := g.convertSelectionSet(
namePrefix, selectionSet, def, queryOptions)
if err != nil {
return nil, err
}

implementationTypes := g.schema.GetPossibleTypes(def)
goType := &goInterfaceType{
GoName: name,
Description: def.Description,
GraphQLName: def.Name,
SharedFields: make([]*goStructField, 0, len(selectionSet)),
SharedFields: sharedFields,
Implementations: make([]*goStructType, len(implementationTypes)),
}
g.typeMap[name] = goType

// TODO(benkraft): This sorta-duplicates what we'll do in each
// implementation when it traverses the fields. But they'll differ
// more once we support fragments; at that point we should figure out
// how to refactor.
for _, selection := range selectionSet {
field, ok := selection.(*ast.Field)
if !ok { // fragment/interface, not a shared field
continue
}
_, fieldDirective, err := g.parsePrecedingComment(field, field.GetPosition())
if err != nil {
return nil, err
}
fieldOptions := queryOptions.merge(fieldDirective)

goField, err := g.convertField(namePrefix, field, fieldOptions, queryOptions)
if err != nil {
return nil, err
}
goType.SharedFields = append(goType.SharedFields, goField)
}

for i, implDef := range implementationTypes {
// Note for shared fields we propagate forward the interface's
// name-prefix: that is, the implementations will have fields with
Expand Down Expand Up @@ -357,7 +323,154 @@ func (g *generator) convertDefinition(
}
}

// convertField converts a single GraphQL operation-field into a GraphQL type.
// convertSelectionSet converts a GraphQL selection-set into a list of
// corresponding Go struct-fields (and their Go types)
//
// A selection-set is a list of fields within braces like `{ myField }`, as
// appears at the toplevel of a query, in a field's sub-selections, or within
// an inline or named fragment.
//
// containingTypedef is the type-def whose fields we are selecting, and may be
// an object type or an interface type. In the case of interfaces, we'll call
// convertSelectionSet once for the interface, and once for each
// implementation.
func (g *generator) convertSelectionSet(
namePrefix string,
selectionSet ast.SelectionSet,
containingTypedef *ast.Definition,
queryOptions *GenqlientDirective,
) ([]*goStructField, error) {
fields := make([]*goStructField, 0, len(selectionSet))
for _, selection := range selectionSet {
_, selectionDirective, err := g.parsePrecedingComment(
selection, selection.GetPosition())
if err != nil {
return nil, err
}
selectionOptions := queryOptions.merge(selectionDirective)

switch selection := selection.(type) {
case *ast.Field:
field, err := g.convertField(
namePrefix, selection, selectionOptions, queryOptions)
if err != nil {
return nil, err
}
fields = append(fields, field)
case *ast.FragmentSpread:
return nil, errorf(selection.Position, "not implemented: %T", selection)
case *ast.InlineFragment:
fragmentFields, err := g.convertInlineFragment(
namePrefix, selection, containingTypedef, queryOptions)
if err != nil {
return nil, err
}
fields = append(fields, fragmentFields...)
default:
return nil, errorf(nil, "invalid selection type: %T", selection)
}
}

// We need to deduplicate, if you asked for
// { id, id, id, ... on SubType { id } }
// (which, yes, is legal) we'll treat that as just { id }.
uniqFields := make([]*goStructField, 0, len(selectionSet))
fieldNames := make(map[string]bool, len(selectionSet))
for _, field := range fields {
// 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] {
// 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
// with different selections (subject to some additional rules).
// We say: that's too complicated! and allow duplicate fields
// only if they're "leaf" types (enum or scalar).
switch field.GoType.Unwrap().(type) {
case *goOpaqueType, *goEnumType:
// Leaf field; we can just deduplicate.
// Note GraphQL already guarantees that the conflicting field
// has scalar/enum type iff this field does:
// https://spec.graphql.org/draft/#SameResponseShape()
continue
case *goStructType, *goInterfaceType:
// TODO(benkraft): Keep track of the position of each
// selection, so we can put this error on the right line.
return nil, errorf(nil,
"genqlient doesn't allow duplicate fields with different selections "+
"(see https://github.com/Khan/genqlient/issues/64); "+
"duplicate field: %s.%s", containingTypedef.Name, field.JSONName)
default:
return nil, errorf(nil, "unexpected field-type: %T", field.GoType.Unwrap())
}
}
uniqFields = append(uniqFields, field)
fieldNames[field.JSONName] = true
}
return uniqFields, nil
}

// fragmentMatches returns true if the given fragment is "active" when applied
// to the given type.
//
// "Active" here means "the fragment's fields will be returned on all objects
// of the given type", which is true when the given type is or implements
// the fragment's type. This is distinct from the rules for when a fragment
// spread is legal, which is true when the fragment would be active for *any*
// of the concrete types the spread-context could have (see
// https://spec.graphql.org/draft/#sec-Fragment-Spreads or DESIGN.md).
//
// containingTypedef is as described in convertInlineFragment, below.
// fragmentTypedef is the definition of the fragment's type-condition, i.e. the
// definition of MyType in a fragment `on MyType`.
func fragmentMatches(containingTypedef, fragmentTypedef *ast.Definition) bool {
if containingTypedef.Name == fragmentTypedef.Name {
return true
}
for _, iface := range containingTypedef.Interfaces {
// Note we don't need to recurse into the interfaces here, because in
// GraphQL types must list all the interfaces they implement, including
// all types those interfaces implement [1]. Actually, at present
// gqlparser doesn't even support interfaces implementing other
// interfaces, but our code would handle that too.
// [1] https://spec.graphql.org/draft/#sec-Interfaces.Interfaces-Implementing-Interfaces
if iface == fragmentTypedef.Name {
return true
}
}
return false
}

// convertInlineFragment converts a single GraphQL inline fragment
// (`... on MyType { myField }`) into Go struct-fields.
//
// containingTypedef is the type-def corresponding to the type into which we
// are spreading; it may be either an interface type (when spreading into one)
// or an object type (when writing the implementations of such an interface, or
// when using an inline fragment in an object type which is rare).
//
// In general, we treat such fragments' fields as if they were fields of the
// parent selection-set (except of course they are only included in types the
// fragment matches); see DESIGN.md for more.
func (g *generator) convertInlineFragment(
namePrefix string,
fragment *ast.InlineFragment,
containingTypedef *ast.Definition,
queryOptions *GenqlientDirective,
) ([]*goStructField, error) {
// You might think fragmentTypedef would be fragment.ObjectDefinition, but
// actually that's the type into which the fragment is spread.
fragmentTypedef := g.schema.Types[fragment.TypeCondition]
if !fragmentMatches(containingTypedef, fragmentTypedef) {
return nil, nil
}
return g.convertSelectionSet(namePrefix, fragment.SelectionSet,
containingTypedef, queryOptions)
}

// convertField converts a single GraphQL operation-field into a Go
// struct-field (and its type).
//
// Note that input-type fields are handled separately (inline in
// convertDefinition), because they come from the type-definition, not the
Expand Down
12 changes: 12 additions & 0 deletions generate/testdata/errors/ConflictingSelections.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package errors

const _ = `# @genqlient
query {
myField {
subField { subSubField1 subSubField2 }
... on OnePossibleConcreteType {
subField { subSubField3 subSubField4 }
}
}
}
`
8 changes: 8 additions & 0 deletions generate/testdata/errors/ConflictingSelections.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query {
myField {
subField { subSubField1 subSubField2 }
... on OnePossibleConcreteType {
subField { subSubField3 subSubField4 }
}
}
}
18 changes: 18 additions & 0 deletions generate/testdata/errors/ConflictingSelections.schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type Query {
myField: InterfaceType
}

interface InterfaceType {
subField: SubFieldType
}

type OnePossibleConcreteType implements InterfaceType {
subField: SubFieldType
}

type SubFieldType {
subSubField1: String!
subSubField2: String!
subSubField3: String!
subSubField4: String!
}
69 changes: 69 additions & 0 deletions generate/testdata/queries/ComplexInlineFragments.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# We test all the spread cases from DESIGN.md, see there for more context on
# each, as well as various other nonsense. But for abstract-in-abstract
# spreads, we can't test cases (4b) and (4c), where I implements J or vice
# versa, because gqlparser doesn't support interfaces that implement other
# interfaces yet.
query ComplexInlineFragments {
root {
id
... on Topic { schoolGrade } # (1) object spread in object scope
... on Content { name } # (3) abstract spread in object scope
}
randomItem {
id
... on Article { text } # (2) object spread in abstract scope
... on Content { name } # (4a) abstract spread in abstract scope, I == J
... on HasDuration { duration } # (4d) abstract spread in abstract scope, neither implements the other
}
repeatedStuff: randomItem {
id
id
url
otherId: id
... on Article {
name
text
otherName: name
}
... on Content {
id
name
otherName: name
}
... on HasDuration { duration }
}
conflictingStuff: randomItem {
# These two have different types! Naming gets complicated. Note GraphQL
# says [1] that you can only have such naming conflicts when the fields are
# both on object-typed spreads (reasonable, so they can never collide) and
# they are of "shapes that can be merged", e.g. both nullable objects,
# which seems very strange to me but is the most interesting case for us
# anyway (since where we could have trouble is naming the result types).
# [1] https://spec.graphql.org/draft/#SameResponseShape()
# TODO(benkraft): This actually generates the wrong thing right now (the
# two thumbnail types get the same name, and one clobbers the other). Fix
# in a follow-up commit.
... on Article { thumbnail { id thumbnailUrl } }
... on Video { thumbnail { id timestampSec } }
}
nestedStuff: randomItem {
... on Topic {
children {
id
... on Article {
text
parent {
... on Content {
name
parent {
... on Topic {
children { id name }
}
}
}
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions generate/testdata/queries/SimpleInlineFragment.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
query SimpleInlineFragment {
randomItem {
id
name
... on Article { text }
... on Video { duration }
}
}
Loading

0 comments on commit ae5c33d

Please sign in to comment.