forked from colbyfayock/next-wordpress-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
: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:
...is adapted for Gato GraphQL like this:
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 aproviders
subfolderThe
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.