Last active
April 3, 2019 18:36
-
-
Save kayinrage/f668ba82b492058e47bc26dcf56fb5e2 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 Book, type: :request do | |
describe '.resolve' do | |
it 'returns book for provided id' do | |
author = create(:author) | |
book = create(:book, author: author, title: 'Hero', publication_date: 1984, genre: 'Horror') | |
post '/graphql', params: { query: query(id: book.id) } | |
json = JSON.parse(response.body) | |
data = json['data']['book'] | |
expect(data).to include( | |
'id' => be_present, | |
'title' => 'Hero', | |
'publicationDate' => 1984, | |
'genre' => 'Horror', | |
'author' => { 'id' => author.id.to_s } | |
) | |
end | |
end | |
def query(id:) | |
<<~GQL | |
query { | |
book(id: #{id}) { | |
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