Add support for interfaces, part 4: getter methods (#57)

## Summary:
Right now, if you make a query like `{ myInterface { field } }`, you
have to type-switch on all the possible implementations of `myInterface`
to get at `field`.  Now, we generate getter-methods (e.g. `GetField`),
to make that access easier.  Of course this only applies to shared
fields (which for now are the only ones, but once we support fragments
will no longer be).

This also includes a small change to the way we generate type-names for
interfaces: we no longer include the name of the concrete type in the
interface we propagate forward, so we generate
`MyInterfaceMyFieldMyType`, not `MyInterfaceMyImplMyFieldMyType`, in the
case where you have an interface `MyInterface` implemented by `MyImpl`
(and maybe other types) with field `myField: MyType`.  This is necessary
so the getter method returns a well-defined type, and also probably
convenient for calling code.  It will have to get a little bit more
complicated once we support fragments, where you could have two
implementing types with identically-named fields of different types, but
I think it'll be easiest to figure out how to deal with that when
implementing fragments.

While I was in the area, I added to the interface doc-comment a list of
the implementations.  (In GraphQL, we're guaranteed to know them all
assuming our schema is up to date.)

Issue: https://github.com/Khan/genqlient/issues/8

## Test plan:
make check


Author: benjaminjkraft

Reviewers: benjaminjkraft, dnerdy, aberkan, MiguelCastillo

Required Reviewers: 

Approved by: dnerdy

Checks:  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/57
This commit is contained in:
Ben Kraft
2021-08-25 12:02:18 -07:00
committed by GitHub
parent 65f9e90f2b
commit 8f9d1cf792
10 changed files with 750 additions and 289 deletions
+37 -2
View File
@@ -249,6 +249,7 @@ func (g *generator) convertDefinition(
GoName: goName,
GoType: fieldGoType,
JSONName: field.Name,
GraphQLName: field.Name,
Description: field.Description,
}
}
@@ -264,19 +265,52 @@ func (g *generator) convertDefinition(
GoName: name,
Description: def.Description,
GraphQLName: def.Name,
SharedFields: make([]*goStructField, 0, len(selectionSet)),
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 {
implName, implNamePrefix := g.typeName(namePrefix, implDef)
// Note for shared fields we propagate forward the interface's
// name-prefix: that is, the implementations will have fields with
// types like
// MyInterfaceMyFieldMyType
// not
// MyInterfaceMyImplMyFieldMyType
// ^^^^^^
// In particular, this means that the Go type of MyField will be
// the same across all the implementations; this is important so
// that we can write a method GetMyField() that returns it!
implName, _ := g.typeName(namePrefix, implDef)
// TODO(benkraft): In principle we should skip generating a Go
// field for __typename each of these impl-defs if you didn't
// request it (and it was automatically added by
// preprocessQueryDocument). But in practice it doesn't really
// hurt, and would be extra work to avoid, so we just leave it.
implTyp, err := g.convertDefinition(
implName, implNamePrefix, implDef, pos, selectionSet, queryOptions)
implName, namePrefix, implDef, pos, selectionSet, queryOptions)
if err != nil {
return nil, err
}
@@ -356,6 +390,7 @@ func (g *generator) convertField(
GoName: goName,
GoType: fieldGoType,
JSONName: field.Alias,
GraphQLName: field.Name,
Description: field.Definition.Description,
}, nil
}