Reputation: 4398
I'm trying to implement two mutations in graphql-ruby - one for creation of a resource, and one for editing. In most scenarios, they both take the exact same parameters from the client, so I want to avoid duplicating the mutations and try and have the arguments specified in a reusable class/module.
I'm using graphql-ruby 1.8 and the new class based API, and have started with this:
class Mutations::ApplicationMutation < GraphQL::Schema::Mutation
... common to every mutation ...
end
class Mutations::CreateResourceMutation < Mutations::ApplicationMutation
argument :name, String, required: true
argument :description, String
argument :create_only_field, String
end
class Mutations::UpdateResourceMutation < Mutations::ApplicationMutation
argument :name, String, required: true
argument :description, String
argument :update_only_field, String
end
In this basic example, the name
and description
attributes are the same in both mutations. I've extracted the resolver out into another class so that is reusable, but I'm not sure the best way to tackle the arguments.
I imagine an ActiveSupport::Concern would work, but it doesn't feel right or the way I think it should work for this, but I'm very new to GraphQL in general so have no idea.
Upvotes: 1
Views: 320
Reputation: 96
I can suggest a nice solution creating a complex argument and use in two mutations. Here is the code
# graphql/inputs/resource_input.rb
Inputs::ResourceInput = GraphQL::InputObjectType.define do
name 'ResourceInput'
description 'An input object representing arguments for a user'
argument :name, String, required: true
argument :description, String
argument :create_only_field, String
argument :update_only_field, String
end
and in mutations you can just use
argument :resourceInput, Inputs::ResourceInput, "the resource complex input"
Hope this will help you to avoid dupes
Upvotes: 3