-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* External Dependencies | ||
*/ | ||
import { isFunction } from 'lodash'; | ||
|
||
/** | ||
* WordPress Dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
|
||
const createQueryHigherOrderComponent = ( client ) => ( mapPropsToQuery, mapPropsToVariables = () => ( {} ) ) => ( WrappedComponent ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would these more accurately be 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 commentThe 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 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. |
||
return class GraphQueryComponent extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.state = { | ||
data: null, errors: null, | ||
}; | ||
} | ||
|
||
componentDidMount() { | ||
this.buildQuery( this.props ); | ||
this.request(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.cancelRequest(); | ||
} | ||
|
||
componentWillReceiveProps( newProps ) { | ||
this.buildQuery( newProps ); | ||
this.request(); | ||
} | ||
|
||
buildQuery( props ) { | ||
if ( isFunction( mapPropsToQuery ) ) { | ||
this.query = mapPropsToQuery( props ); | ||
} else { | ||
this.query = mapPropsToQuery; | ||
} | ||
this.variables = mapPropsToVariables( props ); | ||
} | ||
|
||
cancelRequest() { | ||
if ( this.unsubscribe ) { | ||
this.unsubscribe(); | ||
} | ||
} | ||
|
||
request() { | ||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, actually a GraphQL result is always like this For convenience, we could add an optional callback to "polish" the props like suggested by @aduth. |
||
} ); | ||
this.unsubscribe = observer.unsubscribe; | ||
} | ||
|
||
render() { | ||
return ( | ||
<WrappedComponent { ...this.props } { ...this.state } /> | ||
); | ||
} | ||
}; | ||
}; | ||
|
||
export default createQueryHigherOrderComponent; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Internal Dependencies | ||
*/ | ||
import { getEditedPostTitle } from './selectors'; | ||
|
||
export const schema = ` | ||
type CoreEditedPost { | ||
title: String | ||
} | ||
|
||
type CoreEditor { | ||
post: CoreEditedPost | ||
} | ||
|
||
extend type Query { | ||
editor: CoreEditor | ||
} | ||
`; | ||
|
||
export const resolver = { | ||
Query: { | ||
editor: ( _, args, context ) => ( { | ||
post: () => ( { | ||
title() { | ||
const state = context.state[ 'core/editor' ]; | ||
return getEditedPostTitle( state ); | ||
}, | ||
} ), | ||
} ), | ||
}, | ||
|
||
CoreEditor: editor => editor, | ||
|
||
CoreEditedPost: post => post, | ||
}; |
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
.