Skip to content

Commit

Permalink
feat(typescript-react-apollo): new option reactApolloVersion (#2077)
Browse files Browse the repository at this point in the history
"reactApolloVersion" to specify version
of "react-apollo", as exported type names
are changed @ v3
  • Loading branch information
ken0x0a authored and dotansimha committed Jul 3, 2019
1 parent 74d0596 commit cd55640
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
19 changes: 19 additions & 0 deletions packages/plugins/typescript/react-apollo/src/index.ts
Expand Up @@ -108,6 +108,25 @@ export interface ReactApolloRawPluginConfig extends RawClientSideBasePluginConfi
* @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) => {
Expand Down
9 changes: 8 additions & 1 deletion packages/plugins/typescript/react-apollo/src/visitor.ts
Expand Up @@ -13,6 +13,7 @@ export interface ReactApolloPluginConfig extends ClientSideBasePluginConfig {
hooksImportFrom: string;
reactApolloImportFrom: string;
componentSuffix: string;
reactApolloVersion: 2 | 3;
}

export class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApolloRawPluginConfig, ReactApolloPluginConfig> {
Expand All @@ -25,6 +26,7 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApolloRawPlug
withMutationFn: getConfigValue(rawConfig.withMutationFn, true),
hooksImportFrom: getConfigValue(rawConfig.hooksImportFrom, 'react-apollo-hooks'),
reactApolloImportFrom: getConfigValue(rawConfig.reactApolloImportFrom, 'react-apollo'),
reactApolloVersion: getConfigValue(rawConfig.reactApolloVersion, 2),
} as any);

autoBind(this);
Expand Down Expand Up @@ -72,7 +74,7 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApolloRawPlug
private _buildMutationFn(node: OperationDefinitionNode, operationResultType: string, operationVariablesTypes: string): string {
if (node.operation === 'mutation') {
this.imports.add(this.getReactApolloImport());
return `export type ${this.convertName(node.name.value + 'MutationFn')} = ReactApollo.MutationFn<${operationResultType}, ${operationVariablesTypes}>;`;
return `export type ${this.convertName(node.name.value + 'MutationFn')} = ReactApollo.${MutationFnNameForVersionMap[this.config.reactApolloVersion]}<${operationResultType}, ${operationVariablesTypes}>;`;
}
return null;
}
Expand Down Expand Up @@ -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',
};

0 comments on commit cd55640

Please sign in to comment.