Skip to content

Commit

Permalink
entgql: ignore the __typename meta-field on fields selection
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m committed Apr 22, 2023
1 parent 1a350e8 commit 1bae543
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 0 deletions.
7 changes: 7 additions & 0 deletions entgql/internal/todo/ent/gql_collection.go

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

65 changes: 65 additions & 0 deletions entgql/internal/todo/todo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2275,6 +2275,71 @@ func TestFieldSelection(t *testing.T) {
"SELECT `categories`.`id`, `categories`.`text` FROM `categories` WHERE `categories`.`id` IN (?, ?, ?, ?)",
}, rec.queries)

var (
// language=GraphQL
query3 = `query {
todos {
edges {
node {
__typename
text
}
}
}
}`
rsp3 struct {
Todos struct {
Edges []struct {
Node struct {
TypeName string `json:"__typename"`
Text string
}
}
}
}
)
rec.reset()
client.New(handler.NewDefaultServer(gen.NewSchema(ec))).
MustPost(query3, &rsp3)
require.Equal(t, []string{
// Ignore the __typename meta field.
"SELECT `todos`.`id`, `todos`.`text` FROM `todos` ORDER BY `todos`.`id`",
}, rec.queries)

var (
// language=GraphQL
query4 = `query {
todos {
edges {
node {
text
extendedField
}
}
}
}`
rsp4 struct {
Todos struct {
Edges []struct {
Node struct {
Text string
ExtendedField string
}
}
}
}
)
rec.reset()
client.New(handler.NewDefaultServer(gen.NewSchema(ec))).
MustPost(query4, &rsp4)
require.Equal(t, []string{
// Unknown fields enforce query all columns.
"SELECT `todos`.`id`, `todos`.`created_at`, `todos`.`status`, " +
"`todos`.`priority`, `todos`.`text`, `todos`.`blob`, " +
"`todos`.`category_id`, `todos`.`init`, `todos`.`custom`, " +
"`todos`.`customp` FROM `todos` ORDER BY `todos`.`id`",
}, rec.queries)

rootO2M := ec.OneToMany.CreateBulk(
ec.OneToMany.Create().SetName("t0.1"),
ec.OneToMany.Create().SetName("t0.2"),
Expand Down
2 changes: 2 additions & 0 deletions entgql/internal/todofed/ent/gql_collection.go

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

7 changes: 7 additions & 0 deletions entgql/internal/todogotype/ent/gql_collection.go

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

6 changes: 6 additions & 0 deletions entgql/internal/todopulid/ent/gql_collection.go

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

6 changes: 6 additions & 0 deletions entgql/internal/todouuid/ent/gql_collection.go

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

1 change: 1 addition & 0 deletions entgql/template/collection.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func ({{ $receiver }} *{{ $query }}) collectField(ctx context.Context, opCtx *gr
case {{ range $i, $m := . }}{{ if $i }}, {{ end }}"{{ $m }}"{{ end }}:
{{- end }}
{{- end -}}
case "__typename":
default:
unknownSeen = true
{{- end }}
Expand Down

0 comments on commit 1bae543

Please sign in to comment.