The GraphQL API allows you to query and mutate any resource type on your Appwrite server through the endpoint
/v1/graphql. Every endpoint available through REST is available through GraphQL, except for OAuth.
Although every query executes through the same endpoint, there are multiple ways to make a GraphQL request. All requests, however, share a common structure.
|query||required||string||The GraphQL query to execute.|
|operationName||optional||string||If the query contains several named operations, controls which one to execute.|
|variables||optional||object|| An object containing variable names and values for the query. Variables are made available to your query with the |
GraphQL Model Parameters
In Appwrite's GraphQL API, all internal model parameters are prefixed with
_ instead of
$ is reserved by GraphQL.
$collectionId in the REST API would be referenced as
_collectionId in the GraphQL API.
You can execute a GraphQL query via a GET request, passing a
query and optionally
variables as query parameters.
There are multiple ways to make a GraphQL POST request, differentiated by content type.
There are two ways to make requests with the
application/json content type. You can send a JSON object containing a
query and optionally
variables, or an array of objects with the same structure.
application/graphql content type can be used to send a query as the raw POST body.
Multipart Form Data
multipart/form-data content type can be used to upload files via GraphQL. In this case, the form data must include the following parts in addition to the files to upload:
|operations||required||string||JSON encoded GraphQL query and optionally operation name and variables. File variables should contain null values.|
|map||required||string|| JSON encoded map of form-data filenames to the operations dot-path to inject the file to, e.g. |
A response to a GraphQL request will have the following structure:
|data||object||The data returned by the query, maps requested field names to their results.|
|errors||object||An array of errors that occurred during the request.|
The data object will contain a map of requested field names to their results. If no data is returned, the object will not be present in the response.
The errors array will contain error objects, each with their own message and path. The path will contain the field key that is null due to the error. If no errors occur, the array will not be present in the response.
GraphQL authenticates using Appwrite accounts and sessions. Both accounts and sessions can be created with GraphQL using the
More information and examples of authenticating users can be found in the dedicated authentication guide.
There are two main features that make GraphQL appealing when compared to the REST API: selection sets and query batching.
Selection sets can be used to tell a GraphQL API exactly which fields of a particular resource you would like to receive in the response. The server will respond with only those fields, nothing more, nothing less. This gives you full control over what data comes into your application.
For example, to retrieve only the email of a currently authenticated user, you could query the
accountGet field, passing only email as the field selection set.
Given this query, the GraphQL API will respond with:
This can be a useful feature for performance, network efficiency, and app responsiveness. As the processing happens on the server, the bandwidth consumed for the request can be dramatically reduced.
GraphQL allows sending multiple queries or mutations in the same request. There are two different ways to batch queries. The simplest way is to include multiple fields in a single query or mutation.
If both field executions succeed, the response will contain a data key for each field, containing the values of the selected fields.
If there was no authenticated user, the
accountGet field would fail to resolve. In such a case the value of the data key for that field will be null, and an object will be added to the errors array instead.
Batching with a single query or mutation has some down-sides. You can not mix and match queries and mutations within the same request unless you provide an operationName, in which case you can only execute one query per request.
Additionally, all variables must be passed in the same object, which can be cumbersome and hard to maintain.
The second way to batch is to pass an array of queries or mutations in the request. In this way, you can execute queries and mutations and keep variables separated for each.
This allows you to execute complex actions in a single network request.
Appwrite SDKs also support GraphQL in addition to the REST services.