Skip to content

Commit

Permalink
entgql: satisfies fragments on Node/Nodes queries
Browse files Browse the repository at this point in the history
This fixes a long time bug that was found now in the new fields selection optimization
  • Loading branch information
a8m committed Apr 23, 2023
1 parent 1bae543 commit 24731ec
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 24 deletions.
30 changes: 22 additions & 8 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.

70 changes: 70 additions & 0 deletions entgql/internal/todo/todo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2582,3 +2582,73 @@ func TestOrderByEdgeCount(t *testing.T) {
require.Equal(t, rsp.Categories.Edges[1].Node.TodosCount, 3)
})
}

func TestSatisfiesFragments(t *testing.T) {
ctx := context.Background()
ec := enttest.Open(
t, dialect.SQLite,
fmt.Sprintf("file:%s?mode=memory&cache=shared&_fk=1", t.Name()),
enttest.WithMigrateOptions(migrate.WithGlobalUniqueID(true)),
)
gqlc := client.New(handler.NewDefaultServer(gen.NewSchema(ec)))
cat := ec.Category.Create().SetText("cat").SetStatus(category.StatusEnabled).SaveX(ctx)
todos := ec.Todo.CreateBulk(
ec.Todo.Create().SetText("t1").SetStatus(todo.StatusPending).SetCategory(cat),
ec.Todo.Create().SetText("t2").SetStatus(todo.StatusInProgress).SetCategory(cat),
ec.Todo.Create().SetText("t3").SetStatus(todo.StatusCompleted).SetCategory(cat),
).SaveX(ctx)
var (
// language=GraphQL
query = `query CategoryTodo($id: ID!) {
category: node(id: $id) {
__typename
id
... on Category {
text
...CategoryTodos
}
}
}
fragment CategoryTodos on Category {
todos (orderBy: {field: TEXT}) {
edges {
node {
id
...TodoFields
}
}
}
}
fragment TodoFields on Todo {
id
text
createdAt
}
`
rsp struct {
Category struct {
TypeName string `json:"__typename"`
ID, Text string
Todos struct {
Edges []struct {
Node struct {
ID, Text, CreatedAt string
}
}
}
}
}
)
gqlc.MustPost(query, &rsp, client.Var("id", cat.ID))
require.Equal(t, strconv.Itoa(cat.ID), rsp.Category.ID)
require.Len(t, rsp.Category.Todos.Edges, 3)
for i := range todos {
require.Equal(t, strconv.Itoa(todos[i].ID), rsp.Category.Todos.Edges[i].Node.ID)
require.Equal(t, todos[i].Text, rsp.Category.Todos.Edges[i].Node.Text)
ts, err := todos[i].CreatedAt.MarshalText()
require.NoError(t, err)
require.Equal(t, string(ts), rsp.Category.Todos.Edges[i].Node.CreatedAt)
}
}
14 changes: 14 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.

24 changes: 19 additions & 5 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.

24 changes: 19 additions & 5 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.

24 changes: 19 additions & 5 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.

Loading

0 comments on commit 24731ec

Please sign in to comment.