/
index.ts
165 lines (156 loc) · 4.84 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Types, PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers';
import { visit, GraphQLSchema, concatAST, Kind, FragmentDefinitionNode } from 'graphql';
import { RawClientSideBasePluginConfig, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
import { ReactApolloVisitor } from './visitor';
import { extname } from 'path';
export interface ReactApolloRawPluginConfig extends RawClientSideBasePluginConfig {
/**
* @name withHOC
* @type boolean
* @description Customized the output by enabling/disabling the HOC.
* @default true
*
* @example
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* - typescript-operations
* - typescript-react-apollo
* config:
* withHOC: false
* ```
*/
withHOC?: boolean;
/**
* @name withComponent
* @type boolean
* @description Customized the output by enabling/disabling the generated Component.
* @default true
*
* @example
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* - typescript-operations
* - typescript-react-apollo
* config:
* withComponent: false
* ```
*/
withComponent?: boolean;
/**
* @name withHooks
* @type boolean
* @description Customized the output by enabling/disabling the generated React Hooks.
* @default false
*
* @example
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* - typescript-operations
* - typescript-react-apollo
* config:
* withHooks: false
* ```
*/
withHooks?: boolean;
/**
* @name withMutationFn
* @type boolean
* @description Customized the output by enabling/disabling the generated mutation function signature.
* @default true
*
* @example
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* - typescript-operations
* - typescript-react-apollo
* config:
* withMutationFn: true
* ```
*/
withMutationFn?: boolean;
/**
* @name hooksImportFrom
* @type string
* @description You can specify alternative module that is exports `useQuery` `useMutation` and `useSubscription`.
* This is useful for further abstraction of some common tasks (eg. error handling).
* Filepath relative to generated file can be also specified.
* @default react-apollo-hooks
*/
hooksImportFrom?: string;
/**
* @name reactApolloImportFrom
* @type string
* @description You can specify module that exports components `Query`, `Mutation`, `Subscription` and HOCs
* This is useful for further abstraction of some common tasks (eg. error handling).
* Filepath relative to generated file can be also specified.
* @default react-apollo
*/
reactApolloImportFrom?: string;
/**
* @name componentSuffix
* @type string
* @description You can specify a suffix that gets attached to the name of the generated component.
* @default Component
*/
componentSuffix?: string;
/**
* @name reactApolloVersion
* @type 2 | 3
* @description Customized the output by enabling/disabling the HOC.
* @default 2
*
* @example
* ```yml
* generates:
* path/to/file.ts:
* plugins:
* - typescript
* - typescript-operations
* - typescript-react-apollo
* config:
* reactApolloVersion: 3
* ```
*/
reactApolloVersion?: 2 | 3;
}
export const plugin: PluginFunction<ReactApolloRawPluginConfig> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: ReactApolloRawPluginConfig) => {
const allAst = concatAST(
documents.reduce((prev, v) => {
return [...prev, v.content];
}, [])
);
const allFragments: LoadedFragment[] = [
...(allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION) as FragmentDefinitionNode[]).map(fragmentDef => ({ node: fragmentDef, name: fragmentDef.name.value, onType: fragmentDef.typeCondition.name.value, isExternal: false })),
...(config.externalFragments || []),
];
const visitor = new ReactApolloVisitor(allFragments, config) as any;
const visitorResult = visit(allAst, { leave: visitor });
return {
prepend: visitor.getImports(),
content: [visitor.fragments, ...visitorResult.definitions.filter(t => typeof t === 'string')].join('\n'),
};
};
export const validate: PluginValidateFn<any> = async (schema: GraphQLSchema, documents: Types.DocumentFile[], config: ReactApolloRawPluginConfig, outputFile: string) => {
if (config.withComponent === false) {
if (extname(outputFile) !== '.ts' && extname(outputFile) !== '.tsx') {
throw new Error(`Plugin "react-apollo" with "noComponents" requires extension to be ".ts" or ".tsx"!`);
}
} else {
if (extname(outputFile) !== '.tsx') {
throw new Error(`Plugin "react-apollo" requires extension to be ".tsx"!`);
}
}
};
export { ReactApolloVisitor };