Faced an error that resolve doesn’t work in GraphQL-Ruby.

Tomoharu Tsutsumi
2 min readApr 29, 2024

--

Recently started learning GraphQL because I’ll use it in a company. Roughly, I understood but I spent 3 hours solving a problem. The environment is that the backend is Ruby on Rails and the frontend is Next js.

The problem

It was that ‘resolve’ in mutation doesn’t work. Even if I sent a request to the server, the log became like this.

Started POST "/graphql" for ::1 at 2024-04-28 20:51:34 -0700
Processing by GraphqlController#execute as */*
Parameters: {"operationName"=>"createTodo", "variables"=>{"title"=>"fdsds", "completed"=>true}, "query"=>"mutation createTodo($title: String!, $completed: Boolean!) {\n createTodo(title: $title, completed: $completed) {\n todo {\n id\n title\n completed\n __typename\n }\n errors\n __typename\n }\n}", "graphql"=>{"operationName"=>"createTodo", "variables"=>{"title"=>"fdsds", "completed"=>true}, "query"=>"mutation createTodo($title: String!, $completed: Boolean!) {\n createTodo(title: $title, completed: $completed) {\n todo {\n id\n title\n completed\n __typename\n }\n errors\n __typename\n }\n}"}}
Completed 200 OK in 7ms (Views: 1.2ms | ActiveRecord: 0.0ms | Allocations: 1281)

I thought that the error was related to Ruby on Rails, but it showed up on the console of the client, which meant that the cause was the frontend.

The error I got

new.tsx:28 ApolloError: Field 'createTodo' is missing required arguments: input
Field 'createTodo' doesn't accept argument 'title'
Field 'createTodo' doesn't accept argument 'completed'
Variable $title is declared by createTodo but not used
Variable $completed is declared by createTodo but not used

The corresponding code was below.

const CREATE_TODO = gql`
mutation createTodo($title: String!, $completed: Boolean!) {
createTodo(title: $title, completed: $completed) {
todo {
id
title
completed
}
}
}
`;

The solution

The code has to be like this.

const CREATE_TODO = gql`
mutation createTodo($title: String!, $completed: Boolean!) {
createTodo(input: { title: $title, completed: $completed }) {
todo {
id
title
completed
}
}
}
`;

‘input’ has to be used in the code.

The result of the request is like this.

 TRANSACTION (0.1ms)  begin transaction
↳ app/graphql/mutations/create_todo.rb:13:in `resolve'
Todo Create (2.3ms) INSERT INTO "todos" ("title", "completed", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "vdsvdfvdf"], ["completed", 1], ["created_at", "2024-04-29 03:52:33.844106"], ["updated_at", "2024-04-29 03:52:33.844106"]]
↳ app/graphql/mutations/create_todo.rb:13:in `resolve'
TRANSACTION (0.5ms) commit transaction
↳ app/graphql/mutations/create_todo.rb:13:in `resolve'
Completed 200 OK in 70ms (Views: 0.2ms | ActiveRecord: 2.9ms | Allocations: 3674)

Could save the object!

Feel free to reach out to me on LinkedIn, which you can find below. Looking forward to connecting!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Tomoharu Tsutsumi

Senior Software Engineer at two industry-leading startups ( Go | Ruby | TypeScript | JavaScript | Gin | Echo | Rails | React | Redux | Next)