From 2b8dbbaec7b8740630ee7099661a0bceff761c8b Mon Sep 17 00:00:00 2001 From: Ken <2770219+ken0x0a@users.noreply.github.com> Date: Fri, 28 Jun 2019 18:30:19 +0900 Subject: [PATCH] feat(typescript-react-apollo): add new option "reactApolloVersion" to specify version of "react-apollo", as exported type names are changed @ v3 --- .../typescript/react-apollo/src/index.ts | 21 ++++++++++++++++++- .../typescript/react-apollo/src/visitor.ts | 9 +++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/plugins/typescript/react-apollo/src/index.ts b/packages/plugins/typescript/react-apollo/src/index.ts index 19dce9c2de5..8ba13e90c2b 100644 --- a/packages/plugins/typescript/react-apollo/src/index.ts +++ b/packages/plugins/typescript/react-apollo/src/index.ts @@ -101,13 +101,32 @@ export interface ReactApolloRawPluginConfig extends RawClientSideBasePluginConfi * @default react-apollo */ reactApolloImportFrom?: string; - componentSuffix?: 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 = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: ReactApolloRawPluginConfig) => { diff --git a/packages/plugins/typescript/react-apollo/src/visitor.ts b/packages/plugins/typescript/react-apollo/src/visitor.ts index 2e1cf4450bb..e51c6c4ffe8 100644 --- a/packages/plugins/typescript/react-apollo/src/visitor.ts +++ b/packages/plugins/typescript/react-apollo/src/visitor.ts @@ -13,6 +13,7 @@ export interface ReactApolloPluginConfig extends ClientSideBasePluginConfig { hooksImportFrom: string; reactApolloImportFrom: string; componentSuffix: string; + reactApolloVersion: 2 | 3; } export class ReactApolloVisitor extends ClientSideBaseVisitor { @@ -25,6 +26,7 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor;`; + return `export type ${this.convertName(node.name.value + 'MutationFn')} = ReactApollo.${MutationFnNameForVersionMap[this.config.reactApolloVersion]}<${operationResultType}, ${operationVariablesTypes}>;`; } return null; } @@ -142,3 +144,8 @@ export function use${operationName}(baseOptions?: ReactApolloHooks.${operationTy return [mutationFn, component, hoc, hooks].filter(a => a).join('\n'); } } + +const MutationFnNameForVersionMap = { + 2: 'MutationFn', + 3: 'MutationFunction', +};