0

I'm using the graphql gem and I'm having issue to create a mutation to update a recor.

This is where the mutation type is first defined:

module Types class MutationType < Types::BaseObject field :createTransaction, mutation: Mutations::CreateTransaction field :updateTransaction, mutation: Mutations::UpdateTransaction end end 

This is the mutation itself:

# app/graphql/mutations/update_transaction.rb module Mutations class UpdateTransaction < Mutations::BaseMutation graphql_name "UpdateTransaction" argument :id, ID, required: true argument :input, Inputs::UpdateTransactionInput, required: true def resolve(input:) transaction = Transaction.find(input[:id]) (...) end end end 

The update input:

module Inputs class UpdateTransactionInput < Types::BaseInputObject graphql_name "UpdateTransactionInput" description "Attributes for updating a transaction" puts "Loading Inputs::UpdateTransactionInput" # Debug line argument :id, ID, required: true argument :due_date, String, required: true argument :description, String, required: true argument :amount, Float, required: true end end 

But I keep getting the error: Found two visible definitions for UpdateTransactionInput: #<Class:0x0000000106dae998>, Inputs::UpdateTransactionInput" even thouhg I'm pretty sure it's only defined one.

I'm querying it like this:

mutation UpdateTransaction($input:UpdateTransactionInput!) { updateTransaction(input: $input) { id dueDate description amount __typename } __typename } 

Thoughts on what I am doing wrong here?

I already tried to rename the UpdateTransactionInput to something totally different and it keeps saying the same error even though the UpdateTransactionInput isn't defined in the code anymore.

1
  • I am running into the exact same issue. Did you ever find the solution? Thanks so much! Commented Jan 16 at 14:30

2 Answers 2

2

I ran into this same issue and spent a long time trying to solve it.

It turns out that the graphql gem automatically generates and exposes a corresponding Input type for each of your Mutations. You do not have to create them manually.

Define your mutation as below, and Graphql will dynamically define a corresponding input type named UpdateTransactionInput for you.

# app/graphql/mutations/update_transaction.rb module Mutations class UpdateTransaction < Mutations::BaseMutation argument :id, ID, required: true argument :due_date, String, required: true argument :description, String, required: true argument :amount, Float, required: true def resolve(id:, due_date:, description:, amount:) transaction = Transaction.find(input[:id]) (...) end end end 
Sign up to request clarification or add additional context in comments.

Comments

1

The duplication occurs because your base_mutation.rb file is inheriting from GraphQL::Schema::RelayClassicMutation. This causes automatic generation of input type files.

If you want to use your own custom input files, you should change the inheritance to GraphQL::Schema::Mutation. This will allow you to manually define your input types without the automatic generation and without throwing error of duplication.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.