Skip to content

Instantly share code, notes, and snippets.

@jbaxleyiii
Last active September 18, 2016 21:12
Show Gist options
  • Save jbaxleyiii/70c5fff8fadfe25ef7ad4d5e623d92e3 to your computer and use it in GitHub Desktop.
Save jbaxleyiii/70c5fff8fadfe25ef7ad4d5e623d92e3 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
const GET_USER_WITH_ID = gql`
query getUser(id: $ID!) {
user { name }
}
`;
const withUser = graphql(GET_USER_WITH_ID, {
// `ownProps` are the props passed into `MyComponentWithData`
// `data` is the result data (see above)
props: ({ ownProps, data }) => {
if (data.loading) return { userLoading: true };
if (data.error) return { hasErrors: true };
return {
currentUser: data.user,
refetchUser: data.refetch,
};
}
});
class MyComponent extends Component { ... }
const MyComponentWithData = withUser(MyComponent);
MyComponent.propTypes = {
userLoading: React.PropTypes.boolean,
hasErrors: React.PropTypes.boolean,
currentUser: React.PropTypes.object,
refetchUser: React.PropTypes.func,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment