0

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"

LionOnTheWeb
  • 326
  • 2
  • 16
  • Hey @Aricha, I've answered something similar to this here [How to use GraphQL fragment on multiple types](https://stackoverflow.com/questions/51269168/how-to-use-graphql-fragment-on-multiple-types). – Derek Nguyen May 28 '20 at 13:39
  • @DerekNguyen thanks for your answer man, this is very similar to the issue I'm facing however it's not quite the same for WPGraphQL. Maybe I'm doing it wrong, but when I try using the method from that post I get thrown an error – `UNHANDLED REJECTION Schema must contain uniquely named types but contains multiple types named "WP_Post".` – LionOnTheWeb May 29 '20 at 03:35
  • Ah, that error sounds familiar... Could you head to graphiql and check the casing of type name? I remember seeing a while back an issue where wordpress type is actually lowercase or something like that – Derek Nguyen May 29 '20 at 03:46
  • @DerekNguyen, I seem to be able to query the data individually, similar to the fragment format above, with the correct casing of the types - WordPress' graphiql plugin lets me query based on type `Post` while wpgraphiql adds a prefix in which I can query based on type `WP_Post`. Both in capitalized format. I updated my post to reflect when I tried for the type interface suggested by your response to the post you linked. – LionOnTheWeb May 29 '20 at 04:07
  • Hmm, actually where should I put the interface code? I have it currently in `gatsby-node.js`; is that the right location for it? – LionOnTheWeb May 29 '20 at 04:29
  • I've just seen that you're creating type defs in `sourceNodes`. This should be done in `exports.createSchemaCustomization` instead (in gatsby-node.js) – Derek Nguyen May 29 '20 at 06:02
  • I get the same error, I've just replaced `sourceNodes` with the createSchemaCustomization string mentioned above. No dice. Should I replace the `implements Node` string with something else? FYI I'm using `"gatsby-source-graphql": "^2.1.29"` – LionOnTheWeb May 29 '20 at 20:50

0 Answers0