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

adding allowUndefinedQueryVariables option to typescript-operations plugin #9652

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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 @@ -257,7 +257,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 @@ -288,7 +288,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 @@ -133,9 +135,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.only('should add undefined as possible value according to allowUndefinedQueryVariables', async () => {
saihaj marked this conversation as resolved.
Show resolved Hide resolved
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