A Guide to Using GraphQL with Rasayel
Introduction
#
GraphQL is a powerful query language designed to provide more flexible and efficient communication between clients and servers. In this guide, we will explain how to use GraphQL with the Rasayel API, focusing on the base type App and how most objects are connected to it.
For reference, visit the types directory .
Composing a GraphQL Query
#
A GraphQL query is made up of three main components: the operation type, selection set, and, when needed, variables. Let's break down each part to understand how they work together.
Operation Type
#
There are three primary operation types in GraphQL:
- Query: Used to request data from the server.
- Mutation: Used to modify data on the server.
- Subscription: Used for real-time updates to data.
For this guide, we will focus on queries.
Selection Set
#
The selection set is a list of fields you want to retrieve from the server. You can request multiple fields in a single query and even nest fields to fetch related data.
Variables
#
Variables are optional but provide a way to pass dynamic values into the query. They can be used in arguments to filter, sort, or paginate the data you request.
Example Query
#
Let's examine an example query that retrieves the first 10 conversations, filtering them by channel ID:
In this query:
app
is the base type from which we are requesting data.conversations
is the field we want to retrieve from app. It accepts arguments such as first and search to filter and paginate results.first: 10
is an argument that limits the number of conversations fetched to 10.search: { filters: [{channelId: {is: {YOUR ID}}]}
is another argument, applying a filter that only returns conversations with a specific channel ID.id
andlastMessage
are fields requested for each conversation.readAt
is a nested field within the lastMessage object.
Using Variables to make our query more dynamic.
#
Instead of hardcoding values, like we did before, we can use variables in our GraphQL query.
In this rewritten query, we've done the following:
- We've added the
query
keyword at the beginning, followed by an operation nameGetConversations
. The operation name is optional but helps to identify the purpose of the query. - We've defined two variables,
$first
and$channelId
, with their respective types (Int
andID
). These variables are enclosed within parentheses right after the operation name. - We've replaced the hardcoded values for first and channelId in the conversations field with their corresponding variables
$first
and$channelId
. - Now, when executing this query, you can provide variable values separately, which makes the query more reusable and dynamic. For example, you can pass the following variables when executing the query:
Accessing Types in Rasayel
#
To explore available types and their fields, visit the types directory . This resource provides detailed information on the structure of the GraphQL schema, including object types, input types, and enums.
Conclusion
#
This guide provides an introduction to using GraphQL with Rasayel, focusing on composing queries with the base type App. By understanding how to structure queries, select fields, and apply arguments, you can efficiently request data from the Rasayel API. For further information, visit the types directory .
API
Pagination