Skip to content

Instantly share code, notes, and snippets.

@rmosolgo
Last active February 7, 2024 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmosolgo/6303bb7d7db01fb70cc4d290a702088b to your computer and use it in GitHub Desktop.
Save rmosolgo/6303bb7d7db01fb70cc4d290a702088b to your computer and use it in GitHub Desktop.
Custom clientMutationId directives in GraphQL-Ruby
require "bundler/inline"
gemfile do
gem "graphql", "2.2.6"
end
class MySchema < GraphQL::Schema
class Undocumented < GraphQL::Schema::Directive
locations(FIELD_DEFINITION, INPUT_FIELD_DEFINITION)
description "This should be removed our generated docs"
end
class BaseMutation < GraphQL::Schema::RelayClassicMutation
# Copied from RelayClassicMutation, with a directive:
argument :client_mutation_id, String, "A unique identifier for the client performing the mutation.", required: false,
directives: { Undocumented => {} }
# The payload should always include this field
field(:client_mutation_id, String, "A unique identifier for the client performing the mutation.", directives: { Undocumented => {} })
end
class DoSomething < BaseMutation
argument :left, Int
argument :right, Int
field :sum, Int
def resolve(left:, right:)
{
sum: left + right,
}
end
end
class Mutation < GraphQL::Schema::Object
field :do_something, mutation: DoSomething
end
mutation(Mutation)
end
pp MySchema.execute("mutation { doSomething(input: { left: 1, right: 2 }) { sum } }").to_h
# {"data"=>{"doSomething"=>{"sum"=>3}}}
puts MySchema.to_definition
# """
# This should be removed our generated docs
# """
# directive @undocumented on FIELD_DEFINITION | INPUT_FIELD_DEFINITION
# """
# Autogenerated input type of DoSomething
# """
# input DoSomethingInput {
# """
# A unique identifier for the client performing the mutation.
# """
# clientMutationId: String @undocumented
# left: Int!
# right: Int!
# }
# """
# Autogenerated return type of DoSomething.
# """
# type DoSomethingPayload {
# """
# A unique identifier for the client performing the mutation.
# """
# clientMutationId: String @undocumented
# sum: Int
# }
# type Mutation {
# doSomething(
# """
# Parameters for DoSomething
# """
# input: DoSomethingInput!
# ): DoSomethingPayload
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment