Skip to content
This repository was archived by the owner on Apr 11, 2019. It is now read-only.

Commit d207558

Browse files
authored
Merge pull request #40 from RyanCCollins/feat_rc_graphql_generator
Feat: update the generator to include graphql option
2 parents e8d3cc8 + e3da493 commit d207558

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

config/generators/container/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ module.exports = {
2929
default: true,
3030
message: 'Do you want actions/constants/reducer for this container?',
3131
},
32+
{
33+
type: 'confirm',
34+
name: 'wantGraphQL',
35+
default: false,
36+
message: 'Do you want a colocated GraphQL / Apollo query and mutation for this container?',
37+
},
3238
],
3339
actions: (data) => {
3440
const actions = [{
@@ -76,7 +82,7 @@ module.exports = {
7682
templateFile: './container/actions.test.js.hbs',
7783
abortOnFail: true,
7884
});
79-
85+
8086
actions.push({
8187
type: 'modify',
8288
path: '../../app/src/store.js',

config/generators/container/index.js.hbs

+42-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import * as {{ properCase name }}ActionCreators from './actions';
88
import cssModules from 'react-css-modules';
99
import styles from './index.module.scss';
1010
{{/if}}
11+
{{#if wantGraphQL}}
12+
import { graphql } from 'react-apollo';
13+
import gql from 'graphql-tag';
14+
{{/if}}
1115

12-
class {{ properCase name }} extends Component { // eslint-disable-line react/prefer-stateless-function
16+
class {{ properCase name }}Container extends Component { // eslint-disable-line react/prefer-stateless-function
1317
render() {
1418
return (
1519
{{#if wantSCSSModules}}
@@ -36,18 +40,52 @@ const mapDispatchToProps = (dispatch) => ({
3640
),
3741
});
3842
{{/if}}
39-
4043
{{#if wantSCSSModules}}
41-
const Container = cssModules({{ properCase name }}, styles);
44+
45+
const Container = cssModules({{ properCase name }}Container, styles);
4246
{{else}}
43-
const Container = {{ properCase name }};
47+
48+
const Container = {{ properCase name }}Container;
4449
{{/if}}
4550

51+
{{#if wantGraphQL}}
52+
const {{ camelCase name }}Query = gql`
53+
query {{ camelCase name }} {
54+
id
55+
}
56+
`;
57+
58+
const ContainerWithData = graphql({{ camelCase name }}Query, {
59+
props: ({ data: { loading, error, {{ camelCase name }} } }) => ({
60+
{{ camelCase name }},
61+
loading,
62+
error,
63+
}),
64+
})(Container);
65+
66+
const {{ camelCase name }}Mutation = gql`
67+
mutation {{ camelCase name }} {
68+
id
69+
}
70+
`;
71+
72+
const ContainerWithMutation = graphql({{ camelCase name }}Mutation)(ContainerWithData);
73+
{{/if}}
4674
{{#if wantActionsAndReducer}}
75+
{{#if wantGraphQL}}
76+
77+
export default connect(
78+
mapStateToProps,
79+
mapDispatchToProps
80+
)(ContainerWithMutation);
81+
{{else}}
82+
4783
export default connect(
4884
mapStateToProps,
4985
mapDispatchToProps
5086
)(Container);
87+
{{/if}}
5188
{{else}}
89+
5290
export default Container;
5391
{{/if}}

0 commit comments

Comments
 (0)