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

Conversation

leoloso
Copy link
Owner

@leoloso leoloso commented Mar 26, 2024

The starter now accepts defining the "provider" for the GraphQL server for WordPress, between these 2 providers:

By default, WPGraphQL is used. To use Gato GraphQL instead, we must add to .env.local:

WORDPRESS_GRAPHQL_PROVIDER="gatographql"

How does it work?

@colbyfayock's next-wordpress-starter project has a clear-cut architecture, neatly divided into logic and data layers.

To add support for another GraphQL server, only the data layer needed to be adapted; the logic layer remains as it was.

The strategy is to have the GraphQL query adapted using aliases, as to always produce the same response, independently of the server.

(In other words: even though WPGraphQL and Gato GraphQL offer slightly different schemas, they can still produce the same response).

For instance, the following query for WPGraphQL:

query AllCategories {
  categories(first: 10000) {
    edges {
      node {
        databaseId
        description
        id
        name
        slug
      }
    }
  }
}

...is adapted for Gato GraphQL like this:

query AllCategories {
  id
  categories: self {
    id
    edges: postCategories(pagination: { limit: -1 }) {
      node: self {
        databaseId: id
        description
        id
        name
        slug
      }
    }
  }
}

Since the response is the same, the logic layer doesn't need any adaptation at all.

Changes applied

Here is a description of all the changes applied over the upstream colbyfayock/next-wordpress-starter project:

Moved all data files under a providers subfolder

The data folder contains all .gql files with the GraphQL queries, originally corresponding to WPGraphQL. These files have now been moved under a corresponding "provider" subfolder:

  • data/categories.js => data/providers/wpgraphql/categories.js
  • data/menus.js => data/providers/wpgraphql/menus.js
  • data/pages.js => data/providers/wpgraphql/pages.js
  • data/posts.js => data/providers/wpgraphql/posts.js
  • data/site.js => data/providers/wpgraphql/site.js
  • data/users.js => data/providers/wpgraphql/users.js

And likewise, the corresponding files have been created for Gato GraphQL:

  • data/providers/gatographql/categories.js
  • data/providers/gatographql/menus.js
  • data/providers/gatographql/pages.js
  • data/providers/gatographql/posts.js
  • data/providers/gatographql/site.js
  • data/providers/gatographql/users.js

Depending on the selected provider, either the .gql files from WPGraphQL or from Gato GraphQL are exported.

Extracted queries from plugins/util.js

File plugins/util.js also contains a handful of GraphQL queries, to generate the metadata.

These queries have been extracted and placed under a .gql files under the corresponding "provider" subfolder:

  • plugins/providers/wpgraphql/util.js
  • plugins/providers/gatographql/util.js

Added env var WORDPRESS_GRAPHQL_PROVIDER

To select which GraphQL server to use. The default value is wpgraphql.

Warning:

Cache data may be lost when replacing the self field of a Query object.

To address this problem (which is not a bug in Apollo Client), either ensure all objects of type QueryRoot have an ID or a custom merge function, or define a custom merge function for the Query.self field, so InMemoryCache can safely merge these objects:

  existing: {"__typename":"QueryRoot","posts({\"limit\":-1})":[{"__typename":"Post","self":{"__typename":"Post","title":"Hello world!","excerpt":"Welcome to WordPress. This is your first post. Edit or delete it, then start writing!","id":1,"slug":"hello-world","date":"August 7, 2021","modified":"August 11, 2021","author":{"__typename":"User","self":{"__typename":"User","name":"admin"}},"self":{"__typename":"Post","categories({\"limit\":-1})":[{"__typename":"PostCategory","self":{"__typename":"PostCategory","name":"Uncategorized"}}]}}},{"__typename":"Post","self":{"__typename":"Post","title":"Public or Private API mode, for extra security","excerpt":"Verse Block Write poetry and other literary expressions honoring all spaces and line-breaks. Table Block Row 1 Column 1 Row 1 Column 2 Row 2 Column 1 Row 2 Column 2 Row 3 Column 1 Row 3 Column 2 Separator Block Spacer Block","id":25,"slug":"public-or-private-api-mode-for-extra-security","date":"December 12, 2020","modified":"December 12, 2020","author"
  incoming: {"__typename":"QueryRoot","pages({\"limit\":-1})":[{"__typename":"Page","self":{"__typename":"Page","slug":"sample-page","modified":"August 11, 2021"}},{"__typename":"Page","self":{"__typename":"Page","slug":"saranga","modified":"August 7, 2021"}},{"__typename":"Page","self":{"__typename":"Page","slug":"%ce%b5%cf%80%ce%af%cf%80%ce%b5%ce%b4%ce%bf-3","modified":"February 14, 2020"}},{"__typename":"Page","self":{"__typename":"Page","slug":"%ce%b5%cf%80%ce%af%cf%80%ce%b5%ce%b4%ce%bf-2","modified":"February 14, 2020"}},{"__typename":"Page","self":{"__typename":"Page","slug":"greek","modified":"February 14, 2020"}},{"__typename":"Page","self":{"__typename":"Page","slug":"page-markup-and-formatting","modified":"March 15, 2013"}},{"__typename":"Page","self":{"__typename":"Page","slug":"page-image-alignment","modified":"March 15, 2013"}},{"__typename":"Page","self":{"__typename":"Page","slug":"level-3b","modified":"June 23, 2011"}},{"__typename":"Page","self":{"__typename":"Page","slug":"level-

For more information about these options, please refer to the documentation:

  * Ensuring entity objects have IDs: https://go.apollo.dev/c/generating-unique-identifiers
  * Defining custom merge functions: https://go.apollo.dev/c/merging-non-normalized-objects

Solution: https://stackoverflow.com/questions/63423578/cache-data-may-be-lost-when-replacing-the-my-field-of-a-query-object
@leoloso leoloso changed the title Support providers for the GraphQL server for WordPress: Added Gato GraphQL in addition to WPGraphQL Support providers for the GraphQL server: Gato GraphQL in addition to WPGraphQL Mar 26, 2024
@leoloso leoloso merged commit 6db2bb9 into main Mar 26, 2024
6 checks passed
@leoloso leoloso deleted the Set-wpgraphql-gatographql-behind-a-provider branch March 26, 2024 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant