graphqlgraphql-ruby

Ruby Graphql: Found two visible definitions for UpdateTransactionInput


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.


Solution

  • 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.