Skip to content

Commit

Permalink
Merge branch 'mxstbr-issue-1954'
Browse files Browse the repository at this point in the history
  • Loading branch information
dotansimha committed Jul 6, 2019
2 parents d928e0d + a17932a commit 0e67020
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,23 @@ export interface ParsedResolversConfig extends ParsedConfig {
};
defaultMapper: ParsedMapper | null;
avoidOptionals: boolean;
addUnderscoreToArgsType: boolean;
}

export interface RawResolversConfig extends RawConfig {
/**
* @name addUnderscoreToArgsType
* @type boolean
* @description Adds `_` to generated `Args` types in order to avoid duplicate identifiers.
*
* @example With Custom Values
* ```yml
* config:
* addUnderscoreToArgsType: true
* ```
*
*/
addUnderscoreToArgsType?: boolean;
/**
* @name contextType
* @type string
Expand Down Expand Up @@ -185,6 +199,7 @@ export class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawRes
super(
rawConfig,
{
addUnderscoreToArgsType: getConfigValue(rawConfig.addUnderscoreToArgsType, false),
contextType: parseMapper(rawConfig.contextType || 'any', 'ContextType'),
rootValueType: parseMapper(rawConfig.rootValueType || '{}', 'RootValueType'),
avoidOptionals: getConfigValue(rawConfig.avoidOptionals, false),
Expand Down Expand Up @@ -616,6 +631,7 @@ export type IDirectiveResolvers${contextType} = ${name}<ContextType>;`
? `, ${this.convertName(parentName, {
useTypesPrefix: true,
}) +
(this.config.addUnderscoreToArgsType ? '_' : '') +
this.convertName(node.name, {
useTypesPrefix: false,
}) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,35 @@ import {
ScalarTypeDefinitionNode,
UnionTypeDefinitionNode,
StringValueNode,
isUnionType,
isInterfaceType,
} from 'graphql';
import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor';
import { parseMapper } from './mappers';
import { DEFAULT_SCALARS } from './scalars';
import { normalizeDeclarationKind } from './declaration-kinds';
import { EnumValuesMap, ScalarsMap, DeclarationKindConfig, DeclarationKind } from './types';
import { transformComment, buildScalars, DeclarationBlock, DeclarationBlockConfig, indent, wrapWithSingleQuotes } from './utils';
import { transformComment, buildScalars, DeclarationBlock, DeclarationBlockConfig, indent, wrapWithSingleQuotes, getConfigValue } from './utils';
import { OperationVariablesToObject } from './variables-to-object';

export interface ParsedTypesConfig extends ParsedConfig {
enumValues: EnumValuesMap;
declarationKind: DeclarationKindConfig;
addUnderscoreToArgsType: boolean;
}

export interface RawTypesConfig extends RawConfig {
/**
* @name addUnderscoreToArgsType
* @type boolean
* @description Adds `_` to generated `Args` types in order to avoid duplicate identifiers.
*
* @example With Custom Values
* ```yml
* config:
* addUnderscoreToArgsType: true
* ```
*
*/
addUnderscoreToArgsType?: boolean;
/**
* @name enumValues
* @type EnumValuesMap
Expand Down Expand Up @@ -83,6 +95,7 @@ export class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig
super(
rawConfig,
{
addUnderscoreToArgsType: getConfigValue(rawConfig.addUnderscoreToArgsType, false),
enumValues: rawConfig.enumValues || {},
declarationKind: normalizeDeclarationKind(rawConfig.declarationKind),
...additionalConfig,
Expand Down Expand Up @@ -286,7 +299,10 @@ export class BaseTypesVisitor<TRawConfig extends RawTypesConfig = RawTypesConfig
return fieldsWithArguments
.map(field => {
const name =
node.name.value +
this.convertName(node.name.value, {
useTypesPrefix: true,
}) +
(this.config.addUnderscoreToArgsType ? '_' : '') +
this.convertName(field, {
useTypesPrefix: false,
}) +
Expand Down
16 changes: 16 additions & 0 deletions packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,22 @@ describe('TypeScript Resolvers Plugin', () => {
await validate(mergeOutputs([result, `type MyCustomCtx = {};`]));
});

it('Should with correctly with addUnderscoreToArgsType set to true', async () => {
const result = (await plugin(
schema,
[],
{
addUnderscoreToArgsType: true,
},
{ outputFile: '' }
)) as Types.ComplexPluginOutput;

expect(result.content).toContain('MyType_WithArgsArgs');
expect(result.content).not.toContain('MyTypeWithArgsArgs');

await validate(mergeOutputs([result]));
});

it('Should allow to override context with mapped context type', async () => {
const result = (await plugin(
schema,
Expand Down
16 changes: 16 additions & 0 deletions packages/plugins/typescript/typescript/tests/typescript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ describe('TypeScript', () => {
};`);
validateTs(result);
});

it('#1954 - Duplicate type names for args type', async () => {
const schema = buildSchema(`
type PullRequest {
reviewThreads(first: Int!): Int
}
type PullRequestReview {
threads(first: Int!, last: Int!): Int
}`);

const result = (await plugin(schema, [], { addUnderscoreToArgsType: true }, { outputFile: '' })) as Types.ComplexPluginOutput;

expect(result.content).toContain('PullRequest_ReviewThreadsArgs');
expect(result.content).toContain('PullRequestReview_ThreadsArgs');
});
});

describe('Config', () => {
Expand Down
51 changes: 12 additions & 39 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3450,13 +3450,6 @@ auto-bind@2.0.0:
resolved "https://registry.yarnpkg.com/auto-bind/-/auto-bind-2.0.0.tgz#9a55a461b521f58daf955169203bed1a07a970a9"
integrity sha512-rvRBv0/O7iriUMqSzTDhAfyAD1vVnElAEruo5rMSFeYLA0iKDEzLPSJiwMnL86+IPpTlhfOIAzjoKZ9TaySYdA==

auto-parse@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/auto-parse/-/auto-parse-1.7.0.tgz#7dd7f9dd4bd921744594c95c52ec4bc213a7c3b9"
integrity sha512-PLNPNt2gNRqIom895AsEeUo1HcDjfhDvxHW2pr+pTkTrI9CauqCD5AvYSEJ6tieES0Rb/88HK9+0m83uJNS2zw==
dependencies:
typpy "2.3.11"

autolinker@~0.15.0:
version "0.15.3"
resolved "https://registry.yarnpkg.com/autolinker/-/autolinker-0.15.3.tgz#342417d8f2f3461b14cf09088d5edf8791dc9832"
Expand Down Expand Up @@ -6161,7 +6154,7 @@ dir-glob@2.0.0:
arrify "^1.0.1"
path-type "^3.0.0"

dkim-signer@^0.2.2:
dkim-signer@0.2.2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/dkim-signer/-/dkim-signer-0.2.2.tgz#aa81ec071eeed3622781baa922044d7800e5f308"
integrity sha1-qoHsBx7u02IngbqpIgRNeADl8wg=
Expand Down Expand Up @@ -7560,13 +7553,6 @@ function-bind@^1.1.1:
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d"
integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==

function.name@^1.0.3:
version "1.0.12"
resolved "https://registry.yarnpkg.com/function.name/-/function.name-1.0.12.tgz#34eec84476d9fb67977924a4cdcb98ec85695726"
integrity sha512-C7Tu+rAFrWW5RjXqtKtXp2xOdCujq+4i8ZH3w0uz/xrYHBwXZrPt96x8cDAEHrIjeyEv/Jm6iDGyqupbaVQTlw==
dependencies:
noop6 "^1.0.1"

functional-red-black-tree@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
Expand Down Expand Up @@ -10830,7 +10816,7 @@ magic-string@^0.25.2:
dependencies:
sourcemap-codec "^1.4.4"

mailcomposer@^3.12.0:
mailcomposer@3.12.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/mailcomposer/-/mailcomposer-3.12.0.tgz#9c5e1188aa8e1c62ec8b86bd43468102b639e8f9"
integrity sha1-nF4RiKqOHGLsi4a9Q0aBArY56Pk=
Expand Down Expand Up @@ -11443,10 +11429,10 @@ neo-async@^2.5.0, neo-async@^2.6.0:
resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.1.tgz#ac27ada66167fa8849a6addd837f6b189ad2081c"
integrity sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw==

netlify-lambda@1.4.13:
version "1.4.13"
resolved "https://registry.yarnpkg.com/netlify-lambda/-/netlify-lambda-1.4.13.tgz#a324615133b8f1a3aaeec8c5dfc825cf07aaddf1"
integrity sha512-f8CeAt8iGeORFn6fcNn3UXNSjGPL8vWaLm3yZH6I7txENOOdKMp7SdwvNL5YEN3s5h9+GAQ05qy3Ems0nle2JQ==
netlify-lambda@1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/netlify-lambda/-/netlify-lambda-1.5.0.tgz#52b1a16af73b3eeb1dee3c6178f189f010dd6450"
integrity sha512-Np1+dGu850o5qVzVlUlvuLD7M2QzL9HuH0BqKZisXtFPz8SYLv7Ulyj1GNYnUfBBa6SnZAQ+wU4L4u3laGm7Ww==
dependencies:
"@babel/core" "^7.0.0"
"@babel/plugin-proposal-class-properties" "^7.0.0"
Expand Down Expand Up @@ -11613,11 +11599,6 @@ noop-logger@^0.1.1:
resolved "https://registry.yarnpkg.com/noop-logger/-/noop-logger-0.1.1.tgz#94a2b1633c4f1317553007d8966fd0e841b6a4c2"
integrity sha1-lKKxYzxPExdVMAfYlm/Q6EG2pMI=

noop6@^1.0.1:
version "1.0.8"
resolved "https://registry.yarnpkg.com/noop6/-/noop6-1.0.8.tgz#eff06e2e5b3621e9e5618f389d6a2294f76e64ad"
integrity sha512-+Al5csMVc40I8xRfJsyBcN1IbpyvebOuQmMfxdw+AL6ECELey12ANgNTRhMfTwNIDU4W9W0g8EHLcsb3+3qPFA==

"nopt@2 || 3":
version "3.0.6"
resolved "https://registry.yarnpkg.com/nopt/-/nopt-3.0.6.tgz#c6465dbf08abcd4db359317f79ac68a646b28ff9"
Expand Down Expand Up @@ -15085,14 +15066,13 @@ send@0.17.1:
range-parser "~1.2.1"
statuses "~1.5.0"

sendmail@1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/sendmail/-/sendmail-1.6.0.tgz#8cc192cb8e2e88b393cd41c4c1207d50b131d29c"
integrity sha512-2lnZZNvejv+F0UdNsOF9fE4DWUx16ijd+47D5tl3dOSp+hpAHipr56UUBzYfx+r38GRarhWNZRpFhPAf3pZ7Kg==
sendmail@1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/sendmail/-/sendmail-1.6.1.tgz#6be92fb4be70d1d9ad102030aeb1e737bd512159"
integrity sha512-lIhvnjSi5e5jL8wA1GPP6j2QVlx6JOEfmdn0QIfmuJdmXYGmJ375kcOU0NSm/34J+nypm4sa1AXrYE5w3uNIIA==
dependencies:
auto-parse "^1.7.0"
dkim-signer "^0.2.2"
mailcomposer "^3.12.0"
dkim-signer "0.2.2"
mailcomposer "3.12.0"

sentence-case@^2.1.0:
version "2.1.1"
Expand Down Expand Up @@ -16483,13 +16463,6 @@ typescript@>=2.8.3:
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.5.1.tgz#ba72a6a600b2158139c5dd8850f700e231464202"
integrity sha512-64HkdiRv1yYZsSe4xC1WVgamNigVYjlssIoaH2HcZF0+ijsk5YK2g0G34w9wJkze8+5ow4STd22AynfO6ZYYLw==

typpy@2.3.11:
version "2.3.11"
resolved "https://registry.yarnpkg.com/typpy/-/typpy-2.3.11.tgz#21a0d22c96fb646306e08b6c669ad43608e1b3b9"
integrity sha512-Jh/fykZSaxeKO0ceMAs6agki9T5TNA9kiIR6fzKbvafKpIw8UlNlHhzuqKyi5lfJJ5VojJOx9tooIbyy7vHV/g==
dependencies:
function.name "^1.0.3"

uglify-js@3.4.x:
version "3.4.10"
resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.4.10.tgz#9ad9563d8eb3acdfb8d38597d2af1d815f6a755f"
Expand Down

0 comments on commit 0e67020

Please sign in to comment.