-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Data Module: Expose State using a GraphQL API
- Loading branch information
1 parent
0257138
commit 60249eb
Showing
7 changed files
with
315 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) => { | ||
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 ); | ||
} ); | ||
this.unsubscribe = observer.unsubscribe; | ||
} | ||
|
||
render() { | ||
return ( | ||
<WrappedComponent { ...this.props } { ...this.state } /> | ||
); | ||
} | ||
}; | ||
}; | ||
|
||
export default createQueryHigherOrderComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.