I want to create a query fragment that I can use to query regular WordPress posts and custom post types which share the same properties. Assume I have the following code using graphql aliases:
query getData($includeCategory: Boolean!) {
wp {
data1: customPostTypes(where: {categoryName: "Exammple 1"}, first: 3) {
nodes {
...dataFragment
}
}
data2: posts(first:3) {
nodes {
...dataFragment
}
}
data3: customPostTypes(where: {categoryName:"Example 2"}, first: 3) {
nodes {
...dataFragment
}
}
}
}
and a single query fragment that looks like:
fragment dataFragment on WP_CustomPostType {
title
uri
status
id
categories @include(if: $includeCategory) {
nodes {
name
}
}
}
Because I have to define the type of field that the fragment will be used on, it prevents me from being able to use it for all the post types that I want to. The above example will work for customPostTypes only and not posts as the field that needs to be defined for that is WP_Post
This is purely for cosmetics but it would be awesome to reuse just one fragment for post types/custom post types with the same properties.
Is there a way I can use one query fragment for all of my post types?
UPDATE
This question is similar, however when working with WPGraphQL and custom post types, using code like this:
exports.sourceNodes = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
interface PostType {
title: String
uri: String!
status: String!
id: ID!
}
type Work implements Node & PostType {
title: String
uri: String!
status: String!
id: ID!
}
type Post implements Node & PostType {
title: String
uri: String!
status: String!
id: ID!
}
`
createTypes(typeDefs)
}
produces the error: UNHANDLED REJECTION Schema must contain uniquely named types but contains multiple types named "WP_Work"