Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support providers for the GraphQL server: Gato GraphQL in addition to WPGraphQL #1

Merged
merged 71 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
d1003b9
Converted `QUERY_ALL_CATEGORIES`
leoloso Aug 2, 2021
dd8bf29
Merge branch 'main' into Add-plugin-GraphQL-API-for-WP-as-the-GraphQL…
leoloso Aug 6, 2021
09006c2
Merge branch 'main' into Add-plugin-GraphQL-API-for-WP-as-the-GraphQL…
leoloso Aug 7, 2021
6ac3796
Adapted `QUERY_CATEGORY_BY_SLUG`
leoloso Aug 8, 2021
bbd4837
Partly adapted `QUERY_CATEGORY_SEO_BY_SLUG`
leoloso Aug 8, 2021
9cb52f3
Adapted query for menus
leoloso Aug 10, 2021
f586f15
Renamed field to `localURLPath`
leoloso Aug 11, 2021
5af4eac
Implemented "childPages"
leoloso Aug 11, 2021
bdc4842
Adapted query
leoloso Aug 11, 2021
5009dee
Adapted query
leoloso Aug 11, 2021
40aec34
Started adapting query
leoloso Aug 11, 2021
ccda23d
Adapted query
leoloso Aug 11, 2021
877855f
Adapted query
leoloso Aug 11, 2021
49707be
limit => -1
leoloso Aug 11, 2021
6901e65
Adapted query
leoloso Aug 11, 2021
aa3f362
Adapted query
leoloso Aug 11, 2021
e72fda1
Adapted query
leoloso Aug 11, 2021
01ff32f
Adapted query
leoloso Aug 11, 2021
0dc2dfb
Adapted queries
leoloso Aug 11, 2021
66b7f16
Fixed query mapping
leoloso Aug 11, 2021
15cb1bf
Add QueryRoot.id before/after self to avoid ApolloCache warning
leoloso Aug 11, 2021
d0c0d44
Merge branch 'main' into Add-plugin-GraphQL-API-for-WP-as-the-GraphQL…
leoloso Sep 3, 2021
e3a689b
Use input object
leoloso Nov 16, 2021
23ec814
Renamed fields: date/modified
leoloso Nov 16, 2021
0295521
Updated queries with InputObject
leoloso Nov 26, 2021
18a0c78
Merge branch 'main' into Add-plugin-GraphQL-API-for-WP-as-the-GraphQL…
leoloso Nov 26, 2021
e5ef170
Updated parentPage/childPages
leoloso Nov 29, 2021
4e6c3fd
Merge branch 'main' into Add-plugin-GraphQL-API-for-WP-as-the-GraphQL…
leoloso Nov 29, 2021
b777cb1
urlPath => urlAbsolutePath
leoloso Dec 6, 2021
a1bfceb
Merge from origin/main
leoloso Jul 4, 2023
757f9c6
Updated deps
leoloso Jul 4, 2023
be79e04
Merge remote-tracking branch 'origin/main' into Add-plugin-GraphQL-AP…
leoloso Jul 4, 2023
87f203c
urlAbsolutePath => urlPath
leoloso Jul 4, 2023
8233226
option => optionValue
leoloso Jul 4, 2023
44a02a7
option => optionValue
leoloso Jul 4, 2023
fc69733
modified => modifiedDateStr
leoloso Jul 4, 2023
1171535
date: dateStr
leoloso Jul 4, 2023
70c1e14
modified: modifiedDateStr
leoloso Jul 4, 2023
b829fb1
option => optionValue
leoloso Jul 4, 2023
d22422d
item.path !== null ("path" can be null!)
leoloso Jul 4, 2023
e6eca50
Merge remote-tracking branch 'upstream/main' into Add-plugin-GraphQL-…
leoloso Sep 15, 2023
4535892
categoryIDs => categories
leoloso Sep 15, 2023
30ea4ff
authorSlug => author.slug
leoloso Sep 15, 2023
2ef9e15
Merge remote-tracking branch 'upstream/main' into Add-plugin-GraphQL-…
leoloso Mar 26, 2024
ad81eb4
Moved queries in util under data
leoloso Mar 26, 2024
f85656d
Moved data under providers/gatographql/
leoloso Mar 26, 2024
5630efa
added env WORDPRESS_GRAPHQL_PROVIDER
leoloso Mar 26, 2024
90ede2a
Provider on github actions
leoloso Mar 26, 2024
b3f6099
providers
leoloso Mar 26, 2024
fca1759
providers
leoloso Mar 26, 2024
5ed2fb2
import back from data/
leoloso Mar 26, 2024
457056b
dynamic imports
leoloso Mar 26, 2024
3cc0dd1
original wpgraphql data
leoloso Mar 26, 2024
de6ef9a
wpgraphql util.js
leoloso Mar 26, 2024
95aff9c
dynamic exports for wpgraphql
leoloso Mar 26, 2024
835d5a6
moved getProvider()
leoloso Mar 26, 2024
3efebba
Moved util to providers
leoloso Mar 26, 2024
7a60cf2
moved data providers
leoloso Mar 26, 2024
cb3664c
from './providers/providers'
leoloso Mar 26, 2024
de03851
from './providers/wpgraphql/...'
leoloso Mar 26, 2024
827ea30
fixed dynamic exports
leoloso Mar 26, 2024
514e8bf
removed providers.js
leoloso Mar 26, 2024
b7f39f2
not async
leoloso Mar 26, 2024
22640ef
format
leoloso Mar 26, 2024
9a460f3
process WORDPRESS_GRAPHQL_PROVIDER from env
leoloso Mar 26, 2024
df1242c
no need to declare WORDPRESS_GRAPHQL_PROVIDER
leoloso Mar 26, 2024
90caf4b
throw error if non-existing provider
leoloso Mar 26, 2024
f6ab34a
requirements in README
leoloso Mar 26, 2024
9608389
info on .env
leoloso Mar 26, 2024
0a77f6a
gato graphql info in readme
leoloso Mar 26, 2024
b864d17
For WPGraphQL only.
leoloso Mar 26, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ jobs:
- run: pnpm build && pnpm export
env:
WORDPRESS_GRAPHQL_ENDPOINT: ${{ secrets.WORDPRESS_GRAPHQL_ENDPOINT }}
WORDPRESS_GRAPHQL_PROVIDER: ${{ secrets.WORDPRESS_GRAPHQL_PROVIDER }}
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Scaling WordPress with the power of [Next.js](https://nextjs.org/) and the stati

### Requirements
* [WordPress](https://wordpress.org/)
* [WPGraphQL](https://www.wpgraphql.com/)
* [WPGraphQL](https://www.wpgraphql.com/) or [Gato GraphQL](https://gatographql.com)
* Environment variables (see below)

```bash
Expand All @@ -39,6 +39,11 @@ Change it as follows:
WORDPRESS_GRAPHQL_ENDPOINT="http://yourhost.com/index.php?graphql
```

The starter by default uses **WPGraphQL**. To use **Gato GraphQL**, add this variable to `.env.local`:
```
WORDPRESS_GRAPHQL_PROVIDER="gatographql"
```

## 🚀 Getting Started

### What is this and what does it include?
Expand Down Expand Up @@ -68,30 +73,37 @@ Check out the [Issues](https://github.com/colbyfayock/next-wordpress-starter/iss

### Requirements
* [WordPress](https://wordpress.org/)
* [WPGraphQL](https://www.wpgraphql.com/)
* [WPGraphQL](https://www.wpgraphql.com/) or [Gato GraphQL](https://gatographql.com)
* Environment variables (see below)

### Providers

This project makes use of **WPGraphQL** or **Gato GraphQL** to query WordPress with GraphQL. Which provider to use is your choice. By default, **WPGraphQL** is used.

### Environment

This project makes use of WPGraphQL to query WordPress with GraphQL. In order to make that request to the appropriate endpoint, we need to set a environment variable to let Next.js know where to request the site information from.
In order to make that request to the appropriate endpoint, we need to set a environment variable to let Next.js know where to request the site information from.

Create a new file locally called `.env.local` and add the following:

```bash
WORDPRESS_GRAPHQL_ENDPOINT="[WPGraphQL Endpoint]"
WORDPRESS_GRAPHQL_ENDPOINT="[GraphQL Endpoint]"
```

Replace the contents of the variable with your WPGraphQL endpoint. By default, it should resemble `[Your Host]/graphql`.
Replace the contents of the variable with your GraphQL endpoint. By default, it should resemble `[Your Host]/graphql`.

*Note: environment variables can optionally be statically configured in next.config.js*

*Note 2: In Gato GraphQL, the public single endpoint [must be explicitly enabled](https://gatographql.com/guides/config/enabling-and-configuring-the-single-endpoint)*

#### All Environment Variables

| Name | Required | Default | Description |
| ---------------------------------- | -------- | - | ------------------------------------------------- |
| WORDPRESS_GRAPHQL_ENDPOINT | Yes | - | WordPress WPGraphQL endpoint (ex: host.com/graphl)|
| WORDPRESS_MENU_LOCATION_NAVIGATION | No | PRIMARY | Configures header navigation Menu Location |
| WORDPRESS_PLUGIN_SEO | No | false | Enables SEO plugin support (true, false) |
| Name | Required | Default | Description |
| ---------------------------------- | -------- | - | ------------------------------------------------- |
| WORDPRESS_GRAPHQL_ENDPOINT | Yes | - | WordPress GraphQL endpoint (ex: host.com/graphql) |
| WORDPRESS_GRAPHQL_PROVIDER | No | wpgraphql | GraphQL provider: 'wpgraphql' or 'gatographql' |
| WORDPRESS_MENU_LOCATION_NAVIGATION | No | PRIMARY | Configures header navigation Menu Location |
| WORDPRESS_PLUGIN_SEO | No | false | Enables SEO plugin support (true, false) |

Please note some themes do not have PRIMARY menu location.

Expand Down Expand Up @@ -185,7 +197,7 @@ By enabling the Image Accelerator from Jetpack, your images will automatically b

### Yoast SEO

The Yoast SEO plugin is partially supported including most major features like metadata and open graph customization.
The Yoast SEO plugin is partially supported including most major features like metadata and open graph customization. For WPGraphQL only.

#### Requirements
* Yoast SEO plugin
Expand Down
65 changes: 65 additions & 0 deletions plugins/providers/gatographql/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { gql } = require('@apollo/client');

const QUERY_ALL_POSTS = gql`
{
id
posts: self {
id
edges: posts(pagination: { limit: -1 }) {
node: self {
title
excerpt
databaseId: id
slug
date: dateStr
modified: modifiedDateStr
author {
node: self {
name
}
}
categories: self {
edges: categories(pagination: { limit: -1 }) {
node: self {
name
}
}
}
}
}
}
}
`;

const QUERY_SITE_METADATA = gql`
{
id
generalSettings: self {
id
description: optionValue(name: "blogdescription")
language: optionValue(name: "WPLANG")
title: optionValue(name: "blogname")
}
}
`;

const QUERY_ALL_PAGES = gql`
{
id
pages: self {
id
edges: pages(pagination: { limit: -1 }) {
node: self {
slug
modified: modifiedDateStr
}
}
}
}
`;

module.exports = {
QUERY_ALL_POSTS,
QUERY_SITE_METADATA,
QUERY_ALL_PAGES,
};
59 changes: 59 additions & 0 deletions plugins/providers/wpgraphql/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const { gql } = require('@apollo/client');

const QUERY_ALL_POSTS = gql`
{
posts(first: 10000) {
edges {
node {
title
excerpt
databaseId
slug
date
modified
author {
node {
name
}
}
categories {
edges {
node {
name
}
}
}
}
}
}
}
`;

const QUERY_SITE_METADATA = gql`
{
generalSettings {
description
language
title
}
}
`;

const QUERY_ALL_PAGES = gql`
{
pages(first: 10000) {
edges {
node {
slug
modified
}
}
}
}
`;

module.exports = {
QUERY_ALL_POSTS,
QUERY_SITE_METADATA,
QUERY_ALL_PAGES,
};
62 changes: 12 additions & 50 deletions plugins/util.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
const fs = require('fs');
const he = require('he');
const { gql, ApolloClient, InMemoryCache } = require('@apollo/client');
const { ApolloClient, InMemoryCache } = require('@apollo/client');
const RSS = require('rss');
const prettier = require('prettier');

const config = require('../package.json');

const { WORDPRESS_GRAPHQL_PROVIDER } = require('../src/lib/provider');

const {
QUERY_ALL_POSTS,
QUERY_SITE_METADATA,
QUERY_ALL_PAGES,
} = require(`./providers/${WORDPRESS_GRAPHQL_PROVIDER}/util`);

/**
* createFile
*/
Expand Down Expand Up @@ -70,34 +78,7 @@ function createApolloClient(url) {
*/

async function getAllPosts(apolloClient, process, verbose = false) {
const query = gql`
{
posts(first: 10000) {
edges {
node {
title
excerpt
databaseId
slug
date
modified
author {
node {
name
}
}
categories {
edges {
node {
name
}
}
}
}
}
}
}
`;
const query = QUERY_ALL_POSTS;

let posts = [];

Expand Down Expand Up @@ -139,15 +120,7 @@ async function getAllPosts(apolloClient, process, verbose = false) {
*/

async function getSiteMetadata(apolloClient, process, verbose = false) {
const query = gql`
{
generalSettings {
description
language
title
}
}
`;
const query = QUERY_SITE_METADATA;

let metadata = {};

Expand Down Expand Up @@ -175,18 +148,7 @@ async function getSiteMetadata(apolloClient, process, verbose = false) {
*/

async function getPages(apolloClient, process, verbose = false) {
const query = gql`
{
pages(first: 10000) {
edges {
node {
slug
modified
}
}
}
}
`;
const query = QUERY_ALL_PAGES;

let pages = [];

Expand Down
92 changes: 26 additions & 66 deletions src/data/categories.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,29 @@
import { gql } from '@apollo/client';
import {
QUERY_ALL_CATEGORIES as WPGRAPHQL_QUERY_ALL_CATEGORIES,
QUERY_CATEGORY_BY_SLUG as WPGRAPHQL_QUERY_CATEGORY_BY_SLUG,
QUERY_CATEGORY_SEO_BY_SLUG as WPGRAPHQL_QUERY_CATEGORY_SEO_BY_SLUG,
} from './providers/wpgraphql/categories';

export const QUERY_ALL_CATEGORIES = gql`
query AllCategories {
categories(first: 10000) {
edges {
node {
databaseId
description
id
name
slug
}
}
}
}
`;

export const QUERY_CATEGORY_BY_SLUG = gql`
query CategoryBySlug($slug: ID!) {
category(id: $slug, idType: SLUG) {
databaseId
description
id
name
slug
}
}
`;
import {
QUERY_ALL_CATEGORIES as GATOGRAPHQL_QUERY_ALL_CATEGORIES,
QUERY_CATEGORY_BY_SLUG as GATOGRAPHQL_QUERY_CATEGORY_BY_SLUG,
QUERY_CATEGORY_SEO_BY_SLUG as GATOGRAPHQL_QUERY_CATEGORY_SEO_BY_SLUG,
} from './providers/gatographql/categories';

export const QUERY_CATEGORY_SEO_BY_SLUG = gql`
query CategorySEOBySlug($slug: ID!) {
category(id: $slug, idType: SLUG) {
id
seo {
canonical
metaDesc
metaRobotsNofollow
metaRobotsNoindex
opengraphAuthor
opengraphDescription
opengraphModifiedTime
opengraphPublishedTime
opengraphPublisher
opengraphTitle
opengraphType
title
twitterDescription
twitterTitle
twitterImage {
altText
sourceUrl
mediaDetails {
width
height
}
}
opengraphImage {
altText
sourceUrl
mediaDetails {
height
width
}
}
const { WORDPRESS_GRAPHQL_PROVIDER } = require('lib/provider');
module.exports = {
...(WORDPRESS_GRAPHQL_PROVIDER === 'wpgraphql'
? {
QUERY_ALL_CATEGORIES: WPGRAPHQL_QUERY_ALL_CATEGORIES,
QUERY_CATEGORY_BY_SLUG: WPGRAPHQL_QUERY_CATEGORY_BY_SLUG,
QUERY_CATEGORY_SEO_BY_SLUG: WPGRAPHQL_QUERY_CATEGORY_SEO_BY_SLUG,
}
: {}),
...(WORDPRESS_GRAPHQL_PROVIDER === 'gatographql'
? {
QUERY_ALL_CATEGORIES: GATOGRAPHQL_QUERY_ALL_CATEGORIES,
QUERY_CATEGORY_BY_SLUG: GATOGRAPHQL_QUERY_CATEGORY_BY_SLUG,
QUERY_CATEGORY_SEO_BY_SLUG: GATOGRAPHQL_QUERY_CATEGORY_SEO_BY_SLUG,
}
}
}
`;
: {}),
};
Loading
Loading