Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gatsby-source-wordpress] Query endpoints with a sort order set #5006

Closed
wants to merge 1 commit into from

Conversation

sebastienfi
Copy link
Contributor

@sebastienfi sebastienfi commented Apr 16, 2018

Query the endpoints using the same order each time, with the first post in the head of the response.

@sebastienfi sebastienfi requested review from pieh and jbolda April 16, 2018 16:41
@gatsbybot
Copy link
Collaborator

Deploy preview for using-drupal ready!

Built with commit a745398

https://deploy-preview-5006--using-drupal.netlify.com

@gatsbybot
Copy link
Collaborator

Deploy preview for gatsbygram ready!

Built with commit a745398

https://deploy-preview-5006--gatsbygram.netlify.com

@pieh
Copy link
Contributor

pieh commented Apr 16, 2018

I'm not against this change, but just for context - what problem does this solve?

@gatsbyjs gatsbyjs deleted a comment from dangerbot-gatsby Apr 18, 2018
@m-allanson
Copy link
Contributor

It looks like this reverses the default ordering:
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters. Is this ok to merge in?

@pieh
Copy link
Contributor

pieh commented Apr 25, 2018

It won't break anything - only nodes will be inserted in different order and therefore also returned in different order if user didn't specify sort by field in query.

@sebastienfi Can you let us know why you want this change?

@sebastienfi
Copy link
Contributor Author

sebastienfi commented Apr 26, 2018

Sure!
So, the idea is to normalize the order of the created nodes. Therefore, the default order served by the source plugin. Some installation of WordPress will expose the post ordered ID DESC, some other by last modification date DESC.
That could be a problem for the GraphQL invariant schema. A lot of people using ACF Pro for its dynamic fields have chosen the "dummy post" strategy: create a dummy post immediately after post type creation - so its ID is the lowest, and expose in this dummy post all the possibilities of your schema. When Gatsby then infers the GraphQL schema, it will start with that post. The following posts, will compare to this schema deduced from the reference point (the dummy post) and not the other way around.
With this change, we normalize the order of the nodes and ensure that the nodes will always compare to the aimed schema.

@pieh
Copy link
Contributor

pieh commented Apr 26, 2018

Unless there is bug, order of nodes shouldn't affect resulting schema - we construct exampleValue (used to generate schema) from all nodes of same type, not just first node (maybe this jsdoc would explain it better than me - https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/schema/data-tree-utils.js#L140 ).

@sebastienfi
Copy link
Contributor Author

sebastienfi commented Apr 26, 2018

@pieh That's true, I may be wrong, but it seems to me the schema builds by fusionning all the posts one by one to a consolidated vision of the fields, excluding each time what doesn't fit in the model at build.
Let's say you have in the REST API these 2 posts :

{
id: 1,
acf: { 
  file: {
     key: "/anything.jpg",
     bucket: "my-bucket",
     region: "us-east-2"
    }
  }
},
{
id: 2,
acf: { 
  file: false
  }
},

I give you that an REST API sending this kind of result is completely wrong. Welcome in WordPress.

If the post with ID of 1 is applied to the schema first, then querying file.key of the post with ID of 2 will send null.
The other way around, if post with ID of 2 is applied to schema first, querying file.key of the post with ID of 1 will throw a GraphQL error.
I prefer the first option, because on the second option the build fails.

@pieh
Copy link
Contributor

pieh commented Apr 26, 2018

In your example if we have mixed types for same field (acf.file - object / boolean) gatsby should skip this field (because it can't infer correct type) and show warning in console (warning was added recently - in [email protected]).

This is why I recommend on wordpress side to add acf filters to return null if value is empty for fields like relationship, image, etc - otherwise gatsby would skip those fields. (null is neutral for gatsby, that's why this works and boolean false doesn't)

@sebastienfi
Copy link
Contributor Author

@pieh Interesting. May you have an example of WP code to send null instead of false in such scenario?

@pieh
Copy link
Contributor

pieh commented Apr 26, 2018

@sebastienfi check this comment - #4461 (comment)

@m-allanson
Copy link
Contributor

Is this something we could handle better in the WordPress plugin? e.g. if a field has false and Object values, show a link to @pieh's comment. Or add a config option that lets you decide how to handle this.

I guess it's tricky to get this right automatically. But maybe we can make it easier to deal with.

@pieh
Copy link
Contributor

pieh commented Apr 27, 2018

@m-allanson That's actually good idea - I'll give it a try. Previous idea was to replace all false values with null, but that would break stuff that has legitimate boolean values

@pieh
Copy link
Contributor

pieh commented Jun 6, 2018

@sebastienfi Can we close this PR?

@m-allanson
Copy link
Contributor

Closing as this seems inactive. Please re-open if you'd like to pick this up again!

@m-allanson m-allanson closed this Jun 27, 2018
@wardpeet wardpeet deleted the sebastienfi-wp-orderby-date-asc branch December 9, 2019 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants