Skip to content

Instantly share code, notes, and snippets.

@kayinrage
Created April 3, 2019 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayinrage/bc272ac8cffb54ec4c31425736f1d1b1 to your computer and use it in GitHub Desktop.
Save kayinrage/bc272ac8cffb54ec4c31425736f1d1b1 to your computer and use it in GitHub Desktop.
GraphQL with Ruby On Rails: Queries in Multiple Files
require 'rails_helper'
module Queries
RSpec.describe Books, type: :request do
describe '.resolve' do
it 'returns all books' do
author = create(:author)
create(:book, author: author, title: 'Hero', publication_date: 1984, genre: 'Horror')
create(:book, author: author, title: 'Gifted', publication_date: 1988, genre: 'Thriller')
post '/graphql', params: { query: query }
json = JSON.parse(response.body)
data = json['data']['books']
expect(data).to match_array [
hash_including(
'id' => be_present,
'title' => 'Hero',
'publicationDate' => 1984,
'genre' => 'Horror',
'author' => { 'id' => author.id.to_s }
),
hash_including(
'id' => be_present,
'title' => 'Gifted',
'publicationDate' => 1988,
'genre' => 'Thriller',
'author' => { 'id' => author.id.to_s }
)
]
end
end
def query
<<~GQL
query {
books {
id
title
publicationDate
genre
author {
id
}
}
}
GQL
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment