Skip to content

Commit

Permalink
Mutations: create link with graphiql
Browse files Browse the repository at this point in the history
  • Loading branch information
ztratify committed Nov 30, 2020
1 parent 27fddbb commit 2e6a812
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
8 changes: 3 additions & 5 deletions app/graphql/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions app/graphql/mutations/create_link.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 2 additions & 7 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions test/graphql/mutations/create_link_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2e6a812

Please sign in to comment.