Advanced Guides

Relay Compatibility

15min

Relay Compatibility

Introduction

The Parse Server GraphQL API follows latest standards currently available for highly-scalable APIs and ambitious front-end projects.

The Parse Open Source Team choose to follow the GraphQL Server Relay Specification.

Relay is a JavaScript framework for building data-driven React applications powered by GraphQL, designed from the ground up to be easy to use, extensible and, most of all, performant. Relay accomplishes this with static queries and ahead-of-time code generation.

Starting from Parse 3.10, full compatibility with Relay is implemented. This document will walk you through those implementations

Prerequisites

To begin with this tutorial, you will need:

1 - Create a New Back4App App

First of all, it’s necessary to make sure that you have an existing app created at Back4App. However, if you are a new user, you can check this tutorial to learn how to create one.

2 - Create a few Classes

In your newly created App, go to the Database Browser and click the Create a class button

Document image


Choose to create a Custom class and give it a name. Following the Relay example schema, I created the classes Faction, Ship, and others as described with matching properties, but you can create your classes to follow this documentation. Just change your queries and mutations accordingly. Remember that by convention classes start with an Uppercase letter, are CamelCase, and do not contain special characters such as spaces and symbols. Click Create class when you’re done.

3 - GraphQL Console

With your Classes and Properties created, you can go to the API Console and then GraphQL Console to execute your queries and mutations.

Document image


4 - Queries

Our very first query will retrieve an object based on its objectId (not to confuse with id). Parse has evolved and now queries support both ObjectId, formerly known as id in previous versions, but now also supports Global Id, known as id, which refers to Relay’s global ID and has a longer format as it contains the classname encrypted into it.

Example of ObjectId: EaT0dDk09v Example of id (a.k.a. Global id): RmFjdGlvbjpFYVQwZERrMDl2

Let’s make our very first query retrieving an object by its ObjectId:

GraphQL


That will output

GraphQL


Now, let’s change that for the GlobalId for Relay:

GraphQL


And notice that the result will be the same:

GraphQL


This happens because the Global Id works, as its name implies, globally, and has the class name encrypted into it, so Parse knows where to search for that ID.

5 - Refetching

Also with the Global Id you can prefetch like Relay’s specification as follows:

GraphQL


which will result in

GraphQL


6 - Connections

Relay’s connections work the same way in Parse with GraphQL, so, if you need to retrieve the Rebel’s ships:

GraphQL


which will result:

GraphQL


You can also retrieve the Nth ships:

GraphQL


resulting in

GraphQL


Or retrieve the Nth ship after a specific one, using its cursor value:

GraphQL


which will retrieve:

GraphQL


7 - Mutations

We can also use Mutations compatible with Relay’s mutations. Let’s create a new Ship:

GraphQL


That needs the following Query Variable:

GraphQL


And will return the Global Id and name as specified in the mutation:

GraphQL