Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for interfaces, part 1: the simplest cases #52

Merged
merged 2 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions generate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ type Config struct {
// and UnmarshalJSON methods, or otherwise be convertible to JSON.
Scalars map[string]string `yaml:"scalars"`

// Set to true to use features that aren't fully ready to use.
//
// This is primarily intended for genqlient's own tests. These features
// are likely BROKEN and come with NO EXPECTATION OF COMPATIBBILITY. Use
// them at your own risk!
AllowBrokenFeatures bool `yaml:"allow_broken_features"`
benjaminjkraft marked this conversation as resolved.
Show resolved Hide resolved

// Set automatically to the filename of the config file itself.
configFilename string
}
Expand Down
17 changes: 15 additions & 2 deletions generate/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (g *generator) baseTypeForOperation(operation ast.Operation) (*ast.Definiti
case ast.Mutation:
return g.schema.Mutation, nil
case ast.Subscription:
if !allowBrokenFeatures {
if !g.Config.AllowBrokenFeatures {
return nil, errorf(nil, "genqlient does not yet support subscriptions")
}
return g.schema.Subscription, nil
Expand Down Expand Up @@ -255,10 +255,23 @@ func (g *generator) convertDefinition(
return goType, nil

case ast.Interface, ast.Union:
if !allowBrokenFeatures {
if !g.Config.AllowBrokenFeatures {
return nil, errorf(pos, "not implemented: %v", def.Kind)
}

// We need to request __typename so we know which concrete type to use.
hasTypename := false
for _, selection := range selectionSet {
field, ok := selection.(*ast.Field)
if ok && field.Name == "__typename" {
hasTypename = true
}
}
if !hasTypename {
// TODO(benkraft): Instead, modify the query to add __typename.
return nil, errorf(pos, "union/interface type %s must request __typename", def.Name)
}

implementationTypes := g.schema.GetPossibleTypes(def)
goType := &goInterfaceType{
GoName: name,
Expand Down
5 changes: 1 addition & 4 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ import (
"golang.org/x/tools/imports"
)

// Set to true to test features that aren't yet really ready.
var allowBrokenFeatures = false

// generator is the context for the codegen process (and ends up getting passed
// to the template).
type generator struct {
Expand Down Expand Up @@ -230,7 +227,7 @@ func Generate(config *Config) (map[string][]byte, error) {
strings.Join(config.Operations, ", "))
}

if len(document.Fragments) > 0 && !allowBrokenFeatures {
if len(document.Fragments) > 0 && !config.AllowBrokenFeatures {
return nil, errorf(document.Fragments[0].Position,
"genqlient does not yet support fragments")
}
Expand Down
17 changes: 8 additions & 9 deletions generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ const (
// update the snapshots. Make sure to check that the output is sensible; the
// snapshots don't even get compiled!
func TestGenerate(t *testing.T) {
// we can test parts of features even if they're not done yet!
allowBrokenFeatures = true

files, err := ioutil.ReadDir(dataDir)
if err != nil {
t.Fatal(err)
Expand All @@ -59,6 +56,7 @@ func TestGenerate(t *testing.T) {
"Junk": "interface{}",
"ComplexJunk": "[]map[string]*[]*map[string]interface{}",
},
AllowBrokenFeatures: true,
})
if err != nil {
t.Fatal(err)
Expand All @@ -74,9 +72,12 @@ func TestGenerate(t *testing.T) {
if testing.Short() {
t.Skip("skipping build due to -short")
} else if sourceFilename == "InterfaceNesting.graphql" ||
sourceFilename == "InterfaceNoFragments.graphql" ||
sourceFilename == "Omitempty.graphql" {
t.Skip("TODO: enable these once they build")
sourceFilename == "InterfaceListField.graphql" {
t.Skip("TODO: enable after fixing " +
"https://github.com/Khan/genqlient/issues/8")
} else if sourceFilename == "Omitempty.graphql" {
t.Skip("TODO: enable after fixing " +
"https://github.com/Khan/genqlient/issues/43")
}

goContent := generated[goFilename]
Expand Down Expand Up @@ -109,9 +110,6 @@ func TestGenerate(t *testing.T) {
}

func TestGenerateErrors(t *testing.T) {
// we can test parts of features even if they're not done yet!
allowBrokenFeatures = true

files, err := ioutil.ReadDir(errorsDir)
if err != nil {
t.Fatal(err)
Expand All @@ -138,6 +136,7 @@ func TestGenerateErrors(t *testing.T) {
"ValidScalar": "string",
"InvalidScalar": "bogus",
},
AllowBrokenFeatures: true,
})
if err == nil {
t.Fatal("expected an error")
Expand Down
5 changes: 5 additions & 0 deletions generate/testdata/errors/MissingTypeName.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package errors

const _ = `# @genqlient
query MyQuery { i { f } }
`
1 change: 1 addition & 0 deletions generate/testdata/errors/MissingTypeName.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
query MyQuery { i { f } }
11 changes: 11 additions & 0 deletions generate/testdata/errors/MissingTypeName.schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type Query {
i: I
}

type T implements I {
f: String!
}

interface I {
f: String!
}
11 changes: 11 additions & 0 deletions generate/testdata/queries/InterfaceListField.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query InterfaceNoFragmentsQuery {
root {
id
name
children {
__typename
id
name
}
}
}
3 changes: 3 additions & 0 deletions generate/testdata/queries/InterfaceNesting.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ query InterfaceNesting {
root {
id
children {
__typename
id
parent {
__typename
id
children {
__typename
id
}
}
Expand Down
10 changes: 2 additions & 8 deletions generate/testdata/queries/InterfaceNoFragments.graphql
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
query InterfaceNoFragmentsQuery {
root {
id
name
children {
id
name
}
}
root { id name } # (make sure sibling fields work)
randomItem { __typename id name }
}
1 change: 1 addition & 0 deletions generate/testdata/queries/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Query {
"""usersWithRole looks a user up by role."""
usersWithRole(role: Role!): [User!]!
root: Topic!
randomItem: Content!
randomLeaf: LeafContent!
convert(dt: DateTime!, tz: String): DateTime!
maybeConvert(dt: DateTime, tz: String): DateTime
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"operations": [
{
"operationName": "InterfaceNoFragmentsQuery",
"query": "\nquery InterfaceNoFragmentsQuery {\n\troot {\n\t\tid\n\t\tname\n\t\tchildren {\n\t\t\t__typename\n\t\t\tid\n\t\t\tname\n\t\t}\n\t}\n}\n",
"sourceLocation": "testdata/queries/InterfaceListField.graphql"
}
]
}
Loading