docs: various fixes to the graphql tutorial (#2965)

* doc/md: fix resolver diff and isolation example

* doc/md: fix updateTodo input field

* doc/md: add missing extension and fix paths

Without entgql.WithSchemaGenerator entc.go fails to run.

* doc/md: add file titles

Note that the where:TodoWhereInput parameter is pre-filled in the
generated ent.graphql file.

* doc/md: add context to conjunction/negation example

Only add for the first example to provide a lead to readers.
This commit is contained in:
Dan Kortschak
2022-09-27 16:32:16 +09:30
committed by GitHub
parent bd80e069b9
commit e02622a064
3 changed files with 44 additions and 35 deletions

View File

@@ -37,16 +37,12 @@ srv := handler.NewDefaultServer(todo.NewSchema(client))
2\. Then, in the GraphQL mutations, use the client from context as follows:
```diff title="todo.resolvers.go"
func (mutationResolver) CreateTodo(ctx context.Context, todo TodoInput) (*ent.Todo, error) {
}
+func (mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
+ client := ent.FromContext(ctx)
+ return client.Todo.
- return r.client.Todo.
Create().
SetText(todo.Text).
SetStatus(todo.Status).
SetNillablePriority(todo.Priority). // Set the "priority" field if provided.
SetNillableParentID(todo.Parent). // Set the "parent_id" field if provided.
Save(ctx)
+ return client.Todo.Create().SetInput(input).Save(ctx)
-func (r *mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
- return r.client.Todo.Create().SetInput(input).Save(ctx)
}
```
@@ -54,17 +50,17 @@ func (mutationResolver) CreateTodo(ctx context.Context, todo TodoInput) (*ent.To
If you'd like to tweak the transaction's isolation level, you can do so by implementing your own `TxOpener`. For example:
```go
```go title="cmd/todo/main.go"
srv.Use(entgql.Transactioner{
TxOpener: entgql.TxOpenerFunc(func(ctx context.Context) (context.Context, driver.Tx, error) {
tx, err := client.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})
if err != nil {
return nil, nil, err
}
ctx = NewTxContext(ctx, tx)
ctx = NewContext(ctx, tx.Client())
ctx = ent.NewTxContext(ctx, tx)
ctx = ent.NewContext(ctx, tx.Client())
return ctx, tx, nil
})
}),
})
```