doc/tutorial: update entgql + gqlgen integration (#2915)

This commit is contained in:
Ariel Mashraki
2022-09-08 17:56:11 +03:00
committed by GitHub
parent d0943109b5
commit 82ee525676
6 changed files with 625 additions and 834 deletions

View File

@@ -27,7 +27,7 @@ Node interface, read the following paragraphs that were taken from [relay.dev](h
The code for this tutorial is available under [github.com/a8m/ent-graphql-example](https://github.com/a8m/ent-graphql-example),
and tagged (using Git) in each step. If you want to skip the basic setup and start with the initial version of the GraphQL
server, you can clone the repository and checkout `v0.1.0` as follows:
server, you can clone the repository as follows:
```console
git clone git@github.com:a8m/ent-graphql-example.git
@@ -37,64 +37,43 @@ go run ./cmd/todo/
## Implementation
Ent supports the Node interface through its GraphQL integration. By following a few simple steps you can add support for it in your application. We start by adding the `Node` interface to our GraphQL schema:
Ent supports the Node interface through its GraphQL integration. By following a few simple steps you can add support
for it in your application. We start by telling `gqlgen` that Ent provides the `Node` interface by editing the
`gqlgen.yaml` file as follows:
```diff title="todo.graphql"
+interface Node {
+ id: ID!
+}
-type Todo {
+type Todo implements Node {
id: ID!
createdAt: Time
status: Status!
priority: Int!
text: String!
parent: Todo
children: [Todo!]
}
type Query {
todos: [Todo!]
+ node(id: ID!): Node
+ nodes(ids: [ID!]!): [Node]!
}
```
Then, we tell gqlgen that Ent provides this interface by editing the `gqlgen.yaml` file as follows:
```diff title="gqlgen.yml"
```diff title="gqlgen.yml" {7-9}
# This section declares type mapping between the GraphQL and Go type systems.
models:
# Defines the ID field as Go 'int'.
ID:
model:
- github.com/99designs/gqlgen/graphql.IntID
+ Node:
+ model:
+ - todo/ent.Noder
Node:
model:
- todo/ent.Noder
Status:
model:
- todo/ent/todo.Status
```
To apply these changes, we must rerun the `gqlgen` code-gen. Let's do that by running:
To apply these changes, we rerun the code generation:
```console
go generate ./...
go generate .
```
Like before, we need to implement the GraphQL resolve in the `todo.resolvers.go` file, but that's simple.
Let's replace the default resolvers with the following:
Like before, we need to implement the GraphQL resolvers in `ent.resolvers.go`. With a one-liner change, we can
implement those by replacing the generated `gqlgen` code with the following:
```go title="todo.resolvers.go"
```diff title="ent.resolvers.go"
func (r *queryResolver) Node(ctx context.Context, id int) (ent.Noder, error) {
return r.client.Noder(ctx, id)
- panic(fmt.Errorf("not implemented: Node - node"))
+ return r.client.Noder(ctx, id)
}
func (r *queryResolver) Nodes(ctx context.Context, ids []int) ([]ent.Noder, error) {
return r.client.Noders(ctx, ids)
- panic(fmt.Errorf("not implemented: Nodes - nodes"))
+ return r.client.Noders(ctx, ids)
}
```
@@ -120,7 +99,7 @@ mutation CreateTodo($todo: TodoInput!) {
# Output: { "data": { "createTodo": { "id": "2", "text": "Create GraphQL Example", "createdAt": "2021-03-10T15:02:18+02:00", "priority": 1, "parent": null } } }
```
Running the **Nodes** API on one of the todo items will return:
Running the **Node** API on one of the todo items will return:
````graphql
query {
@@ -153,5 +132,5 @@ query {
---
Well done! As you can see, by changing a few lines of code our application now implements the Relay Node Interface.
In the next section, we will show how to implement the Relay Cursor Connections spec using Ent which is very useful
In the next section, we will show how to implement the Relay Cursor Connections spec using Ent, which is very useful
if we want our application to support slicing and pagination of query results.