Wp-graphql: PostObjectUnion vs ContentNode Interface

Created on 4 May 2020  路  4Comments  路  Source: wp-graphql/wp-graphql

Is there a reason to keep the PostObjectUnion type around? Doesn't ContentNode achieve the same thing but in a better way?

For example if you want the uri of the parent post you have to do:

      parent {
        ... on Post {
          uri
        }
        ... on Page {
          uri
        }
        ... on TeamMember {
          uri
        }
        # etc
      }

This would be alot nicer:

      parent {
        uri
      }
Actionable Breaking Change API Interfaces Enhancement

All 4 comments

@TylerBarnes ContentNode _is_ indeed applied to _all_ Post Types. . .although we (maybe just me with myself?) have discussed having it applied to just post types that support content?

Not all Post Types are used for content. For example, the vip-legacy-redirects plugin uses Custom Post Types as database entries to manage 301 redirects, etc. These are _not_ content in any way, and pretty much only share the _id_ field in common with other post types.

Totally open to this change though. Unions in general feel like they have less room in the core WPGraphQL Schema now that interfaces are well supported.

That makes sense. I wonder if those cases could be solved with documentation? So plugins like that would know to filter themselves out of the ContentNode interface

Ya, I think it all depends on how we hook the Interface into being applied.

Now that I look at it, I think my intent _actually_ was to use ContentNode for _all_ Post Types, because I also have ContentNodeWithEditor Interface, which is applied to Post Types that support the editor. 馃

So ya, I think we can swap that union for the interface.

I believe ACF extension uses this Union.

@TylerBarnes #1337 resolves this. The PostObjectUnion has been replaced with ContentNode Interface throughout the core WPGraphQL plugin.

Here's an example in action:

Screen Shot 2020-06-04 at 8 27 34 AM

{
  mediaItems {
    nodes {
      id
      sourceUrl
      parent {
        node {
          __typename
          id
          uri
        }
      }
    }
  }
}

Response:

{
  "data": {
    "mediaItems": {
      "nodes": [
        {
          "id": "cG9zdDo2MjA=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/20200521_235450-3-scaled.jpg",
          "parent": {
            "node": {
              "__typename": "Page",
              "id": "cG9zdDo0Nzg=",
              "uri": "/english-page/"
            }
          }
        },
        {
          "id": "cG9zdDo2MTk=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/20200521_235450-2-scaled.jpg",
          "parent": {
            "node": {
              "__typename": "Page",
              "id": "cG9zdDo0Nzg=",
              "uri": "/english-page/"
            }
          }
        },
        {
          "id": "cG9zdDo2MTg=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/20200521_235450-1-scaled.jpg",
          "parent": {
            "node": {
              "__typename": "Page",
              "id": "cG9zdDo0Nzg=",
              "uri": "/english-page/"
            }
          }
        },
        {
          "id": "cG9zdDo2MTc=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/20200521_235450-scaled.jpg",
          "parent": {
            "node": {
              "__typename": "Page",
              "id": "cG9zdDo0Nzg=",
              "uri": "/english-page/"
            }
          }
        },
        {
          "id": "cG9zdDo2MTM=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/giphy-12.gif",
          "parent": {
            "node": {
              "__typename": "Post",
              "id": "cG9zdDo1Nzk=",
              "uri": "/preview-without-saving-update/"
            }
          }
        },
        {
          "id": "cG9zdDo2MTI=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/giphy-11.gif",
          "parent": {
            "node": {
              "__typename": "Post",
              "id": "cG9zdDo1Nzk=",
              "uri": "/preview-without-saving-update/"
            }
          }
        },
        {
          "id": "cG9zdDo2MTE=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/giphy-10.gif",
          "parent": {
            "node": {
              "__typename": "Post",
              "id": "cG9zdDo1Nzk=",
              "uri": "/preview-without-saving-update/"
            }
          }
        },
        {
          "id": "cG9zdDo2MTA=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/giphy-9.gif",
          "parent": {
            "node": {
              "__typename": "Post",
              "id": "cG9zdDo1Nzk=",
              "uri": "/preview-without-saving-update/"
            }
          }
        },
        {
          "id": "cG9zdDo2MDk=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/giphy-8.gif",
          "parent": null
        },
        {
          "id": "cG9zdDo2MDg=",
          "sourceUrl": "http://acf2.local/wp-content/uploads/2020/06/giphy-1.gif",
          "parent": null
        }
      ]
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasonbahl picture jasonbahl  路  4Comments

benknight picture benknight  路  3Comments

TylerBarnes picture TylerBarnes  路  4Comments

cr101 picture cr101  路  4Comments

CalebBarnes picture CalebBarnes  路  4Comments