Home > front end >  GraphQL-Ruby : When executing a mutation, get error Variable $...... is declared by ....(mutation)..
GraphQL-Ruby : When executing a mutation, get error Variable $...... is declared by ....(mutation)..

Time:02-14

I'm cutting my teeth on GraphQL ruby here and this seems like it should be basic but the docs make this very difficult to grok. Where did I go wrong here?

My goal is simply to make a GraphQL mutation in my Ruby app.

app/graphql/types/mutation_type.rb

module Types
  class MutationType < Types::BaseObject

    field :name, String

    field :create_trip, mutation: Mutations::CreateTrip
  end
end

Here's the mutation in app/graphql/mutations/create_trip.rb

class Mutations::CreateTrip < Mutations::BaseMutation
  null true
  argument :leavingFrom, String

  field :name, String

  type Types::TripType

  def resolve(leavingFrom: )
    new_trip = Trip.create(leavingFrom: leavingFrom)

    # this doesn't really do anything--- this is proof-of-concept code only
    if new_trip.save
      # Successful creation, return the created object with no errors
      {
        trip: trip,
        errors: [],
      }
    else
      # Failed save, return the errors to the client
      {
        trip: nil,
        errors: trip.errors.full_messages
      }
    end
  end
end

the app/graphql/types/trip_type.rb is

# frozen_string_literal: true

module Types
  class TripType < Types::BaseObject
    field :id, ID, null: false
    field :traveling_party_id, Integer
    field :name, String
  end
end

When I run this mutation:

mutation CreateTrip($leavingFrom: String) {
  name
}

(with some data for leavingFrom)

I get this unexpected result: "Variable $leavingFrom is declared by CreateTrip but not used"

Expected result: My mutation calls through to the resolve on my GraphQL mutation.

I think the problem is in my query, because it does not look like the Ruby code ever called. Contrary to the message itself, the Ruby variable leavingFrom is indeed used by the mutation in the resolve body.

CodePudding user response:

mutation CreateTrip($leavingFrom: String) {
  CreateTrip(leavingFrom: $leavingFrom) {
    name
  }
}

CodePudding user response:

The answer given here is totally right.

On a more explanatory way, when you are executing a query or a mutation (operation type), you can name your operation or not.

<operation_type> <operation_name(optional)> (<variables>) {
  # your query or mutation
}

# without naming it
mutation($input: String) {
  # your mutation
}

# naming it 
mutation MyMutation($input: String) {
  # your mutation
}

Also I personally always use camelCase to name my mutations/queries and have a different name between mutation/query and operation name, using your example, I would do something like this:

mutation CreateTripMutation($leavingFrom: String) {
  createTrip(leavingFrom: $leavingFrom) {
    name
  },
}

Hope to have helped you

  • Related