Complete beginner's guide to setting up WordPress with GraphQL for use with the GraphQL proxy server. Learn how to prepare your WordPress backend to serve content via GraphQL API.
This guide will walk you through setting up WordPress with GraphQL capabilities so you can use it as a backend for the GraphQL proxy server. By the end of this guide, your WordPress installation will expose a GraphQL API that the proxy can consume.
Before you begin, ensure you have:
- WordPress 5.0+ installed and running
- Administrator access to WordPress admin panel
- FTP/SFTP access or file manager access to upload plugins
- Basic understanding of WordPress plugins and GraphQL concepts
WPGraphQL is the core plugin that adds GraphQL functionality to WordPress.
-
Access WordPress Admin:
- Go to
https://yourdomain.com/wp-admin/ - Log in with administrator credentials
- Go to
-
Navigate to Plugins:
- Click Plugins → Add New in the left sidebar
-
Install WPGraphQL:
- In the search box, type "WPGraphQL"
- Find "WPGraphQL" by Jason Bahl
- Click Install Now
- After installation completes, click Activate
-
Verify Installation:
- You should see "GraphQL" appear in the WordPress admin menu
- Visit Settings → GraphQL to see the plugin settings
If you plan to use custom fields, install ACF and its GraphQL integration:
-
Install ACF:
- Search for "Advanced Custom Fields" by Elliot Condon
- Install and activate
-
Install ACF to WPGraphQL:
- Search for "ACF to WPGraphQL" by Jason Bahl
- Install and activate
-
Access GraphQL Settings:
- Go to Settings → GraphQL in WordPress admin
-
Configure Basic Settings:
- GraphQL Endpoint: Usually
/graphql(default) - Exclude Private Data: Keep enabled for security
- Enable Public Introspection: Enable for development (disable in production)
- Enable GraphiQL Interface: Enable for testing queries
- GraphQL Endpoint: Usually
-
Save Changes: Click Save Changes
-
Visit GraphiQL Interface:
- Go to
https://yourdomain.com/wp-admin/edit.php?post_type=graphql&page=graphiql - Or directly to
https://yourdomain.com/graphiql(if enabled)
- Go to
-
Run Test Query:
{ generalSettings { title description } } -
Expected Response:
{ "data": { "generalSettings": { "title": "Your Site Title", "description": "Your site description" } } }
-
Create Posts:
- Go to Posts → Add New
- Add several posts with:
- Titles and content
- Categories and tags
- Featured images
- Excerpts
-
Create Categories:
- Go to Posts → Categories
- Add categories like "News", "Tutorials", "Reviews"
-
Create Tags:
- Go to Posts → Tags
- Add relevant tags for your content
- Create Pages:
- Go to Pages → Add New
- Create pages like "About", "Contact", "Privacy Policy"
- Add User:
- Go to Users → Add New
- Create an author user with some posts assigned
Test these queries in GraphiQL to understand your data structure:
{
generalSettings {
title
description
url
}
}{
posts(first: 5) {
nodes {
id
title
content
excerpt
slug
date
status
}
}
}{
categories(first: 10) {
nodes {
id
categoryId
name
slug
description
count
}
}
}{
tags(first: 20) {
nodes {
id
tagId
name
slug
count
}
}
}{
users(first: 10) {
nodes {
id
userId
name
slug
email
}
}
}{
pages(first: 5) {
nodes {
id
pageId
title
content
slug
status
}
}
}{
posts(first: 10) {
nodes {
id
title
excerpt
slug
date
categories {
nodes {
name
slug
}
}
tags {
nodes {
name
slug
}
}
author {
node {
name
slug
}
}
}
}
}{
posts(first: 10) {
nodes {
id
title
featuredImage {
node {
sourceUrl
altText
}
}
}
}
}{
posts(where: { categoryName: "news" }, first: 10) {
nodes {
id
title
categories {
nodes {
name
}
}
}
}
}If your GraphQL proxy is on a different domain, you may need CORS configuration:
-
Check Current CORS Settings:
- In GraphQL settings, check if CORS is enabled
-
Add CORS Headers (if needed):
- Add this to your
functions.phpor create a custom plugin:
// Add CORS headers for GraphQL endpoint add_action('init', function() { if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') { header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST, OPTIONS'); header('Access-Control-Allow-Headers: Content-Type, Authorization'); exit; } });
- Add this to your
For authenticated requests (mutations), set up authentication:
-
Install JWT Authentication:
- Search for "JWT Authentication for WP-API" plugin
- Install and activate
-
Configure JWT:
- Add these constants to
wp-config.php:
define('JWT_AUTH_SECRET_KEY', 'your-secret-key-here'); define('JWT_AUTH_CORS_ENABLE', true);
- Add these constants to
-
Test Authentication:
# Get JWT token curl -X POST https://yourdomain.com/wp-json/jwt-auth/v1/token \ -H "Content-Type: application/json" \ -d '{"username":"admin","password":"password"}'
-
Install Caching Plugin:
- Search for "WP Rocket" or "WP Super Cache"
- Install and configure
-
GraphQL-Specific Caching:
- The GraphQL proxy will handle its own caching
- WordPress caching can still help with admin performance
-
Optimize Database:
- Go to Tools → Database Optimize
- Or use a plugin like "WP-Optimize"
-
Monitor Performance:
- Check query execution times in GraphiQL
- Monitor database queries
If you installed ACF earlier:
-
Create Field Groups:
- Go to Custom Fields → Add New
- Create fields for posts, pages, or custom post types
-
Test Custom Fields in GraphQL:
{ posts(first: 5) { nodes { id title customFields { key value } } } }
If you have custom post types:
-
Ensure CPT is Public:
- Custom post types must be public to appear in GraphQL
-
Query Custom Post Types:
{ customPostTypeName(first: 10) { nodes { id title content } } }
-
Set Environment Variables:
export UPSTREAM_GRAPHQL_ENDPOINT=https://yourdomain.com/graphql export GRAPHQL_MODE=GETMODE
-
Start Proxy:
bun run start
-
Health Check:
curl http://localhost:5001/health
-
Test Query through Proxy:
curl -X POST http://localhost:5001/graphql \ -H "Content-Type: application/json" \ -d '{"query": "{ posts(first: 5) { nodes { id title } } }"}'
-
Test Caching:
- Run the same query twice
- Check response headers for
x-cache: HIT
Before going live:
-
Disable GraphiQL in Production:
- Go to GraphQL settings
- Disable "Enable GraphiQL Interface"
-
Disable Introspection:
- Disable "Enable Public Introspection"
-
Set Proper CORS:
- Instead of
*, specify your domain:
header('Access-Control-Allow-Origin: https://yourdomain.com');
- Instead of
-
Use HTTPS:
- Ensure your WordPress site uses SSL
-
Enable Compression:
- Ensure GZIP compression is enabled
-
Set Appropriate Limits:
- Configure PHP memory limits
- Set reasonable execution timeouts
-
Monitor Resources:
- Monitor database queries
- Set up error logging
- Ensure WPGraphQL plugin is activated
- Check if endpoint is accessible at
/graphql - Verify permalink structure is set up
- Check if posts are published (not drafts)
- Verify user permissions
- Check GraphQL query syntax
- Enable CORS in GraphQL settings
- Add proper CORS headers in WordPress
- Check if SSL is configured correctly
- Verify JWT plugin is configured
- Check secret keys in wp-config.php
- Test token generation
- Enable caching
- Optimize database
- Check for N+1 query problems
Enable WordPress debug mode for troubleshooting:
// Add to wp-config.php
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);Now that your WordPress site has GraphQL enabled, you can:
- Connect to GraphQL Proxy: Configure the proxy to use your WordPress GraphQL endpoint
- Build Frontend Applications: Use the guides in this documentation to build apps
- Add Custom Fields: Extend your content with ACF and custom GraphQL fields
- Implement Authentication: Add user authentication for mutations
- Set Up Webhooks: Trigger actions when content changes
- Monitor Performance: Set up logging and monitoring for production
You've successfully set up WordPress with GraphQL capabilities. Your WordPress site now serves as a powerful headless CMS backend that can be consumed by the GraphQL proxy server and any GraphQL-compatible applications.
The GraphQL proxy will now be able to:
- ✅ Query your WordPress content efficiently
- ✅ Cache responses for better performance
- ✅ Handle offline scenarios (with SETMODE)
- ✅ Support CRUD operations (with proper authentication)
Happy coding! 🚀