Skip to content

Commit 02c16ca

Browse files
committed
Make unknown instead of any the default scalar type (#10566)
* Make default scalar unknown, instead of any * Set up defaultScalarType tests * Add changeset * Revert unrelated null | undefined changes * Fix test description * Add a test case for custom scalar in dev-test
1 parent 3f5db91 commit 02c16ca

21 files changed

Lines changed: 403 additions & 87 deletions

File tree

.changeset/every-rooms-camp.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': major
3+
'@graphql-codegen/typescript-operations': major
4+
'@graphql-codegen/typescript': major
5+
'@graphql-codegen/typescript-resolvers': major
6+
'@graphql-codegen/client-preset': major
7+
---
8+
9+
BREAKING CHANGE: make `unknown` instead of `any` the default custom scalar type

dev-test/gql-tag-operations/gql/gql.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/
1515
*/
1616
type Documents = {
1717
'\n query Foo {\n Tweets {\n id\n }\n }\n': typeof types.FooDocument;
18-
'\n fragment Lel on Tweet {\n id\n body\n }\n': typeof types.LelFragmentDoc;
18+
'\n fragment Lel on Tweet {\n id\n body\n date\n }\n': typeof types.LelFragmentDoc;
1919
'\n query Bar {\n Tweets {\n ...Lel\n }\n }\n': typeof types.BarDocument;
2020
};
2121
const documents: Documents = {
2222
'\n query Foo {\n Tweets {\n id\n }\n }\n': types.FooDocument,
23-
'\n fragment Lel on Tweet {\n id\n body\n }\n': types.LelFragmentDoc,
23+
'\n fragment Lel on Tweet {\n id\n body\n date\n }\n': types.LelFragmentDoc,
2424
'\n query Bar {\n Tweets {\n ...Lel\n }\n }\n': types.BarDocument,
2525
};
2626

@@ -48,8 +48,8 @@ export function graphql(
4848
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
4949
*/
5050
export function graphql(
51-
source: '\n fragment Lel on Tweet {\n id\n body\n }\n'
52-
): (typeof documents)['\n fragment Lel on Tweet {\n id\n body\n }\n'];
51+
source: '\n fragment Lel on Tweet {\n id\n body\n date\n }\n'
52+
): (typeof documents)['\n fragment Lel on Tweet {\n id\n body\n date\n }\n'];
5353
/**
5454
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
5555
*/

dev-test/gql-tag-operations/gql/graphql.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export type FooQueryVariables = Exact<{ [key: string]: never }>;
66

77
export type FooQuery = { Tweets: Array<{ id: string } | null> | null };
88

9-
export type LelFragment = { id: string; body: string | null } & { ' $fragmentName'?: 'LelFragment' };
9+
export type LelFragment = { id: string; body: string | null; date: unknown | null } & {
10+
' $fragmentName'?: 'LelFragment';
11+
};
1012

1113
export type BarQueryVariables = Exact<{ [key: string]: never }>;
1214

@@ -24,6 +26,7 @@ export const LelFragmentDoc = {
2426
selections: [
2527
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
2628
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
29+
{ kind: 'Field', name: { kind: 'Name', value: 'date' } },
2730
],
2831
},
2932
},
@@ -82,6 +85,7 @@ export const BarDocument = {
8285
selections: [
8386
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
8487
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
88+
{ kind: 'Field', name: { kind: 'Name', value: 'date' } },
8589
],
8690
},
8791
},

dev-test/gql-tag-operations/graphql/gql.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/
1515
*/
1616
type Documents = {
1717
'\n query Foo {\n Tweets {\n id\n }\n }\n': typeof types.FooDocument;
18-
'\n fragment Lel on Tweet {\n id\n body\n }\n': typeof types.LelFragmentDoc;
18+
'\n fragment Lel on Tweet {\n id\n body\n date\n }\n': typeof types.LelFragmentDoc;
1919
'\n query Bar {\n Tweets {\n ...Lel\n }\n }\n': typeof types.BarDocument;
2020
};
2121
const documents: Documents = {
2222
'\n query Foo {\n Tweets {\n id\n }\n }\n': types.FooDocument,
23-
'\n fragment Lel on Tweet {\n id\n body\n }\n': types.LelFragmentDoc,
23+
'\n fragment Lel on Tweet {\n id\n body\n date\n }\n': types.LelFragmentDoc,
2424
'\n query Bar {\n Tweets {\n ...Lel\n }\n }\n': types.BarDocument,
2525
};
2626

