At this stage of our project review, we have successfully installed and configured our WordPress site. We have defined the different sections of our pages and demonstrated how these sections can be utilized to compose an engaging home page. In this blog post, our focus will be on explaining the process of creating queries to retrieve content data for Metalsmith, our chosen static site generator.
During the setup of our WordPress site, we made use of the WP GraphQL plugin. This powerful plugin offers an extendable GraphQL schema and API that seamlessly integrates with our WordPress site. Additionally, we needed to access the Advanced Custom Fields (ACF) and utilize any available SEO information provided by the Yoast plugin. To achieve this, we employed the WPGraphQL for Advanced Custom Fields plugin and the Add WPGraphQL SEO plugin.
To ensure that all ACF fields are accessible through the GraphQL API, it is crucial to enable the "Show in GraphQL" toggle for each respective field.
Building GraphQL queries
The WP GraphQL plugin includes the GraphiQL IDE, an invaluable tool that allows us to explore the available schema. The GraphQL schema essentially defines the various types of data and operations that can be performed through the API. It provides a comprehensive overview of the queries, mutations, and data structures that are at our disposal.
Once we have gained insights from exploring the schema, we can begin constructing GraphQL queries to retrieve the specific content we need. These queries are structured and precisely define the data we want to fetch.
Typically, a query is written starting with an entry point, such as query {}
, and within the curly braces, we specify the fields we wish to retrieve.
Read more for a thorough introduction to GraphQL. Jason Bahl also has several helpful videos on YouTube about WP GraphQL. I'd recommend to start with this one.