Tutorial: Hello World GraphQL Content API
In this tutorial, you will create and query a Hello World endpoint using the GraphQL Content API (GCA). As part of this process, you will create a content type and publish assets that you will query for using the GraphQL explorer.
This tutorial has two tracks depending on how you want to complete it:
- Editorially—An option for those who want to complete this tutorial solely in Brightspot.
- Programmatically—An option for those who want to complete this tutorial in both an IDE and Brightspot.
As part of the effort, you will complete the following steps:
- Create a content type called
Post
and publish two assets from it. - Create a GCA endpoint and configure it so that it allows queries of
Post
assets. - Use the GraphQL explorer to fetch data from assets of the
Post
type.
Assumptions
This tutorial assumes the following:
- Familiarity with GraphQL
- Familiarity with the editorial process of creating a content type in Brightspot
Step 1: Creating a content type and publishing assets
Create a content type Post
using one of the two methods described in the module below. After doing so, publish two assets in Brightspot of the Post
content type. You will query for these assets later in this tutorial.
Creating a content type editorially
- Click > Admin > Content Types.
- In the Name field, enter
Post
. - Under Items, click Add, then select Text Field.
- In the text field's Name field, enter
Message
. - Click Save.
Creating a content type programmatically
- Create a new Java class called
Post
. - Add a string field called
message
.
import com.psddev.cms.db.Content;
public class Post extends Content {
private String message;
}
Publishing assets
Depending on the path you take to reach this step, do one of the following:
- Editorially—Navigate to the dashboard.
- Programmatically—Log into Brightspot.
- In the header, click .
- From the Misc Content Types list, select Post.
- In the Message field, enter
This is my first message
. - Click Publish.
- Click , located to the right of the Publish button, and then select Copy This Post.
- In the Message field of the new asset draft, enter
This is my second message
. - Click Publish.
You now have two assets of the Post
content type you created that you will query later in this tutorial.
Step 2: Creating and configuring a basic GraphQL Content API endpoint
In this step, you will create a basic GraphQL endpoint using one of the two methods described in the module below, and then configure the endpoint such that it allows queries for assets of the Post
content type.
Creating and configuring an endpoint editorially
- Click > Admin > APIs.
- In the left rail, from the Endpoints list and under the Create menu, select GraphQL Content API, then click New.
- In the Name field, enter
Hello World
as the name for the endpoint. This is an internal name used to reference the endpoint in the Endpoints list in the left rail.NoteAfter naming the endpoint, Brightspot automatically generates a draft of the endpoint’s path. Endpoint paths are tied to Brightspot's permalink system and associated with the Global site, which ensures that an endpoint’s path is not a duplicate. If a duplicate exists upon trying to save the endpoint, indicates this in an error message and prevents you from saving.
- In the Path field, verify that
/graphql/content/hello-world
is automatically generated from the Name field. Your edit page looks similar to the following image. - From the Schema Settings field, under Read/Write Content Types, select Post. By doing this, you are restricting the endpoint to only fetching
Post
assets. - Expand the Database Queries cluster, then toggle on Allow Database Queries.
- Click Save.NoteAfter saving the endpoint, the Paths field at the bottom of this edit form displays the same path as the one auto-generated from the Name field; the Paths field exists so that you know the endpoint path even when the endpoint is created programatically.
Creating and configuring an endpoint programmatically
- Create a new Java class called
Hello World
. - Extend
GCAEndpoint
to designate the class as a GCA endpoint. - Implement
Singleton
to ensure there is only one record. - Define the endpoint path.
- Define the schema settings.
- Assign
Post.class
as a read/write entry class.
import java.util.Set;
import com.psddev.dari.db.Recordable;
import com.psddev.dari.db.Singleton;
import com.psddev.graphql.gca.GCAEndpoint;
import com.psddev.graphql.gca.GCASchemaSettings;
@Recordable.DisplayName("Hello World")
public class HelloWorld extends GCAEndpoint implements Singleton {
@Override
public Set<String> getPaths() {
return Set.of("/graphql/content/hello-world");
}
@Override
protected GCASchemaSettings getSchemaSettings() {
return GCASchemaSettings.newBuilder()
.mutableEntryClass(Post.class)
.build();
}
}
-
Sets the name of the endpoint to
Hello World
. -
Defines the Java class as a GCA endpoint and ensures only one record of it is created.
-
Defines the GCA endpoint path.
-
Assigns
Post.class
as a read/write entry class.
Step 3: Querying for Post assets
You can now use the GraphQL explorer to query for Post
assets.
- Depending on the path you take to reach this step, do one of the following:
- Editorially—Continue to step 2 (as you are already on the endpoint edit page).
- Programmatically—In Brightspot, do the following:
- Click > Admin > APIs.
- From the Endpoints list on the left, select the
Hello World
endpoint you created.
- In the top right of the screen, click , then click GraphQL Explorer.NoteYour role must have developer permissions in order to use the GraphQL explorer. For details, see Creating a role.
- In the query panel in the middle, paste the following query:
query MyQuery { Query { Records(from: {type: Post}) { ... on QuerySelect { __typename items { ... on Post { __typename _id message } } } } } }
- Execute the query by clicking the Play icon. Your result should look similar to the following code snippet.
{ "data": { "Query": { "Records": { "__typename": "QuerySelect", "items": [ { "__typename": "Post", "_id": "00000191-be89-d8ae-abf1-fe9b8e9b0000", "message": "This is my first message" }, { "__typename": "Post", "_id": "00000191-be8a-d8ae-abf1-fe9b5c090000", "message": "This is my second message" } ] } } } }
From the Query Panel:
- Prettify Query—Reverts the query back to its more formatted version.
- Minify Query—Condenses the size of the query to a single line that you can scroll.
- Merge Fragments—Merges any fragments you have created into the query.
- Copy Query—Copies the query to the clipboard.
- Toggle Wrap Query—Wraps a minified query around to the next line, avoiding scrolling.
- Toggle Deprecated Fields—Hides fields that have been deprecated.
You have now created a Hello World endpoint using the GraphQL Content API (GCA), and configured it such that you can now use it to query for content.