Integrating GraphQL with Sinatra
Introduction and Context Setting
Welcome to this lesson on integrating graphql-ruby with Sinatra! In previous lessons, you learned about setting up a basic GraphQL server, defining types and queries, and querying with arguments. In this lesson, you'll learn how to integrate graphql-ruby with Sinatra, a popular web application framework for Ruby.
Creating the GraphQL Schema and Resolvers
Type definitions define the shape of your data and the operations that can be performed. For example, you might define a book type and a query type to specify how to fetch books.
Let's define our schema using graphql-ruby's class-based approach:
Next, we define our query type with resolvers:
Finally, we create our schema:
In this code, we define a BookType with three fields: id, title, and author. The QueryType defines two query fields: books, which returns all books, and book, which takes an id argument and returns a specific book. The resolver methods fetch the data from our BOOKS array.
Integrating graphql-ruby with Sinatra
Let's create a basic Sinatra app and integrate graphql-ruby:
In this code block, we set up a POST route at /graphql that handles GraphQL requests. When a request comes in, we parse the JSON body to extract the query and variables. We then execute the query using our AppSchema and return the result as JSON. The content_type :json ensures the response has the correct content type header.
To start the server, we can add:
This configures Sinatra to run on port 4000 and bind to all network interfaces.
Example Queries and Testing: Query Definition
After we have defined our server and started it, let's do some queries.
First, we define our queries:
Here, we define two queries using Ruby's heredoc syntax for multi-line strings:
-
QUERY_BOOKS: A simple query that fetches all books. It doesn't require any arguments and returns the full list with each book'sid,title, andauthor. -
query_book_by_id(id): This is where arguments come into play. Notice thebook(id: "#{id}")syntax - we're passing an argument to thebookfield. The#{id}is Ruby string interpolation that inserts the ID value into the query string. For example, if we callquery_book_by_id('1'), it generates a query that looks likebook(id: "1"). This tells GraphQL to find and return only the book with that specific ID, rather than fetching all books and filtering on the client side.
Example Queries and Testing: Fetch the Data
Next, we use Net::HTTP to execute these queries:
The fetch_data method sends a POST request to our GraphQL endpoint with the specified query. It creates an HTTP connection, sets up the request with proper headers, sends the query as JSON, and prints the formatted response. The run_queries method executes our defined queries.
Example Queries and Testing: Output
Running the previous code should give you output like:
Which corresponds with the data we initially defined.
Summary
In this lesson, you learned how to integrate graphql-ruby with Sinatra, create a GraphQL schema and resolvers, and run your server. You've also seen examples of executing queries to fetch data. Let's go and practice now!
