- how to extend the Headless CMS-related GraphQL types and operations
Let’s say we wanted to extend our GraphQL schema with a custom
listMyPosts query, which, as the name suggests, would enable us to quickly retrieve all posts created via the Headless CMS application, for the currently logged in user.
In other words, we want to return all content entries of the Post content model, where the
createdBy points to the currently logged in user. For demonstration purposes, our Post content model will be very simple:
createdBy field is automatically assigned to every content entry and it represents the currently logged in user.
Creating the new
listMyPosts query can be achieved via a single
With all the changes in place, we should be able to run the following GraphQL mutation:
As we can see, the
listPosts query returned a total of three posts. On the other hand, the
listMyPosts only returned posts for the currently logged in user, which is the expected result.
In the shown examples, you may have noticed we were using the
context object in GraphQL resolver functions. This object contains multiple different properties, mainly being defined from different Webiny applications that were imported in the Headless CMS GraphQL API’s
api/headlessCMS/src/index.ts entrypoint file.
For easier discovery and type safety, we suggest a type is always assigned to the
context object in your GraphQL resolver functions.