Skip to content

Instantly share code, notes, and snippets.

@EfeAgare
Forked from kayinrage/update_book_spec.rb
Created April 21, 2022 12:59
Show Gist options
  • Select an option

  • Save EfeAgare/2ca964892c510bdd61edb16c74fb3c0d to your computer and use it in GitHub Desktop.

Select an option

Save EfeAgare/2ca964892c510bdd61edb16c74fb3c0d to your computer and use it in GitHub Desktop.
Testing GraphQL Mutations In Ruby On Rails with Rspec
require 'rails_helper'
module Mutations
module Books
RSpec.describe UpdateBook, type: :request do
describe 'resolve' do
it 'updates a book' do
book = create(:book, title: 'Hero', publication_date: 1984, genre: 'Horror')
author = create(:author)
post '/graphql', params: { query: query(id: book.id, author_id: author.id) }
expect(book.reload).to have_attributes(
'author_id' => author.id,
'title' => 'Tripwire',
'publication_date' => 1999,
'genre' => 'Thriller',
)
end
it 'returns a book' do
book = create(:book, title: 'Hero', publication_date: 1984, genre: 'Horror')
author = create(:author)
post '/graphql', params: { query: query(id: book.id, author_id: author.id) }
json = JSON.parse(response.body)
data = json['data']['updateBook']
expect(data).to include(
'id' => book.id.to_s,
'title' => 'Tripwire',
'publicationDate' => 1999,
'genre' => 'Thriller',
'author' => { 'id' => author.id.to_s }
)
end
end
def query(id:, author_id:)
<<~GQL
mutation {
updateBook(
id: #{id}
authorId: #{author_id}
title: "Tripwire"
publicationDate: 1999
genre: Thriller
) {
id
title
publicationDate
genre
author {
id
}
}
}
GQL
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment