GraphQL client on Rails that actually makes you more productive
Branch: master
Clone or download
Latest commit d9092dd Feb 12, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin Set up Metaphysics in bin/console for easier debugging Nov 4, 2018
doc Add CHANGELOG generator Oct 16, 2018
lib Bump version to 0.4.0 Jan 30, 2019
spec User now can use ERB in yml fixtures Jan 21, 2019
test Pre-configure TestHelper on Rails Jan 21, 2019
tmp Keep the tmp/ dir for testing in Travis Oct 2, 2018
.gitignore Test against multiple Rubies and Rails Sep 28, 2018
.rspec Switch to rspec Sep 28, 2018
.travis.yml Do not test against Ruby 2.4.5 and Rails edge Jan 30, 2019
Appraisals Do not rely on RAILS_ENV for initialization Oct 8, 2018 Update CHANGELOG [ci skip] Jan 30, 2019 First commit Sep 26, 2018
Gemfile Do not rely on RAILS_ENV for initialization Oct 8, 2018
LICENSE.txt Add banner in README [ci skip] Feb 12, 2019
Rakefile Use normal test runner for generator tests Oct 18, 2018
artemis.gemspec Require graphql => 1.8 Jan 21, 2019
banner.png Update banner [ci skip] Feb 12, 2019

Artemis Build Status

Artemis is a GraphQL client that is designed to fit well on Rails.

  • Convention over Configuration: You'll never have to make trivial decisions or spend time on boring setup. Start making a GraphQL request in literally 30s.
  • Performant by default: You can't do wrong when it comes to performance. All GraphQL files are pre-loaded only once in production and it'll never affect runtime performance. Comes with options that enable persistent connections and even HTTP/2, the next-gen high-performance protocol.
  • First-class support for testing: Testing and stubbing GraphQL requests couldn't be simpler. No need to add external dependencies to test well.

Battle-tested at Artsy

Here is a simple comparion of GraphQL Clients implemented in different gems for the SWAPI GraphQL Wrapper.

graphql-client vs Artemis

Quick start

You could set up Artemis with just a few commands. See it in action:

$ bundle add artemis
$ rails g artemis:install artsy
$ echo '
query($id: String!) {
  artist(id: $id) {
}' > app/operations/artsy/artist.graphql
$ rails c
> Artsy.artist(id: "leonardo-da-vinci")     # => "Leonardo da Vinci"
> Artsy.artist(id: "leonardo-da-vinci").data.artist.birthday # => "1452/04/15"

Getting started

Add this line to your application's Gemfile:

gem 'artemis'

And then execute:

$ bundle

Once you run bundle install on your Rails app, run the install command:

$ rails g artemis:install artsy

You could also use the --authorization option to assign a token so the installer can download the GraphQL schema:

$ rails g artemis:install github --authorization 'token ...'

Generating your first query

Artemis comes with a query generator. For exmaple, you could use the query generator to generate a query stub for artist:

$ rails g artemis:query artist

Then this will generate:

# app/operations/artist.graphql
query($id: String!) {
  artist(id: $id) {
    # Add fields here...

Then you could the class method that has the matching name artist:

Artsy.artist(id: "pablo-picasso")
# => makes a GraphQL query that's in app/operations/artist.graphql

You can also specify a file name:

$ rails g artemis:query artist artist_details_on_artwork
# => generates app/operations/artist_details_on_artwork.graphql

Then you can make a query in artist_details_on_artwork.graphql with:

Artsy.artist_details_on_artwork(id: "pablo-picasso")

The convention

Artemis assumes that the files related to GraphQL are organized in a certain way. For example, a service that talks to Artsy's GraphQL API could have the following structure:

│   ├── artsy
│   │   ├── _artist_fragment.graphql
│   │   ├── artwork.graphql
│   │   ├── artist.graphql
│   │   └── artists.graphql
│   └── artsy.rb
│   └── artsy
│       ├── artwork.yml
│       ├── artist.yml
│       └── artists.yml


Youcan use the before_execute callback to intercept outgoing requests and the after_execute callback to observe the response. A common operation that's done in the before_execute hook is assigning a token to the header:

class Artsy < Artemis::Client
  before_execute do |document, operation_name, variables, context|
    context[:headers] = {
      Authorization: "token ..."

Here the :headers key is a special context type. The hash object assigned to the context[:headers] will be sent as the HTTP headers of the request.

Another common thing when receiving a response is to check if there's any error in the response and throw and error accordingly:

class Artsy < Artemis::Client
  after_execute do |data, errors, extensions|
    raise "GraphQL error: #{errors.to_json}" if errors.present?


You can configure the GraphQL client using the following options. Those configurations are found in the config/graphql.yml.

Name Required? Default Description
adapter No :net_http The underlying client library that actually makes an HTTP request. See Adapters for available options.
pool_size No 25 The number of keep-alive connections. The :net_http adapter will ignore this option.
schema_path No See above The path to the GrapQL schema. Setting an empty value to this will force the client to download the schema upon the first request.
timeout No 10 HTTP timeout set for the adapter in seconds. This will be set to both read_timeout and write_timeout and there is no way to configure them with a different value as of writing (PRs welcome!)
url Yes N/A The URL for the GraphQL endpoint.


There are four adapter options available. Choose the adapter that best fits on your use case.

Adapter Protocol Keep-alive Performance Dependencies
:curb HTTP/1.1, HTTP/2 Yes Fastest curb 0.9.6+
libcurl 7.64.0+
nghttp2 1.0.0+
:net_http (default) HTTP/1.1 only No Slow None
:net_http_persistent HTTP/1.1 only Yes Fast net-http-persistent 3.0.0+
:test N/A (See Testing)

Rake tasks

Artemis also adds a useful rake graphql:schema:update rake task that downloads the GraphQL schema using the Introspection query.

Task Name Options Description
graphql:schema:update SERVICE: Service name the schema is downloaded from
AUTHORIZATION: HTTTP Authroization header value used to download the schema
Downloads and saves the GraphQL schema


Given that you have app/operations/artsy/artist.graphql and fixture file for the artist.yml:

# test/fixtures/graphql/artist.yml:
      name: Leonardo da Vinci
      birthday: 1452/04/15

      name: Yayoi Kusama
      birthday: 1929/03/22

Then you can stub the request with the stub_graphql DSL:

stub_graphql(Artsy, :artist, id: "yayoi-kusama").to_return(:yayoi_kusama)
stub_graphql(Artsy, :artist, id: "leonardo-da-vinci").to_return(:leonardo_da_vinci)

yayoi_kusama = Artsy.artist(id: "yayoi-kusama")     # => "Yayoi Kusama" # => "1452/04/15"

da_vinci = Artsy.artist(id: "leonardo-da-vinci")     # => "Leonardo da Vinci" # => "1452/04/15"

You can also use JSON instead of YAML. See example fixtures and test cases.


Setting up the test helper with Artemis is very easy and simple. Just add the following code to the test/test_helper.rb in your app:

# spec/test_helper.rb
require 'artemis/test_helper'

class ActiveSupport::TestCase
  setup do


Artemis also comes with a script that wires up helper methods on Rspec. Because it is more common to use the spec/ directory to organize spec files in RSpec, the config.artemis.fixture_path config needs to point to spec/fixtures/graphql. Other than that, it is very straightforward to set it up:

# config/application.rb
config.artemis.fixture_path = 'spec/fixtures/graphql'
# Add this to your spec/rails_helper.rb or spec_helper.rb if you don't have rails_helper.rb
require 'artemis/rspec'


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Artemis project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.