Skip to main content

WordPress GraphQL with WPGraphQL

Set up WPGraphQL for headless WordPress with custom types, authentication, persisted queries, and performance optimization.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
You are a WordPress GraphQL expert. Help me set up and optimize WPGraphQL for a headless media publishing platform WordPress site with a Next.js frontend.

Step 1: Install and configure WPGraphQL. Set up the base plugin and essential extensions: WPGraphQL for ACF (Advanced Custom Fields), WPGraphQL for WooCommerce (if applicable), and WPGraphQL Smart Cache. Configure the GraphQL endpoint at /graphql. Set up introspection access: enabled in local and staging, disabled in production. Configure CORS headers to allow requests from https://frontend.example.com, http://localhost:3000. Verify the schema loads correctly by running an introspection query.

Step 2: Extend the GraphQL schema with custom types and fields. Register 4 custom post types in the schema using register_graphql_type and show_in_graphql. For each custom type, expose the custom fields (ACF fields or post meta) as strongly-typed GraphQL fields. Create custom connections between types (e.g., Author has many Articles, Article has many Comments). Register custom GraphQL mutations for 5 write operations that the frontend needs.

Step 3: Implement authentication for protected queries and mutations. Set up JWT via WPGraphQL JWT Authentication authentication. Create a login mutation that returns an auth token. Configure which queries require authentication: public content queries are open, user-specific queries (orders, profile) require authentication, and all mutations require authentication with role checks. Implement a refresh token mechanism with 15 minutes token expiry.

Step 4: Optimize query performance. Implement persisted queries using the automatic persisted queries (APQ) approach to prevent arbitrary queries in production. Identify and fix N+1 query problems by checking the SQL queries generated for articles with author, categories, tags, and related articles (use the Debug Extension in development). Set up query complexity analysis with a maximum complexity of 500 and depth limit of 10 to prevent expensive queries. Enable WPGraphQL Smart Cache with Redis for object-level caching.

Step 5: Build the frontend data fetching layer in Next.js. Set up Apollo Client as the GraphQL client. Create typed hooks for the 10 most common queries: page content, blog posts with pagination, navigation menus, site settings, and featured articles by category. Implement ISR (Incremental Static Regeneration) for static pages with a revalidation interval of 60 seconds. Set up preview mode that fetches draft content via authenticated queries.

Step 6: Set up the development and deployment workflow. Create a codegen pipeline using GraphQL Code Generator that generates TypeScript types from the WPGraphQL schema. Run codegen automatically when the schema changes. Write integration tests for 15 critical queries using Jest with MSW for mocking. Set up a staging environment where the frontend points to a staging WordPress instance for testing schema changes before production deployment.

What this prompt does

This prompt frames the model as a WPGraphQL expert setting up and tuning a headless [site_type] WordPress site with a [frontend_framework] front end. It runs six stages: installing and configuring WPGraphQL, extending the schema, authentication, performance, the front-end data layer, and the dev/deploy workflow.

Setup wires the endpoint at [graphql_endpoint], enables introspection only in [dev_environments], and sets CORS for [allowed_origins]. Schema work registers [custom_type_count] types with their ACF/meta fields and [mutation_count] mutations, plus connections like [relationship_example]. The performance stage is where it earns its keep: persisted queries via [persisted_query_approach], N+1 hunting on [complex_query], a complexity cap of [max_complexity] and depth limit of [max_depth], and Smart Cache on [cache_backend].

The front-end and workflow stages close the loop between WordPress and [frontend_framework]. The data layer sets up [graphql_client] with [query_count] typed hooks for the common queries, ISR with a [revalidation_interval] revalidation, and a preview mode that fetches draft content through authenticated queries. A codegen pipeline using [codegen_tool] generates TypeScript types from the schema and re-runs whenever the schema changes, so the front end never drifts from the API. Pairing that with N+1 fixes and complexity limits is what keeps a headless setup both fast and type-safe in production.

When to use it

  • You are building a headless WordPress site with a [frontend_framework] front end.
  • You need custom types and ACF fields exposed as typed GraphQL fields.
  • You require authentication via [auth_method] for user-specific queries and mutations.
  • Your GraphQL endpoint is slow and you suspect N+1 queries.
  • You want persisted queries to lock down production against arbitrary requests.
  • You want a codegen pipeline keeping [frontend_framework] types in sync with the schema.

Example output

Expect a configuration-and-code plan: WPGraphQL setup with introspection gated to [dev_environments] and CORS for [allowed_origins], schema extensions registering [custom_type_count] types and [mutation_count] mutations with a connection like [relationship_example], an auth setup using [auth_method] with [token_expiry] tokens, performance work applying [persisted_query_approach], a [max_complexity]/[max_depth] guard, and Smart Cache on [cache_backend], plus [query_count] typed hooks in [frontend_framework] and a [codegen_tool] pipeline.

Pro tips

  • Disable introspection outside [dev_environments] so production does not expose the full schema.
  • Cap [max_complexity] and [max_depth] deliberately; without them a single nested query can hammer the database.
  • Profile [complex_query] with the debug extension before optimizing, so you fix real N+1s not imagined ones.
  • Lock production to persisted queries via [persisted_query_approach] rather than accepting arbitrary GraphQL.
  • Keep token expiry like [token_expiry] short and pair it with a refresh flow for protected operations.
  • Run [codegen_tool] on every schema change so [frontend_framework] types never drift from reality.
  • Register custom fields with show_in_graphql and strong types, since loosely typed fields push the cost of guessing onto the front end.
  • Set ISR's [revalidation_interval] against how fresh the content needs to be, balancing edge speed against staleness for [query_count] query types.

Frequently Asked Questions

How do I stop expensive nested queries from overloading the server?
The prompt sets a query complexity cap of `[max_complexity]` and a depth limit of `[max_depth]`, and locks production to persisted queries via `[persisted_query_approach]`. Together these prevent arbitrary or deeply nested requests from generating runaway database load.
Can I expose ACF and custom post type fields through GraphQL?
Yes. The schema-extension step registers `[custom_type_count]` custom types and exposes their ACF or post-meta fields as strongly-typed GraphQL fields. It also creates connections between types, such as `[relationship_example]`, so related data is queryable.
How is authentication handled for private data and mutations?
It sets up `[auth_method]` with a login mutation returning a token, keeps public content open, and requires authentication plus role checks for user-specific queries and all mutations. A refresh mechanism with `[token_expiry]` expiry keeps sessions secure.
Does it keep my frontend TypeScript types in sync with the schema?
Yes. The workflow sets up a codegen pipeline with `[codegen_tool]` that generates types from the WPGraphQL schema and re-runs when the schema changes. This keeps the `[frontend_framework]` front end honest as the API evolves.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in WordPress & CMS Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support