Mutations

Mutation for Creating Links

Setting up mutations is as easy as queries, following a very similar process.

All GraphQL mutations start from a root type called Mutation.

This type is not auto generated.

This type is a placeholder for all GraphQL mutations.

Resolvers with Arguments

Now add a resolver for createLink.

For this purpose, you’ll use GraphQL::Function, as mentioned earlier.

Testing with Playground

To test, just restart the server again and use the new mutation with GraphiQL:

Testing with Unit Test

It’s a good practice in Ruby to unit test your resolver objects.

Here is an example of Resolvers::CreateLink test:

.../graphql-ruby/test/graphql/resolvers/create_link_test.rb
require 'test_helper'

class Resolvers::CreateLinkTest < ActiveSupport::TestCase
  def perform(args = {})
    Resolvers::CreateLink.new.call(nil, args, {})
  end

  test 'creating 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

Next Chapter

Authentication

Learn best practices for implementing email-password authentication on a GraphQL Server written in Ruby.

Go to next chapter