Class: GraphQL::Schema::Mutation
- Extended by:
- GraphQL::Schema::Member::HasFields, Resolver::HasPayloadType
- Defined in:
- lib/graphql/schema/mutation.rb
Overview
This base class accepts configuration for a mutation root field, then it can be hooked up to your mutation root object type.
If you want to customize how this class generates types, in your base class,
override the various generate_* methods.
See GraphQL::Schema::RelayClassicMutation for an extension of this class with some conventions built-in.
Examples
Example: Creating a comment
# Define the mutation:
class Mutations::CreateComment < GraphQL::Schema::Mutation
argument :body, String, required: true
argument :post_id, ID, required: true
field :comment, Types::Comment, null: true
field :errors, [String], null: false
def resolve(body:, post_id:)
post = Post.find(post_id)
comment = post.comments.build(body: body, author: context[:current_user])
if comment.save
# Successful creation, return the created object with no errors
{
comment: comment,
errors: [],
}
else
# Failed save, return the errors to the client
{
comment: nil,
errors: comment.errors.
}
end
end
end
# Hook it up to your mutation:
class Types::Mutation < GraphQL::Schema::Object
field :create_comment, mutation: Mutations::CreateComment
end
# Call it from GraphQL:
result = MySchema.execute <<-GRAPHQL
mutation {
createComment(postId: "1", body: "Nice Post!") {
errors
comment {
body
author {
login
}
}
}
}
GRAPHQL
Direct Known Subclasses
Constant Summary
Constants included from GraphQL::Schema::Member::HasFields
GraphQL::Schema::Member::HasFields::CONFLICT_FIELD_NAMES, GraphQL::Schema::Member::HasFields::GRAPHQL_RUBY_KEYWORDS, GraphQL::Schema::Member::HasFields::RUBY_KEYWORDS
Constants included from EmptyObjects
EmptyObjects::EMPTY_ARRAY, EmptyObjects::EMPTY_HASH
Constants included from Resolver::HasPayloadType
Resolver::HasPayloadType::NO_INTERFACES
Constants included from GraphQL::Schema::Member::HasArguments
GraphQL::Schema::Member::HasArguments::NO_ARGUMENTS
Constants included from GraphQL::Schema::Member::GraphQLTypeNames
GraphQL::Schema::Member::GraphQLTypeNames::Boolean, GraphQL::Schema::Member::GraphQLTypeNames::ID, GraphQL::Schema::Member::GraphQLTypeNames::Int
Instance Attribute Summary
Attributes inherited from Resolver
#context, #exec_index, #exec_result, #field, #field_resolve_step, #object, #prepared_arguments, #raw_arguments
Attributes included from GraphQL::Schema::Member::BaseDSLMethods
#default_graphql_name, #graphql_name
Attributes included from GraphQL::Schema::Member::HasDeprecationReason
Class Method Summary collapse
-
.visible?(context) ⇒ Boolean
: (GraphQL::Query::Context context) -> bool.
Instance Method Summary collapse
-
#call_resolve(_args_hash) ⇒ Object
Returns the application's resolved value.
Methods included from GraphQL::Schema::Member::HasFields
add_field, all_field_definitions, extended, field, field_class, global_id_field, has_no_fields, has_no_fields?, included, own_fields
Methods included from Resolver::HasPayloadType
field, field_class, object_class, payload_type, type
Methods inherited from Resolver
all_field_argument_definitions, any_field_arguments?, argument, #arguments, #authorized?, authorizes?, broadcastable, broadcastable?, #call, complexity, default_page_size, extension, extensions, extras, field_arguments, get_field_argument, has_default_page_size?, has_max_page_size?, inherited, #initialize, max_page_size, null, #ready?, #resolve, resolve_method, #resolve_with_support, resolver_method, type, type_expr, #unauthorized_object
Methods included from GraphQL::Schema::Member::BaseDSLMethods
#authorized?, #comment, #default_relay?, #description, #introspection, #introspection?, #mutation, #visible?
Methods included from FindInheritedValue
Methods included from GraphQL::Schema::Member::HasArguments
#add_argument, #all_argument_definitions, #any_arguments?, #argument, #argument_class, #arguments, #arguments_statically_coercible?, #coerce_arguments, extended, #get_argument, included, #own_arguments, #remove_argument, #validate_directive_argument
Methods included from GraphQL::Schema::Member::HasAuthorization
#authorized?, extended, included
Methods included from GraphQL::Schema::Member::HasAuthorization::ClassConfigured
Methods included from GraphQL::Schema::Member::HasValidators
extended, #validates, #validators
Methods included from GraphQL::Schema::Member::HasPath
Methods included from GraphQL::Schema::Member::HasDirectives
add_directive, #directive, #directives, extended, get_directives, #inherited, #remove_directive, remove_directive
Methods included from GraphQL::Schema::Member::HasDeprecationReason
Methods included from GraphQL::Schema::Member::HasDataloader
#dataload, #dataload_all, #dataload_all_associations, #dataload_all_records, #dataload_association, #dataload_record, #dataloader
Constructor Details
This class inherits a constructor from GraphQL::Schema::Resolver
Class Method Details
.visible?(context) ⇒ Boolean
: (GraphQL::Query::Context context) -> bool
81 82 83 |
# File 'lib/graphql/schema/mutation.rb', line 81 def visible?(context) true end |
Instance Method Details
#call_resolve(_args_hash) ⇒ Object
Returns the application's resolved value. : (Hash[Symbol, untyped] _args_hash) -> untyped
73 74 75 76 77 |
# File 'lib/graphql/schema/mutation.rb', line 73 def call_resolve(_args_hash) # :nodoc: # Clear any cached values from `loads` or authorization: dataloader.clear_cache super end |