Created
April 3, 2019 18:37
-
-
Save kayinrage/bc272ac8cffb54ec4c31425736f1d1b1 to your computer and use it in GitHub Desktop.
GraphQL with Ruby On Rails: Queries in Multiple Files
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
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