@@ -48,8 +48,8 @@ export function graphql(
4848
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
4949
*/
5050
export function graphql(
51-
source: '\n fragment Lel on Tweet {\n id\n body\n }\n'
52-
): (typeof documents)['\n fragment Lel on Tweet {\n id\n body\n }\n'];
51+
source: '\n fragment Lel on Tweet {\n id\n body\n date\n }\n'
52+
): (typeof documents)['\n fragment Lel on Tweet {\n id\n body\n date\n }\n'];
5353
/**
5454
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
5555
*/

dev-test/gql-tag-operations/graphql/graphql.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ export type FooQueryVariables = Exact<{ [key: string]: never }>;
66

77
export type FooQuery = { Tweets: Array<{ id: string } | null> | null };
88

9-
export type LelFragment = { id: string; body: string | null } & { ' $fragmentName'?: 'LelFragment' };
9+
export type LelFragment = { id: string; body: string | null; date: unknown | null } & {
10+
' $fragmentName'?: 'LelFragment';
11+
};
1012

1113
export type BarQueryVariables = Exact<{ [key: string]: never }>;
1214

@@ -24,6 +26,7 @@ export const LelFragmentDoc = {
2426
selections: [
2527
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
2628
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
29+
{ kind: 'Field', name: { kind: 'Name', value: 'date' } },
2730
],
2831
},
2932
},
@@ -82,6 +85,7 @@ export const BarDocument = {
8285
selections: [
8386
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
8487
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
88+
{ kind: 'Field', name: { kind: 'Name', value: 'date' } },
8589
],
8690
},
8791
},

dev-test/gql-tag-operations/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const LelFragment = graphql(/* GraphQL */ `
1414
fragment Lel on Tweet {
1515
id
1616
body
17+
date
1718
}
1819
`);
1920

dev-test/test-schema/resolvers-federation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type Scalars = {
88
Boolean: { input: boolean; output: boolean };
99
Int: { input: number; output: number };
1010
Float: { input: number; output: number };
11-
_FieldSet: { input: any; output: any };
11+
_FieldSet: { input: unknown; output: unknown };
1212
};
1313

1414
export type Address = {

packages/graphql-codegen-cli/tests/codegen.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ describe('Codegen Executor', () => {
695695
Boolean: { input: boolean; output: boolean; }
696696
Int: { input: number; output: number; }
697697
Float: { input: number; output: number; }
698-
UniqueID: { input: any; output: any; }
698+
UniqueID: { input: unknown; output: unknown; }
699699
};`);
700700
});
701701
});

packages/plugins/other/visitor-plugin-common/src/base-visitor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface RawConfig {
7070
strictScalars?: boolean;
7171
/**
7272
* @description Allows you to override the type that unknown scalars will have.
73-
* @default any
73+
* @default unknown
7474
*
7575
* @exampleMarkdown
7676
* ```ts filename="codegen.ts"
@@ -82,7 +82,7 @@ export interface RawConfig {
8282
* 'path/to/file': {
8383
* // plugins...
8484
* config: {
85-
* defaultScalarType: 'unknown'
85+
* defaultScalarType: 'any'
8686
* },
8787
* },
8888
* },

packages/plugins/other/visitor-plugin-common/src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export function buildScalarsFromConfig(
269269
schema: GraphQLSchema | undefined,
270270
config: RawConfig,
271271
defaultScalarsMapping: NormalizedScalarsMap = DEFAULT_SCALARS,
272-
defaultScalarType = 'any'
272+
defaultScalarType = 'unknown'
273273
): ParsedScalarsMap {
274274
return buildScalars(
275275
schema,
@@ -283,7 +283,7 @@ export function buildScalars(
283283
schema: GraphQLSchema | undefined,
284284
scalarsMapping: ScalarsMap,
285285
defaultScalarsMapping: NormalizedScalarsMap = DEFAULT_SCALARS,
286-
defaultScalarType: string | null = 'any'
286+
defaultScalarType: string | null = 'unknown'
287287
): ParsedScalarsMap {
288288
const result: ParsedScalarsMap = {};
289289

0 commit comments

Comments
 (0)