Skip to content

Commit

Permalink
adding allowUndefinedQueryVariables option to typescript-operations p…
Browse files Browse the repository at this point in the history
…lugin (#9652)

* adding allowUndefinedQueryVariables option to typescript-operations plugin

* adding changeset

* Update packages/plugins/typescript/operations/tests/ts-documents.spec.ts

---------

Co-authored-by: Saihajpreet Singh <saihajpreet.singh@gmail.com>
  • Loading branch information
gmurphey and saihaj committed Feb 20, 2024
1 parent d7c7254 commit 920b443
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/lazy-laws-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': minor
'@graphql-codegen/typescript-operations': minor
---

Added allowUndefinedQueryVariables as config option
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ export class BaseDocumentsVisitor<
.join('\n\n');
}

protected applyVariablesWrapper(variablesBlock: string): string {
protected applyVariablesWrapper(variablesBlock: string, _operationType?: string): string {
return variablesBlock;
}

Expand Down Expand Up @@ -294,7 +294,7 @@ export class BaseDocumentsVisitor<

const operationVariables = new DeclarationBlock({
...this._declarationBlockConfig,
blockTransformer: t => this.applyVariablesWrapper(t),
blockTransformer: t => this.applyVariablesWrapper(t, operationType),
})
.export()
.asKind('type')
Expand Down
25 changes: 25 additions & 0 deletions packages/plugins/typescript/operations/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,29 @@ export interface TypeScriptDocumentsPluginConfig extends RawDocumentsConfig {
* ```
*/
maybeValue?: string;

/**
* @description Adds undefined as a possible type for query variables
* @default false
*
* @exampleMarkdown
* ```ts filename="codegen.ts"
* import type { CodegenConfig } from '@graphql-codegen/cli';
*
* const config: CodegenConfig = {
* // ...
* generates: {
* 'path/to/file.ts': {
* plugins: ['typescript'],
* config: {
* allowUndefinedQueryVariables: true
* },
* },
* },
* };
* export default config;
* ```
*/

allowUndefinedQueryVariables?: boolean;
}
7 changes: 5 additions & 2 deletions packages/plugins/typescript/operations/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface TypeScriptDocumentsParsedConfig extends ParsedDocumentsConfig {
immutableTypes: boolean;
noExport: boolean;
maybeValue: string;
allowUndefinedQueryVariables: boolean;
}

export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
Expand All @@ -41,6 +42,7 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
nonOptionalTypename: getConfigValue(config.nonOptionalTypename, false),
preResolveTypes: getConfigValue(config.preResolveTypes, true),
mergeFragmentTypes: getConfigValue(config.mergeFragmentTypes, false),
allowUndefinedQueryVariables: getConfigValue(config.allowUndefinedQueryVariables, false),
} as TypeScriptDocumentsParsedConfig,
schema
);
Expand Down Expand Up @@ -134,9 +136,10 @@ export class TypeScriptDocumentsVisitor extends BaseDocumentsVisitor<
return ';';
}

protected applyVariablesWrapper(variablesBlock: string): string {
protected applyVariablesWrapper(variablesBlock: string, operationType: string): string {
const prefix = this.config.namespacedImportName ? `${this.config.namespacedImportName}.` : '';
const extraType = this.config.allowUndefinedQueryVariables && operationType === 'Query' ? ' | undefined' : '';

return `${prefix}Exact<${variablesBlock === '{}' ? `{ [key: string]: never; }` : variablesBlock}>`;
return `${prefix}Exact<${variablesBlock === '{}' ? `{ [key: string]: never; }` : variablesBlock}>${extraType}`;
}
}
46 changes: 46 additions & 0 deletions packages/plugins/typescript/operations/tests/ts-documents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,52 @@ describe('TypeScript Operations Plugin', () => {
export type UserQuery = { __typename?: 'Query', user: { __typename?: 'User', name: string, age?: number | 'specialType', address?: string, nicknames?: Array<string> | 'specialType', parents?: Array<User> } };
`);
});

it('should add undefined as possible value according to allowUndefinedQueryVariables', async () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
user: User!
}
type User {
name: String!
age: Int
address: String!
nicknames: [String!]
parents: [User!]!
}
`);

const fragment = parse(/* GraphQL */ `
query user($showProperty: Boolean!) {
user {
name
age
address @include(if: $showProperty)
nicknames @include(if: $showProperty)
parents @include(if: $showProperty)
}
}
`);

const { content } = await plugin(
schema,
[{ location: '', document: fragment }],
{
preResolveTypes: true,
allowUndefinedQueryVariables: true,
},
{
outputFile: 'graphql.ts',
}
);

expect(content).toBeSimilarStringTo(`
export type UserQueryVariables = Exact<{
showProperty: Scalars['Boolean']['input'];
}> | undefined;
`);
});
});

describe('Scalars', () => {
Expand Down

0 comments on commit 920b443

Please sign in to comment.