I’m glad you like it
One of the goals of this version 2 is, not only to open the doors to other source packages, but also to allow them to work together.
That means that if a site is using a package wp-graphql-source
instead of wp-source
to connects to WordPress using the WPGraphQL plugin, and you (or another package that you have installed) need to access the REST API, they can do so without affecting the requests that are done using WPGraphQL.
For example, these could be some handlers of the wp-graphql-source
package:
import { wpGraphQl } from "@frontity/wp-graphql-source/handlers";
export default {
state: {
source: {
handlers: {
home: {
library: "wpGraphQl",
pattern: "/",
options: {
query: {
// The graphql query for the home.
}
}
},
posts: {
library: "wpGraphQl",
pattern: "/(.*)?/:slug",
options: {
query: {
// The graphql query for a single post.
}
}
}
// more handlers...
}
}
},
libraries: {
source: {
handlers: {
// Expose the handler code. This will be used by actions.source.fetch.
wpGraphQl
}
}
}
};
Now, a Frontity package that needs the data of some custom post type that exposes some special fields in the REST API should be able to work without problems. This could be its index.js
:
import { wpSource } from "@frontity/wp-source/handlers";
export default {
state: {
source: {
handlers: {
allReviews: {
library: "wpSource",
pattern: "@all-reviews",
options: {
type: "archive",
endpoint: "reviews",
params: {
per_page: 100
}
}
}
}
},
libraries: {
source: {
handlers: {
// Make sure the handler is available, even if wp-source
// is not included.
wpSource
}
}
}
}
};
The only requirement for this plugin is that people add a state.source.api
with the URL of their REST API. These would be the frontity.settings.js
file for that site:
const settings = {
// ...
packages: [
{
name: "@frontity/wp-graphql-source",
state: {
source: {
graphql: "https://mysite.com/graphql"
}
}
},
{
name: "my-reviews-package",
state: {
source: {
api: "https://mysite.com/wp-json"
}
}
}
]
};
If a site uses wp-source
and has already configured state.source.api
, then you don’t need to add it again:
const settings = {
// ...
packages: [
{
name: "@frontity/wp-source",
state: {
source: {
api: "https://mysite.com/wp-json"
}
}
},
"my-reviews-package"
]
};
Yes, we have it in mind. After 8 months, this is the only part of the framework that needs a breaking change, so we want to do it as soon as possible.