Mutation Inputs

In this section, we continue the GraphQL example by explaining how to extend the Ent code generator using Go templates and generate input type objects for our GraphQL mutations that can be applied directly on Ent mutations.

Clone the code (optional)

The code for this tutorial is available under 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 run the program as follows:

  1. git clone git@github.com:a8m/ent-graphql-example.git
  2. cd ent-graphql-example
  3. go run ./cmd/todo/

Mutation Types

Ent supports generating mutation types. A mutation type can be accepted as an input for GraphQL mutations, and it is handled and verified by Ent. Let’s tell Ent that our GraphQL Todo type supports create and update operations:

ent/schema/todo.go

  1. func (Todo) Annotations() []schema.Annotation {
  2. return []schema.Annotation{
  3. entgql.QueryField(),
  4. entgql.Mutations(entgql.MutationCreate(), entgql.MutationUpdate()),
  5. }
  6. }

Then, run code generation:

  1. go generate .

You’ll notice that Ent generated for you 2 types: ent.CreateTodoInput and ent.UpdateTodoInput.

Mutations

After generating our mutation inputs, we can connect them to the GraphQL mutations:

todo.graphql

  1. type Mutation {
  2. createTodo(input: CreateTodoInput!): Todo!
  3. updateTodo(id: ID!, input: UpdateTodoInput!): Todo!
  4. }

Running code generation we’ll generate the actual mutations and the only thing left after that is to bind the resolvers to Ent.

  1. go generate .

todo.resolvers.go

  1. // CreateTodo is the resolver for the createTodo field.
  2. func (r *mutationResolver) CreateTodo(ctx context.Context, input ent.CreateTodoInput) (*ent.Todo, error) {
  3. return r.client.Todo.Create().SetInput(input).Save(ctx)
  4. }
  5. // UpdateTodo is the resolver for the updateTodo field.
  6. func (r *mutationResolver) UpdateTodo(ctx context.Context, id int, input ent.UpdateTodoInput) (*ent.Todo, error) {
  7. return r.client.Todo.UpdateOneID(id).SetInput(input).Save(ctx)
  8. }

Test the CreateTodo Resolver

Let’s start with creating 2 todo items by executing the createTodo mutations twice.

Mutation

  1. mutation CreateTodo {
  2. createTodo(input: {text: "Create GraphQL Example", status: IN_PROGRESS, priority: 2}) {
  3. id
  4. text
  5. createdAt
  6. priority
  7. parent {
  8. id
  9. }
  10. }
  11. }

Output

  1. {
  2. "data": {
  3. "createTodo": {
  4. "id": "1",
  5. "text": "Create GraphQL Example",
  6. "createdAt": "2021-04-19T10:49:52+03:00",
  7. "priority": 2,
  8. "parent": null
  9. }
  10. }
  11. }

Mutation

  1. mutation CreateTodo {
  2. createTodo(input: {text: "Create Tracing Example", status: IN_PROGRESS, priority: 2}) {
  3. id
  4. text
  5. createdAt
  6. priority
  7. parent {
  8. id
  9. }
  10. }
  11. }

Output

  1. {
  2. "data": {
  3. "createTodo": {
  4. "id": "2",
  5. "text": "Create Tracing Example",
  6. "createdAt": "2021-04-19T10:50:01+03:00",
  7. "priority": 2,
  8. "parent": null
  9. }
  10. }
  11. }

Test the UpdateTodo Resolver

The only thing left is to test the UpdateTodo resolver. Let’s use it to update the parent of the 2nd todo item to 1.

  1. mutation UpdateTodo {
  2. updateTodo(id: 2, input: {parentID: 1}) {
  3. id
  4. text
  5. createdAt
  6. priority
  7. parent {
  8. id
  9. text
  10. }
  11. }
  12. }

Output

  1. {
  2. "data": {
  3. "updateTodo": {
  4. "id": "2",
  5. "text": "Create Tracing Example",
  6. "createdAt": "2021-04-19T10:50:01+03:00",
  7. "priority": 1,
  8. "parent": {
  9. "id": "1",
  10. "text": "Create GraphQL Example"
  11. }
  12. }
  13. }
  14. }