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

  • 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