-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Data Module: Expose State using a GraphQL API #4083
Conversation
cc @atimmer |
ebea8f3
to
26935d7
Compare
26935d7
to
60249eb
Compare
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
const createQueryHigherOrderComponent = ( client ) => ( mapPropsToQuery, mapPropsToVariables = () => ( {} ) ) => ( WrappedComponent ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would these more accurately be mapQueryToProps
and mapVariablesToProps
?
Or am I wrong in thinking that these functions would take values from the query result and map them to props for the component?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, they are correctly names, these are useful if you want to create custom queries based on props.
I can see something like mapResultToProp
being usefull as well. It could be a third optional callback.
Edit In a GraphQL API, this is less important though because you already ask for the data you want, it could be convenient though for "undefined" checks and stuff like that.
In your example, wouldn't you want a mapping function from the query result to the props to pass to the underlying component, like |
There's a bug in the production build |
@@ -0,0 +1,66 @@ | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, it might be located next to other HOCs inside @wordpress/components
.
It would be nice to be able to use it as I know all the benefits of GraphQL, how it can scale well, provides a unified interface and helps to limit the number of requests. I can imagine you even would be able to wrap Do you see benefits of having GraphQL in the core on top of selectors and have the latter exposed for plugin developers? I'm just trying to find a way that satisfies needs of all parties involved here. |
No, it will be useless in this case. The advantage of GraphQL is being this discoverable and easy to use API. It certains adds learning curve to people creating data (module creators and advanced plugins that want to use the system to expose their own data), but from a consumer's perspective, which I think is the majority of plugin authors and developpers, its API is even simpler to grasp than having to learn and also find every available selector. |
this.cancelRequest(); | ||
const query = client.query( { query: this.query, variables: this.variables } ); | ||
const observer = query.subscribe( ( results ) => { | ||
this.setState( results ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your example in the pull request description, the data is accessed using props.data
, but in the higher order component, you only set the state. How does this work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, actually a GraphQL result is always like this { data, errors }
so we'll end up with these two props.
For convenience, we could add an optional callback to "polish" the props like suggested by @aduth. mapResultsToProps
Closing this as I think we should go with the selectors API |
This is a POC to trigger a discussion of whether we should continue this way or not. Alternative to #4105 closes #2473
So the idea here is that a module that wants to expose a specific part of its state registers a GraphQL schema describing the exposed data and a GraphQL resolver to fetch this data from the state. ( see
editor/state/data.js
for an example of a (schema, resolver) pair.The data module exposes a
registerSchema
function to register the module's schema/resolver pair.And a
query
Higher Order Component is also exposed to fetch data like so: