Magidoc

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:

  1. Query: Used to request data from the server.
  2. Mutation: Used to modify data on the server.
  3. 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:

  1. app is the base type from which we are requesting data.
  2. conversations is the field we want to retrieve from app. It accepts arguments such as first and search to filter and paginate results.
  3. first: 10 is an argument that limits the number of conversations fetched to 10.
  4. search: { filters: [{channelId: {is: {YOUR ID}}]} is another argument, applying a filter that only returns conversations with a specific channel ID.
  5. id and lastMessage are fields requested for each conversation.
  6. 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:

  1. We've added the query keyword at the beginning, followed by an operation name GetConversations . The operation name is optional but helps to identify the purpose of the query.
  2. We've defined two variables, $first and $channelId , with their respective types (Int and ID ). These variables are enclosed within parentheses right after the operation name.
  3. We've replaced the hardcoded values for first and channelId in the conversations field with their corresponding variables $first and $channelId .
  4. 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 .