Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make suffix of generated React Apollo components customizable #1843

Merged
merged 3 commits into from May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/plugins/typescript/react-apollo/src/index.ts
Expand Up @@ -101,6 +101,13 @@ 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
*/
}

export const plugin: PluginFunction<ReactApolloRawPluginConfig> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: ReactApolloRawPluginConfig) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/plugins/typescript/react-apollo/src/visitor.ts
Expand Up @@ -12,11 +12,13 @@ export interface ReactApolloPluginConfig extends ClientSideBasePluginConfig {
withMutationFn: boolean;
hooksImportFrom: string;
reactApolloImportFrom: string;
componentSuffix: string;
}

export class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApolloRawPluginConfig, ReactApolloPluginConfig> {
constructor(fragments: FragmentDefinitionNode[], rawConfig: ReactApolloRawPluginConfig) {
super(fragments, rawConfig, {
componentSuffix: getConfigValue(rawConfig.componentSuffix, 'Component'),
withHOC: getConfigValue(rawConfig.withHOC, true),
withComponent: getConfigValue(rawConfig.withComponent, true),
withHooks: getConfigValue(rawConfig.withHooks, false),
Expand Down Expand Up @@ -85,7 +87,7 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<ReactApolloRawPlug
}

private _buildComponent(node: OperationDefinitionNode, documentVariableName: string, operationType: string, operationResultType: string, operationVariablesTypes: string): string {
const componentName: string = this.convertName(node.name.value, { suffix: 'Component', useTypesPrefix: false });
const componentName: string = this.convertName(node.name.value, { suffix: this.config.componentSuffix, useTypesPrefix: false });

const isVariablesRequired = operationType === 'Query' && node.variableDefinitions.some(variableDef => variableDef.type.kind === Kind.NON_NULL_TYPE);

Expand Down
Expand Up @@ -154,7 +154,7 @@ describe('React Apollo', () => {
withHooks: true,
withHOC: false,
withComponent: false,
withMutationFn: false
withMutationFn: false,
},
{
outputFile: 'graphql.tsx',
Expand Down Expand Up @@ -425,6 +425,26 @@ query MyFeed {
await validateTypeScript(content, schema, docs, {});
});

it('should generate a component with a custom suffix when specified', async () => {
const docs = [{ filePath: '', content: basicDoc }];
const content = await plugin(
schema,
docs,
{ componentSuffix: 'Q' },
{
outputFile: 'graphql.tsx',
}
);

expect(content).toBeSimilarStringTo(`
export const TestQ = (props: Omit<Omit<ReactApollo.QueryProps<TestQuery, TestQueryVariables>, 'query'>, 'variables'> & { variables?: TestQueryVariables }) =>
(
<ReactApollo.Query<TestQuery, TestQueryVariables> query={TestDocument} {...props} />
);
`);
await validateTypeScript(content, schema, docs, { componentSuffix: 'Q' });
});

it('should not generate Component', async () => {
const docs = [{ filePath: '', content: basicDoc }];
const content = await plugin(
Expand Down