Retrieve the document object
There are two different types of queries you can run with the Prismic GraphQL API. One that will return a single document and another that can return multiple documents. We will go over how to get to the document level of the response in both cases.
There are certain queries you can run to retrieve a single document for a given Custom Type. These queries exclude the edges and node fields and will return the document object directly.
Here is an example of this type of query. In this case we are querying a document of the "blog_post" type:
query {
blog_post(uid:"sample-blog-post", lang:"en-us") {
title
publication_date
_meta {
uid
}
}
}
{
"data": {
"blog_post": {
"title": [
{
"type": "heading1",
"text": "Sample Blog Post",
"spans": []
}
],
"publication_date": "2018-01-22",
"_meta": {
"uid": "sample-blog-post"
}
}
}
}
In this case, you can retrieve the document object as given in the javascript example below:
client.query({
query: gql`
query{
blog_post(uid:"sample-blog-post", lang:"en-us"){
title
publication_date
_meta{
uid
}
}
}
`
}).then(response => {
// retrieve the document object
const document = response.data.blog_post;
console.log(document);
}).catch(error => {
console.error(error);
});
If you don't run a query like the one discussed above, then you can retrieve more than one document. These types of queries will include the edges and node fields.
Note that you might still return only one document with this type of query. The import thing to note about this is that the query and response include the edges and node fields.
Here is an example of this type of query. In this case we are querying for all the documents of the "blog_post" type:
query {
allBlog_posts {
edges {
node {
title
publication_date
_meta {
uid
}
}
}
}
}
Here is an example of the response that might be returned with this type of query. In this case there are two documents returned:
{
"data": {
"allBlog_posts": {
"edges": [
{
"node": {
"title": [
{
"type": "heading1",
"text": "Sample Blog Post",
"spans": []
}
],
"publication_date": "2018-01-22",
"_meta": {
"uid": "sample-blog-post"
}
}
},
{
"node": {
"title": [
{
"type": "heading1",
"text": "Another Sample Post",
"spans": []
}
],
"publication_date": "2018-01-29",
"_meta": {
"uid": "another-sample-post"
}
}
}
]
}
}
}
You can see that in this case, the edges and node fields are returned in the response. Even if only one document is returned you will see these fields.
As shown in the response above, the edges field is an array of objects that include the node field. You can either loop through this array to retrieve all the document objects, or you can select the document you need from the array.
Here is an example in javascript that retrieves the first document object of the response:
client.query({
query: gql`
query {
allBlog_posts {
edges {
node {
title
publication_date
_meta {
uid
}
}
}
}
}
`
}).then(response => {
// retrieve the document object
const document = response.data.allBlog_posts.edges[0].node;
console.log(document);
}).catch(error => {
console.error(error);
});