Graphql-code-generator: [typescript] Disable suffix in type generation

Created on 30 Jul 2019  路  14Comments  路  Source: dotansimha/graphql-code-generator

Is your feature request related to a problem? Please describe.

I am currently analysing to replace apollo codegen with @graphql-codegen, however there is a naming convention clash in our project at the moment.

Our naming convention is to always add Suffix to any graphql query, mutation or subscription. This now results in a type like MyAwesomeQueryQuery.

Describe the solution you'd like
I see there is some suffix option for MongoDB, dunno if it is exactly the same, but an option to disable the suffix would be nice.

Describe alternatives you've considered
The only alternative is not to add suffix as per our naming convention, but then looking at variable name alone is not self-explanatory enough.

enhancement plugins waiting-for-release

Most helpful comment

Available in 1.5.0 馃帀

All 14 comments

@klemenoslaj Thank you for reporting this.
So you query named as query myAwesomeQuery? And then you are getting MyAwesomeQueryQuery, right?
I think the best solution for that will be to remove the Query from the operation name, but that might be a change you'll need to do in your application and usage.
I'm not against having a flag for removing the prefix when it's already there, so I'm keeping open if someone wants to open a PR for this ;)

FWIW I am using custom naming convention config to workaround this issue:

module.exports.typeNames = (name) => {
    if (name.endsWith('QueryQuery')) {
        return `${name.slice(0, -10)}QueryData`;
    }
    if (name.endsWith('QueryQueryVariables')) {
        return `${name.slice(0, -19)}QueryVariables`;
    }
    if (name.endsWith('MutationMutation')) {
        return `${name.slice(0, -16)}MutationData`;
    }
    if (name.endsWith('MutationMutationVariables')) {
        return `${name.slice(0, -25)}MutationVariables`;
    }
    if (name.endsWith('Fragment') && !name.endsWith('InlineFragment')) {
        return name.slice(0, -8);
    }
    return name;
};

@dotansimha just a thought. Maybe even some "smart" detection could be implemented relatively fast, where codegen would check if the variable already ends with GraphQL type and if so, omit suffixing it?

@klemenoslaj I was thinking about this, but it's a breaking change and it might effect others that depends on the naming.

@klemenoslaj I was thinking about this, but it's a breaking change and it might effect others that depends on the naming.

Maybe hiding it behind the flag until the planned major release is an option?


I might have the time this week submit a PR in one way or another.

@klemenoslaj I'm working on it now, will finish in a few minutes :)

@klemenoslaj implemented in: https://github.com/dotansimha/graphql-code-generator/pull/2258
You can try it with the latest alpha version 1.4.1-alpha-dbedcb73.58 (set dedupeOperationSuffix: true in your config file).

@dotansimha super nice. :+1:

dedupeOperationSuffix flag seems to work fine, it only does not work for fragments, as it still produces FragmentFragment situation.


Thanks for that really :+1:

Looking at PR, shouldn't Fragment be a GraphQL operation as well (node.operation)? Then why is FragmentFragment produced 馃

@klemenoslaj
fixed in: https://github.com/dotansimha/graphql-code-generator/pull/2268

we cant use node.operation because we need to change it in a different visitor fn (FragmentDefinition).
node.operation is declared as export type OperationTypeNode = "query" | "mutation" | "subscription";

@dotansimha that makes sense 馃槆Perfect :+1:

@dotansimha I think I may still be hitting this issue with FragmentFragment. Here's an example of some generated code I'm getting while using the latest alpha, 1.4.1-alpha-680e7b39.95:

export type GetSubjectsForSubjectsPageQuery = {
  __typename?: 'Query';
  subjects: Array<{ __typename?: 'Subject'; id: string } & SubjectsTableFragmentFragment>;
};

export type SubjectsTableFragment = {
  __typename?: 'Subject';
  id: string;
  tag: Maybe<string>
}

Let me know if there's any other information which would help you. Great code generator btw, thanks for making it!

My bad, thanks @JKillian !
Fixed in: https://github.com/dotansimha/graphql-code-generator/commit/878f0d9bcf1695a70a7d042b4ea99f3ec3774f1d
Can you please try it? 1.4.1-alpha-7206bd0c.98

Available in 1.5.0 馃帀

Was this page helpful?
0 / 5 - 0 ratings