Writing Queries

To the endpoint created send the following query in a graphql client like GraphiQL.

{
  books{
    isbn
    title
  }
}

You should be able to see the two books we loaded in the test.hello function. Book Scan GraphQL

Lets modify the QueryType to allow querying a book by isbn.

# Changes to graphql/query_type.rb

require 'graphql'
require_relative 'types/book'
require_relative '../models/book'

class QueryType < GraphQL::Schema::Object
  # ...
  # ...

  field :book, Types::Book, null: true do
    description 'Get a book by isbn'
    argument :isbn, String, required: true
  end

  def book(isbn:)
    Book.find(isbn: isbn)
  end

  # ...

If you query by isbn like below, you will get the correct book.

Find Book GraphQL

Get notified on engineering articles like this

Follow us on twitter @neumeralhq