gatsbyjs / gatsby-source-wordpress-experimental

The upcoming v4 of gatsby-source-wordpress, currently in beta
MIT License
385 stars 100 forks source link

ACF field fragments on multiple post types... #307

Closed YCMitch closed 4 years ago

YCMitch commented 4 years ago

So we're between a rock and a hard place with this, after spending a couple days moving from gatsby-source-graphql to here.

We have the same ACF fields on multiple post types (e.g Page, Camera, etc), and have written a Fragment to be used on all of them. As the fields get named differently on every post type (WPGraphQL_Page_FlexibleContent, WPGraphQL_Camera_FlexibleContent, etc), we just use one fragment, and query the same ID on Camera and Page at the same time:

page (id: $databaseId, idType: DATABASE_ID){
    id
    flexibleContent {
        ...FlexibleContentFragment
    }
}
camera (id: $databaseId, idType: DATABASE_ID){
   id
   title
   date
}

This doesn't work with this package. It returns the camera (as wpCamera), but we can't use wpPage to query for it as a page. Is this deliberate? Works fine in WPGraphQL and of course in gatsby-source-graphql.

TylerBarnes commented 4 years ago

Hi @MitchEff , we try to keep our schema as close as possible to the WPGraphQL schema. Can you share a reproduction /graphql endpoint I can use to check this out?

Thanks!

YCMitch commented 4 years ago

Sure thing, @TylerBarnes - I suspect we may have been able to get away with our approach cause of an odd quirk in WPGraphQL to begin with, but now we're quite dependent on it.

The endpoint is https://atomos.yourcreative.com.au/graphql

Note here you can query a Camera and Page with the same databaseId. This means our (enormous) ACF fields fragment can just be for WPGraphQL_Page, while still being used for other post types too:

Screen Shot 2020-11-24 at 11 33 53 am

If you run the same query in gatsby-source-wordpress, wpCamera returns correctly, but wpPage doesn't. It may be deliberate, cause it does seem logical - it just prevents us from using gatsby-source-wordpress.

TylerBarnes commented 4 years ago

Ahh, I see what you mean. Yeah that's definitely a bug in WPGraphQL cc @jasonbahl . There's no way to make that work in Gatsby. Likely what you want to do instead is use the ContentNode interface. That interface is a type which includes all content-related types (post types). So you can make 1 query for both pages and cameras. The only problem is I believe the ACF extension will still split the internal acf field types by post type.

Jason is planning on re-working the ACF extension so the typename isn't included in the ACF type names which should fix this in general, but until then there wont be a way to make this work in this plugin. You can think of this plugin as an automatic fork of the WPGraphQL schema. Many things are similar but in the end it's a completely different schema. Any WPGraphQL bugs like this one wont exist in Gatsby because data is resolved using Gatsby instead of using WPGraphQL.

YCMitch commented 4 years ago

Dang. Okay. @jasonbahl perhaps let me know if there's any clarity I can offer on this one.

In the interim, please don't fix this 'bug' 😬? I haven't the foggiest how we can share ACF fragments between post types otherwise, and our 'flexible content' field group has ~500 lines or so.