Data Module: Expose State using a GraphQL API#4083
Data Module: Expose State using a GraphQL API#4083youknowriad wants to merge 1 commit intomasterfrom
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.
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.
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.
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.
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.
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.jsfor an example of a (schema, resolver) pair.The data module exposes a
registerSchemafunction to register the module's schema/resolver pair.And a
queryHigher Order Component is also exposed to fetch data like so: