GraphQL queries
GraphQL lets you create queries in both long-form and short formats.
By default, a GraphQL query only returns the first ten results found. To change this, use the first argument. See the GraphQL examples for the structure and implementation of GraphQL queries in Sitecore Content Hub.
Search for an asset by ID
The following query fetches data about a single asset from the M_Asset entity type with a specific ID (asset.alexander-mils-667996).
The results are as follows:
Search by definition
The following query fetches data from the M_Content entity type and returns the id and content_Name for each entity.
The results are as follows:
Resolve types from interfaces
Interface is an abstract type that includes a certain set of fields that a type must include to implement the interface. It is only available for the ContentTypes entities defined in Content Hub.
The following query fetches entities from M.Content that have names containing Fruitful Email, Fruitful Blog, or Healthy Mimosa:
The results are as follows:
These results include the content name field, the blog title and body fields from the virtual M_Content_Blog type, and the email subject and body fields from the virtual M-Content_Email type. You can request such inclusions by prepending the type with ... on [Type] in the query, creating a qualifier for the content type when an interface is returned.
Total
The total field returns the number of entities that match specified criteria.
There are no criteria specified in the following example, so the query returns the total number of entities in M.Content:
The results show a total of 23 entities:
Where
Adding a where clause to a query lets you restrict the number of results according to custom criteria, which can include logical operators such as AND and OR, and postfixes such as:
-
_eq -
_neq -
contains -
fulltext
Equal to
The following example includes a where clause stating that the asset title must be equal to a specific phrase:
The results are as follows:
Not equal to
The following example includes a where clause specifying that the asset isDraft property must have a value that is not null:
The results are as follows:
Contains
Use the contains postfix to search for a phrase inside a given field. This postfix only works on a single field.
The following example includes a where clause specifying that the content_Name property must contain the term Fruitful:
The results are as follows:
Operators
There are other operators you can use with the contains postfix such as:
-
[fieldname]_in- checks for values that appear in a defined list of potential values. -
[fieldname]_startswithchecks for values that start with a specific string. -
[fieldname]_endswithchecks for values that end with a specific string.
For a complete list, in the GraphQL IDE, click the Schema tab.
Most operators include an inverted version, which looks for values that do not match the specified criteria. For example, [fieldname]_not_contains is the opposite of [fieldname]_contains.
Full-text search
Use fulltext in the where clause to perform a full-text search. Unlike the contains postfix, fulltext searches all fields of the relevant entity, as well as any applicable relations to ancestors marked for full-text search, for a specific word or a phrase. The fields searched are those that are configured in Content Hub as full text properties.
The following example queries M.Asset for the phrase fruitful lemon in any searchable field:
The results are as follows:
You can combine fulltext with other postfixes, for example:
The results are as follows:
Order by
Use the orderBy keyword to sort the result set in a specific order.
The following query sorts results by the recipe title. You can append _DESC or _ASC to specify the sort order as descending or ascending.
The results are as follows:
Logical operators
You can use AND and OR logical operators to combine statements, creating advanced logic in your queries.
AND
The AND operator is used to combine two statements. For a given entity to be returned by the query, both statements must be true for that entity.
The following query returns results from M.Content where the content name contains Healthy Mimosa and the the blog quote is Who wants a mimosa:
The results are as follows:
OR
The OR operator is used to combine two statements. For a given entity to be returned by the query, at least one of the statements much be true for that entity.
The following query returns content where the content name is either The Power of Protein or Healthy Mimosa.
The results are as follows:
AND with nested OR
You can nest the AND and OR operators.
The following query searches for content with the name summer savings or Fruitful refreshing ginger lemonade.
The results are as follows:
Typename
The __typename field contains the name of the GraphQL schema type for that object. For example:
Running multiple queries
You can run multiple queries in a single request. The queries do not impact each other. In the following example, there are two queries running at the same time:
-
The first query (
Blog) searches M.Content, using the virtual blog types, for blogs that have Fruitful in the title and have a body that is not null. It orders the results by blog title, in ascending order. -
The second query (
Email) searches M.Content, using the virtual email types, for emails that have Fruitful in the title and have a body is not null. It orders the results by email type, in ascending order.
The results of these two queries, which are grouped within a single object named data, are as follows:
Cursor-based pagination
Cursor-based pagination returns a pointer to a specific item in the dataset. On subsequent requests, the server returns results after the given pointer.
First
The first argument limits the number of results returned by the query. The value of this argument must be less than 1,000. The default value is 10. In the following example, the limit is two results.
Without the first argument, this query returns 28 entries. With the first argument set to 2, the results include only the first two entities found:
pageInfo
The pageInfo type contains information needed for pagination.
endCursor
The endCursor field represents the cursor associated with the last entity of the current page.
The cursor is opaque and not intended to be manipulated by the client.
The results are as follows:
hasNext
hasNext is a Boolean value indicating whether there are more results available:
The results are as follows:
hasNext is true, so there are more results that were not returned.
After
The after parameter lets you return only results that occur after a specific point in the overall dataset. This point is determined using a cursor, which can be obtained using a query containing the endCursor argument. In the following query, pagination starts after the content item eyJzZWFyY2hBZnRlciI6WyIxNjEzNzMyMDk1MTcyIl0sImNvdW50Ijo1fQ==.
The results are as follows: