unions are easy too, sorta

This commit is contained in:
Ben Kraft
2020-07-15 17:35:47 -07:00
parent d089ab1afb
commit 6f0e5fdf0d
6 changed files with 22 additions and 13 deletions
-1
View File
@@ -60,7 +60,6 @@ TODO(benkraft): Figure out how to get GitHub Actions to run the example -- it ne
## Major TODOs ## Major TODOs
Query structures to support: Query structures to support:
- unions
- fragments - fragments
Config options: Config options:
+6 -8
View File
@@ -1,12 +1,10 @@
type Response struct { type Response struct {
Root struct { Root struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
Children []content `json:"children"` Children []struct {
Id string `json:"id"`
Name string `json:"name"`
} `json:"children"`
} `json:"root"` } `json:"root"`
}
type content struct {
Id string `json:"id"`
Name string `json:"name"`
} }
+5
View File
@@ -0,0 +1,5 @@
{
randomLeaf {
__typename
}
}
+5
View File
@@ -0,0 +1,5 @@
type Response struct {
RandomLeaf struct {
Typename *string `json:"__typename"`
} `json:"randomLeaf"`
}
+3
View File
@@ -32,6 +32,8 @@ interface Content {
parent: Topic parent: Topic
} }
union LeafContent = Article | Video
type Article implements Content { type Article implements Content {
id: ID! id: ID!
name: String! name: String!
@@ -56,4 +58,5 @@ type Topic implements Content {
type Query { type Query {
user(query: UserQueryInput): User user(query: UserQueryInput): User
root: Topic! root: Topic!
randomLeaf: LeafContent!
} }
+3 -4
View File
@@ -200,9 +200,8 @@ func (builder *typeBuilder) writeType(typ *ast.Type, selectionSet []selection, i
} }
def := builder.schema.Types[typ.Name()] def := builder.schema.Types[typ.Name()]
// TODO: set inline = false for nested types
switch def.Kind { switch def.Kind {
case ast.Scalar, ast.Enum, ast.Union, ast.Interface: case ast.Scalar, ast.Enum:
inline = false inline = false
} }
@@ -222,7 +221,7 @@ func (builder *typeBuilder) writeType(typ *ast.Type, selectionSet []selection, i
func (builder *typeBuilder) writeTypedef(typedef *ast.Definition, selectionSet []selection) error { func (builder *typeBuilder) writeTypedef(typedef *ast.Definition, selectionSet []selection) error {
switch typedef.Kind { switch typedef.Kind {
case ast.Object, ast.InputObject, ast.Interface: case ast.Object, ast.InputObject, ast.Interface, ast.Union:
builder.WriteString("struct {\n") builder.WriteString("struct {\n")
for _, field := range selectionSet { for _, field := range selectionSet {
err := builder.writeField(field) err := builder.writeField(field)
@@ -244,7 +243,7 @@ func (builder *typeBuilder) writeTypedef(typedef *ast.Definition, selectionSet [
} }
builder.WriteString(")\n") builder.WriteString(")\n")
return nil return nil
case ast.Scalar, ast.Union: case ast.Scalar:
// TODO(benkraft): Handle custom scalars, unions, and interfaces. // TODO(benkraft): Handle custom scalars, unions, and interfaces.
return fmt.Errorf("not implemented: %v", typedef.Kind) return fmt.Errorf("not implemented: %v", typedef.Kind)
default: default: