Fetching a Conversation by ID with GraphQL and Ruby
Welcome! In this guide, we'll help you write a simple Ruby script to fetch a conversation by its ID using the provided GraphQL query. Don't worry if you're new to Ruby, GraphQL, or APIs in general – we'll walk you through each step.
Installing Ruby (or use an online tool)
If you don't already have Ruby installed on your computer, you can follow the instructions below to get set up. Alternatively, if you'd rather not install Ruby, you can use an online tool like repl.it to run Ruby code in your browser.
- Windows: Install Ruby here: https://www.ruby-lang.org/en/documentation/installation/#rubyinstaller
- MacOS / Linux: Install Ruby here: https://www.ruby-lang.org/en/documentation/installation/#rbenv
Before you begin, you'll need an access token for the Rasayel API. You can find your access token in the Rasayel API settings page, as documented in our Quickstart guide .
- Add the required libraries to your Ruby script.
- Define the GraphQL query.
- Call the
make_requestmethod to fetch the conversation.
Let's dive into the details of each step.
1. Add the required libraries
To interact with a GraphQL API, we'll need to send HTTP requests and parse JSON responses. Add the following
require statements at the top of your Ruby script to include the necessary libraries:
2. Define the GraphQL query
Copy the provided GraphQL query into your Ruby script as a constant string, like this:
3. Call the
make_request method to fetch the conversation
First, define the
make_request function provided in your Ruby script:
Create a new method called
fetch_conversation that takes an
id as an argument, and then calls the
make_request method with the appropriate query and variables:
Now, you can call the
fetch_conversation method with a conversation ID to fetch the desired conversation:
Complete Ruby script
Here's the complete Ruby script: