Skip to content

Commit f101c09

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 2d6f23e commit f101c09

21 files changed

Lines changed: 399 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 * as types from './graphql.js';
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type FooQueryVariables = Exact<{ [key: string]: never }>;
99

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

12-
export type LelFragment = { id: string; body: string | null } & {
12+
export type LelFragment = { id: string; body: string | null; date: unknown | null } & {
1313
' $fragmentName'?: 'LelFragment';
1414
};
1515

@@ -31,6 +31,7 @@ export const LelFragmentDoc = {
3131
selections: [
3232
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
3333
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
34+
{ kind: 'Field', name: { kind: 'Name', value: 'date' } },
3435
],
3536
},
3637
},
@@ -89,6 +90,7 @@ export const BarDocument = {
8990
selections: [
9091
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
9192
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
93+
{ kind: 'Field', name: { kind: 'Name', value: 'date' } },
9294
],
9395
},
9496
},

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 * as types from './graphql.js';
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type FooQueryVariables = Exact<{ [key: string]: never }>;
99

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

12-
export type LelFragment = { id: string; body: string | null } & {
12+
export type LelFragment = { id: string; body: string | null; date: unknown | null } & {
1313
' $fragmentName'?: 'LelFragment';
1414
};
1515

@@ -31,6 +31,7 @@ export const LelFragmentDoc = {
3131
selections: [
3232
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
3333
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
34+
{ kind: 'Field', name: { kind: 'Name', value: 'date' } },
3435
],
3536
},
3637
},
@@ -89,6 +90,7 @@ export const BarDocument = {
8990
selections: [
9091
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
9192
{ kind: 'Field', name: { kind: 'Name', value: 'body' } },
93+
{ kind: 'Field', name: { kind: 'Name', value: 'date' } },
9294
],
9395
},
9496
},

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
@@ -9,7 +9,7 @@ export type Scalars = {
99
Boolean: { input: boolean; output: boolean };
1010
Int: { input: number; output: number };
1111
Float: { input: number; output: number };
12-
_FieldSet: { input: any; output: any };
12+
_FieldSet: { input: unknown; output: unknown };
1313
};
1414

1515
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
@@ -774,7 +774,7 @@ describe('Codegen Executor', () => {
774774
Boolean: { input: boolean; output: boolean; }
775775
Int: { input: number; output: number; }
776776
Float: { input: number; output: number; }
777-
UniqueID: { input: any; output: any; }
777+
UniqueID: { input: unknown; output: unknown; }
778778
};`);
779779
});
780780
});

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
@@ -291,7 +291,7 @@ export function buildScalarsFromConfig(
291291
schema: GraphQLSchema | undefined,
292292
config: RawConfig,
293293
defaultScalarsMapping: NormalizedScalarsMap = DEFAULT_SCALARS,
294-
defaultScalarType = 'any',
294+
defaultScalarType = 'unknown',
295295
): ParsedScalarsMap {
296296
return buildScalars(
297297
schema,
@@ -305,7 +305,7 @@ export function buildScalars(
305305
schema: GraphQLSchema | undefined,
306306
scalarsMapping: ScalarsMap,
307307
defaultScalarsMapping: NormalizedScalarsMap = DEFAULT_SCALARS,
308-
defaultScalarType: string | null = 'any',
308+
defaultScalarType: string | null = 'unknown',
309309
): ParsedScalarsMap {
310310
const result: ParsedScalarsMap = {};
311311

0 commit comments

Comments
 (0)