Test GraphQL resolver with rspec

Introduction

How do you guys write the graphql test? You can define a query and test it, but it's a bit annoying these days. Therefore, I will explain how to test the resolver defined in query or mutation with rspec.

Mutation example

For example, let's say you have defined the following mutation.

mutation.rb


module Mutations
  class CreateUser < BaseMutation
    argument :id, ID, required: true
    field :user, ObjectTypes::UserType, null: false

    def resolve(id: nil)
      user = ::User.create!(id: id)
      {
        user: user
       }
    end
  end
end

It's simple, but it's a mutation that gets an id and creates a user. The target of this test is this resolve.

write rspce

Now I want to write rspec.

create_user_rspec.rb


require 'rails_helper'

RSpec.describe CreateUser, type: :request do
  describe 'resolver' do
    it 'user has been created' do
      mutation = CreateUser.new(field: nil, object: nil, context:{})
      mutation.resolve(id: [User to create_id])
      expect(..).to eq ..
    end

First, create an instance of the CreateUser class, which is a mutation class. I think that the argument field and object can basically be null. For context, you can also include current_user in some cases. In that case, it will be context: {current_user: User.first}.

It's convenient to be able to enter the context directly. Then, if you read the resolve method of the created mutation, the processing in the resolve defined on the test will be executed. This makes graphql testing a lot easier!

Recommended Posts

Test GraphQL resolver with rspec
[Rails] Test with RSpec
Test Nokogiri with Rspec.
Test with RSpec + Capybara + selenium + chromedriver
Copy and paste test with RSpec
Let's unit test with [rails] Rspec!
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Controller test with RSpec
[Ruby on Rails] Model test with RSpec
Mutation with GraphQL
I want to test Action Cable with RSpec test
I rewrote the Rails tutorial test with RSpec
[For beginners] Test devise user registration with RSpec
Integration Test with Gradle
Introduction to RSpec 1. Test, RSpec
Testing model with RSpec
Use webmock with Rspec
Automatically test with Gauge
Load test with JMeter
Unit test with Junit.
Introduction to RSpec 4. Create test data with Factory Bot
Test Web API with junit
Implement GraphQL with Spring Boot
GraphQL Client starting with Ruby
API creation with Rails + GraphQL
How to test a private method with RSpec for yourself
[Rails] Test code using Rspec
Remote debugging with Gradle test
[Rails] About Rspec response test
How to erase test image after running Rspec test with CarrierWave
Understand code coverage with Rspec, the Ruby on Rails test framework
Image upload using CarrierWave ~ Rspec test ~
REST API test with REST Assured Part 2
RSpec ~ Task model validation test creation
Output test coverage with clover + gradle
Run GraphQL Ruby resolver in parallel
[Rails5] Rspec -Unit test when nesting-
About app testing RSpec (unit test)
[Java] Test private methods with JUnit
I tried GraphQL with Spring Boot
Test list inclusion relationships with AssertJ
[RSpec] How to write test code
Test Spring framework controller with Junit
[RSpec] Unit test (using gem: factory_bot)