-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mutations: create link with graphiql
- Loading branch information
ztratify
committed
Nov 30, 2020
1 parent
27fddbb
commit 2e6a812
Showing
4 changed files
with
40 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |