Skip to content

Commit

Permalink
doc/tutorial: add title with file names to code blocks (ent#2430)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored and gitlawr committed Apr 13, 2022
1 parent 429100e commit 085f98a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions doc/md/tutorial-todo-gql-field-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ and for the node(s) API.
Before we go over the example, we change the `ent.Client` to run in debug mode in the `Todos` resolver and restart
our GraphQL server:

```diff
```diff title="todo.resolvers.go"
func (r *queryResolver) Todos(ctx context.Context, after *ent.Cursor, first *int, before *ent.Cursor, last *int, orderBy *ent.TodoOrder) (*ent.TodoConnection, error) {
- return r.client.Todo.Query().
+ return r.client.Debug().Todo.Query().
Expand Down Expand Up @@ -123,7 +123,7 @@ SELECT DISTINCT `todos`.`id`, `todos`.`text`, `todos`.`created_at`, `todos`.`sta
Let's see how Ent can automatically solve our problem. All we need to do is to add the following
`entql` annotations to our edges:

```diff
```diff title="ent/schema/todo.go"
func (Todo) Edges() []ent.Edge {
return []ent.Edge{
edge.To("parent", Todo.Type).
Expand All @@ -139,9 +139,9 @@ The `Bind()` annotation is now enabled by default, use `Unbind()` to opt-out.
:::

After adding these annotations, `entgql` will do the binding mentioned in the [section](#ent-solution) above. Additionally, it
will also generate edge-resolvers for the nodes under the `edge.go` file:
will also generate edge-resolvers for the nodes under the `gql_edge.go` file:

```go
```go title="ent/gql_edge.go"
func (t *Todo) Children(ctx context.Context) ([]*Todo, error) {
result, err := t.Edges.ChildrenOrErr()
if IsNotLoaded(err) {
Expand Down
12 changes: 6 additions & 6 deletions doc/md/tutorial-todo-gql-mutation-input.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The Ent framework accepts external templates that can extend or override the def
code generator. In the template below, we generate 2 input types (`CreateTodoInput` and `UpdateTodoInput`) for the
GraphQL mutations, and add additional methods on the different builders to accept these objects as an input type.

```gotemplate
```gotemplate title="ent/template/mutation_input.tmpl"
{{ range $n := $.Nodes }}
{{ $input := print "Create" $n.Name "Input" }}
// {{ $input }} represents a mutation input for creating {{ plural $n.Name | lower }}.
Expand Down Expand Up @@ -113,7 +113,7 @@ go generate ./...

You may have noticed that Ent generated a new file `ent/mutation_input.go` with the following content:

```go
```go title="ent/template/mutation_input.go"
// Code generated by entc, DO NOT EDIT.

package ent
Expand Down Expand Up @@ -160,7 +160,7 @@ func (i *UpdateTodoInput) Mutate(m *TodoMutation) {
The new generated Go types are the GraphQL mutation types. Let's define them manually in the GraphQL schema and `gqlgen`
will map them automatically.

```graphql
```graphql title="todo.graphql"
# Define an input type for the mutation below.
# https://graphql.org/learn/schema/#input-types
#
Expand Down Expand Up @@ -207,7 +207,7 @@ go generate ./...

The result is as follows:

```go
```go title="todo.resolvers.go"
func (r *mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
panic(fmt.Errorf("not implemented"))
}
Expand All @@ -225,7 +225,7 @@ func (r *mutationResolver) UpdateTodos(ctx context.Context, ids []int, input ent

The `Set<F>` calls in the `CreateTodo` resolver are replaced with one call named `SetInput`:

```diff
```diff title="todo.resolvers.go"
func (r *mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
return ent.FromContext(ctx).Todo.
Create().
Expand All @@ -240,7 +240,7 @@ func (r *mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoI

The rest of the resolvers (`UpdateTodo` and `UpdateTodos`) will be implemented as follows:

```diff
```diff title="todo.resolvers.go"
func (r *mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
return ent.FromContext(ctx).Todo.Create().SetInput(input).Save(ctx)
}
Expand Down
6 changes: 3 additions & 3 deletions doc/md/tutorial-todo-gql-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ go run ./cmd/todo/

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:

```diff
```diff title="todo.graphql"
+interface Node {
+ id: ID!
+}
Expand All @@ -64,7 +64,7 @@ type Query {

Then, we tell gqlgen that Ent provides this interface by editing the `gqlgen.yaml` file as follows:

```diff
```diff title="gqlgen.yml"
# This section declares type mapping between the GraphQL and Go type systems.
models:
# Defines the ID field as Go 'int'.
Expand All @@ -88,7 +88,7 @@ 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:

```go
```go title="todo.resolvers.go"
func (r *queryResolver) Node(ctx context.Context, id int) (ent.Noder, error) {
return r.client.Noder(ctx, id)
}
Expand Down
6 changes: 3 additions & 3 deletions doc/md/tutorial-todo-gql-paginate.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Ordering can be defined on any comparable field of ent by annotating it with `en
Note that the given `OrderField` name must match its enum value in GraphQL schema (see
[next section](#define-ordering-types-in-graphql-schema) below).

```go
```go title="ent/schema/todo.go"
func (Todo) Fields() []ent.Field {
return []ent.Field{
field.Text("text").
Expand Down Expand Up @@ -91,7 +91,7 @@ func (Todo) Fields() []ent.Field {
Next, we need to define the ordering types along with the [Relay Connection Types](https://relay.dev/graphql/connections.htm#sec-Connection-Types)
in the GraphQL schema:

```graphql
```graphql title="todo.graphql"
# Define a Relay Cursor type:
# https://relay.dev/graphql/connections.htm#sec-Cursor
scalar Cursor
Expand Down Expand Up @@ -161,7 +161,7 @@ go generate ./...

Head over to the `Todos` resolver and update it to pass `orderBy` argument to `.Paginate()` call:

```go
```go title="todo.resolvers.go"
func (r *queryResolver) Todos(ctx context.Context, after *ent.Cursor, first *int, before *ent.Cursor, last *int, orderBy *ent.TodoOrder) (*ent.TodoConnection, error) {
return r.client.Todo.Query().
Paginate(ctx, after, first, before, last,
Expand Down
4 changes: 2 additions & 2 deletions doc/md/tutorial-todo-gql-tx-mutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ we follow these steps:
1\. Edit the `cmd/todo/main.go` and add to the GraphQL server initialization the `entgql.Transactioner` handler as
follows:

```diff
```diff title="cmd/todo/main.go"
srv := handler.NewDefaultServer(todo.NewSchema(client))
+srv.Use(entgql.Transactioner{TxOpener: client})
```

2\. Then, in the GraphQL mutations, use the client from context as follows:
```diff
```diff title="todo.resolvers.go"
func (mutationResolver) CreateTodo(ctx context.Context, todo TodoInput) (*ent.Todo, error) {
+ client := ent.FromContext(ctx)
+ return client.Todo.
Expand Down
18 changes: 9 additions & 9 deletions doc/md/tutorial-todo-gql.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ go run ./cmd/todo/
This tutorial begins where the previous one ended (with a working Todo-list schema). We start by creating a simple GraphQL schema for our todo list, then install the [99designs/gqlgen](https://github.com/99designs/gqlgen)
package and configure it. Let's create a file named `todo.graphql` and paste the following:

```graphql
```graphql title="todo.graphql"
# Maps a Time GraphQL scalar to a Go time.Time struct.
scalar Time

Expand Down Expand Up @@ -80,7 +80,7 @@ go get github.com/99designs/gqlgen
The gqlgen package can be configured using a `gqlgen.yml` file that it automatically loads from the current directory.
Let's add this file. Follow the comments in this file to understand what each config directive means:

```yaml
```yaml title="gqlgen.yml"
# schema tells gqlgen where the GraphQL schema is located.
schema:
- todo.graphql
Expand Down Expand Up @@ -149,7 +149,7 @@ go get entgo.io/contrib/entgql

**2\.** Create a new Go file named `ent/entc.go`, and paste the following content:

```go
```go title="ent/entc.go"
// +build ignore

package main
Expand Down Expand Up @@ -178,7 +178,7 @@ func main() {

**3\.** Edit the `ent/generate.go` file to execute the `ent/entc.go` file:

```go
```go title="ent/generate.go"
package ent

//go:generate go run entc.go
Expand All @@ -190,7 +190,7 @@ Note that `ent/entc.go` is ignored using a build tag, and it's executed by the g
**4\.** In order to execute `gqlgen` through `go generate`, we create a new `generate.go` file (in the root
of the project) with the following:

```go
```go title="generate.go"
package todo

//go:generate go run github.com/99designs/gqlgen
Expand All @@ -205,7 +205,7 @@ go generate ./...
**5\.** `gqlgen` allows changing the generated `Resolver` and add additional dependencies to it. Let's add
the `ent.Client` as a dependency by pasting the following in `resolver.go`:

```go
```go title="resolver.go"
package todo

import (
Expand All @@ -229,7 +229,7 @@ func NewSchema(client *ent.Client) graphql.ExecutableSchema {

We create a new directory `cmd/todo` and a `main.go` file with the following code to create the GraphQL server:

```go
```go title="cmd/todo/main.go"
package main

import (
Expand Down Expand Up @@ -293,7 +293,7 @@ example repository.
If we try to query our todo list, we'll get an error as the resolver method is not yet implemented.
Let's implement the resolver by replacing the `Todos` implementation in the query resolver:

```diff
```diff title="todo.resolvers.go"
func (r *queryResolver) Todos(ctx context.Context) ([]*ent.Todo, error) {
- panic(fmt.Errorf("not implemented"))
+ return r.client.Todo.Query().All(ctx)
Expand All @@ -317,7 +317,7 @@ query AllTodos {
Same as before, if we try to create a todo item in GraphQL, we'll get an error as the resolver is not yet implemented.
Let's implement the resolver by changing the `CreateTodo` implementation in the mutation resolver:

```go
```go title="todo.resolvers.go"
func (r *mutationResolver) CreateTodo(ctx context.Context, todo TodoInput) (*ent.Todo, error) {
return r.client.Todo.Create().
SetText(todo.Text).
Expand Down

0 comments on commit 085f98a

Please sign in to comment.