From 2e6a812723dda1c2428325e12d21718208bc0bd4 Mon Sep 17 00:00:00 2001 From: ztratify Date: Sun, 29 Nov 2020 17:47:51 -0800 Subject: [PATCH] Mutations: create link with graphiql --- app/graphql/mutations/base_mutation.rb | 8 +++----- app/graphql/mutations/create_link.rb | 17 +++++++++++++++++ app/graphql/types/mutation_type.rb | 9 ++------- test/graphql/mutations/create_link_test.rb | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 app/graphql/mutations/create_link.rb create mode 100644 test/graphql/mutations/create_link_test.rb diff --git a/app/graphql/mutations/base_mutation.rb b/app/graphql/mutations/base_mutation.rb index 0749ec0..5c245e7 100644 --- a/app/graphql/mutations/base_mutation.rb +++ b/app/graphql/mutations/base_mutation.rb @@ -1,8 +1,6 @@ module Mutations - class BaseMutation < GraphQL::Schema::RelayClassicMutation - argument_class Types::BaseArgument - field_class Types::BaseField - input_object_class Types::BaseInputObject - object_class Types::BaseObject + # This class is used as a parent for all mutations, and it is the place to have common utilities + class BaseMutation < GraphQL::Schema::Mutation + null false end end diff --git a/app/graphql/mutations/create_link.rb b/app/graphql/mutations/create_link.rb new file mode 100644 index 0000000..5df44bd --- /dev/null +++ b/app/graphql/mutations/create_link.rb @@ -0,0 +1,17 @@ +module Mutations + class CreateLink < BaseMutation + # arguments passed to the `resolve` method + argument :description, String, required: true + argument :url, String, required: true + + # return type from the mutation + type Types::LinkType + + def resolve(description: nil, url: nil) + Link.create!( + description: description, + url: url, + ) + end + end +end diff --git a/app/graphql/types/mutation_type.rb b/app/graphql/types/mutation_type.rb index 28516a5..ba6a63a 100644 --- a/app/graphql/types/mutation_type.rb +++ b/app/graphql/types/mutation_type.rb @@ -1,10 +1,5 @@ module Types - class MutationType < Types::BaseObject - # TODO: remove me - field :test_field, String, null: false, - description: "An example field added by the generator" - def test_field - "Hello World" - end + class MutationType < BaseObject + field :create_link, mutation: Mutations::CreateLink end end diff --git a/test/graphql/mutations/create_link_test.rb b/test/graphql/mutations/create_link_test.rb new file mode 100644 index 0000000..096d810 --- /dev/null +++ b/test/graphql/mutations/create_link_test.rb @@ -0,0 +1,18 @@ +require 'test_helper' + +class Mutations::CreateLinkTest < ActiveSupport::TestCase + def perform(user: nil, **args) + Mutations::CreateLink.new(object: nil, field: nil, context: {}).resolve(args) + end + + test 'create a new link' do + link = perform( + url: 'http://example.com', + description: 'description', + ) + + assert link.persisted? + assert_equal link.description, 'description' + assert_equal link.url, 'http://example.com' + end +end