Comment on page
Queries
A GraphQL query is used for fetching data. This can be useful for verifying which data is stored for a given object class, or for a third-party to export data from your solution.
The simples query you can run is to specify the object class (known as an object type in GraphQL). For example, if you wanted to list all Projects in the database, you could write:
{
project
}
Note that the name of the object class is specified under Endpoint Name in the object class properties in Appfarm Create.
Click the Play button (or Cmd/Ctrl + Enter) to execute the query. The response will be displayed to the right.
The query editor will also edit your query to contain all the properties (fields in GraphQL) that are available to query:
{
project {
_id
title
description
createdDate
createdBy
status
projectNumber
projectDisplayName
af_createdDate
af_updatedDate
}
}
You can use these fields in your query to customize the response that you receive. For example, the following query will only return the id and title properties of each project:
{
project {
_id
title
}
}
To further refine your query you can include arguments, see advanced queries.
If a queried object references another object, or is referenced by another object, you can extract details about the other object.
For example, if you have projects with tasks, you might like to list out the tasks for a project, or obtain the project details for a given task.
// Return all projects, with their ID, title, and an array of associated tasks with each task's ID and title
{project {
_id
title
taskList {
_id
title
}
}}
// Return all tasks with their ID, title, and an object containing the title and description of the associated project
{task {
_id
title
project_reference {
title
description
}
}}
More advanced query operators can be used to further customize the response. In the following examples, Book is queried in different ways.
Restrict the response to contain only
n
objects.// Return a maximum of 2 books, and only return the title
{
Book(limit: 2){
title
}
}
Skip the first
n
objects in the response.// Return every book after the first 2, and only return the title
{
Book(skip: 2){
title
}
}
Sort the response in ascending (ASC) or descending (DESC) order.
// Return all books, sorted in ascending order by number of pages
{
Book(sort: {pages: ASC}){
title
pages
}
}
Combine operators to filter the response.
// Return all books with more than 500 pages
{
Book(filter: {
pages: {gt: 500}
}){
title
pages
}
}
// Return all books with more than 500 pages but less than or equal to 100 pages
{
Book(filter: {
and: [{pages: {gt: 500}}, {pages: {lte: 1000}}]
}){
title
pages
}
}
// Return all books with more than 500 pages but less than or equal to 100 pages and with the title "Jane Eyre"
{
Book(filter: {
or: [
{and: [{pages: {gt: 500}}, {pages: {lte: 1000}}]},
{title: {eq: "Jane Eyre"}}
]
}){
title
pages
}
}
// Return all books with the title "Jane Eyre" or "Wuthering Heights"
{
Book(filter: {
title: {in: ["Jane Eyre","Wuthering Heights"]}
}){
title
pages
}
}
The supported filter operators are
and
, or
, not
, nor
, eq
, ne
, in
, nin
, gt
, gte
, lt
, lte
. Each filter operator will be explained by example: Example:
{
Book(filter: {
and: [{pages: {gt: 500}}, {pages: {lte: 1000}}]
}){
title
pages
}
}
This query will return all books where
- the
pages
field is greater than500
and thepages
field is less than or equal1000
Example:
{
Book(filter: {
or: [
{and: [{pages: {gt: 500}}, {pages: {lte: 1000}}]},
{title: {eq: "Jane Eyre"}}
]
}){
title
pages
}
}
This query will return all books where
- the
pages
field is greater than500
and thepages
field is less than or equal1000
or
- the
title
field equalsJane Eyre
Example:
{
Book(filter: {
pages: {not: {gt: 500}}
}){
title
pages
}
}
This query will return all books where
- the
pages
field is not greater than500
(in other words, 499 or less)
Example:
{
Book(filter: {
nor: [{price: {eq: 1.99}}, {sale: {eq: true}}]
}){
title
price
sale
}
}
This query will return all books that
- contain the
price
field whose value is not equal to1.99
and contain thesale
field whose value is not equal totrue
or - contain the
price
field whose value is not equal to1.99
but do not contain thesale
field or - do not contain the
price
field but contain thesale
field whose value is not equal totrue
or - do not contain the
price
field and do not contain thesale
field
This operator is used for comparison. Example:
{
Book(filter: {
title: {eq: "Jane Eyre"}
}){
title
pages
}
}
This query will return all books where
- the
title
field equalsJane Eyre
This operator is used for comparison. Example:
{
Book(filter: {
title: {ne: "Jane Eyre"}
}){
title
pages
}
}
This query will return all books where
- the
title
field not equalsJane Eyre
This operator is used for comparison. Example:
{
Book(filter: {
title: {in: ["Jane Eyre","Wuthering Heights"]}
}){
title
pages
}
}
This query will return all books where
- the
title
field is any ofJane Eyre
orWuthering Heights
(or rephrased: the value of thetitle
field is in the set of the valuesJane Eyre
andWuthering Heights
)
This operator is used for comparison. Example:
{
Book(filter: {
title: {nin: ["Jane Eyre","Wuthering Heights"]}
}){
title
pages
}
}
This query will return all books where
- the
title
field is none ofJane Eyre
orWuthering Heights
(or rephrased: the value of thetitle
field is not in the set of the valuesJane Eyre
andWuthering Heights
)
This operator is used for comparison. Example:
{
Book(filter: {
pages: {gt: 500}
}){
title
pages
}
}
This query will return all books where
- the
pages
field is greater than500
(i.e. 501 pages or more)
This operator is used for comparison. Example:
{
Book(filter: {
pages: {gte: 500}
}){
title
pages
}
}
This query will return all books where
- the
pages
field is greater than or equal500
(i.e. 500 pages or more)
This operator is used for comparison. Example:
{
Book(filter: {
pages: {lt: 500}
}){
title
pages
}
}
This query will return all books where
- the
pages
field is less than500
(i.e. 499 pages or less)
This operator is used for comparison. Example:
{
Book(filter: {
pages: {lte: 500}
}){
title
pages
}
}
This query will return all books where
- the
pages
field is less than or equal500
(i.e. 500 pages or less)
You can query the total number of objects in a response by using the count query type. This needs to be enabled per object class by selecting Enable Aggregate and granting the Aggregate permission to the appropriate role.
When enabled, a new query type is added to your available queries. Using the object class Order as an example, we would get access to countOrder. This is separate to the order query type for reading data.
// Return a count of all orders
{
countOrder {
count
}
}
// Return a count of all orders made on or after the 1st of September 2022
{
countOrder(filter: {orderDate: {gte: "2022-09-01"}}){
count
}
}
Last modified 3mo ago