From b7c71858cd0aa36a3779b1eab76013b61e3854aa Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Tue, 6 Feb 2024 15:04:20 +0100 Subject: [PATCH 1/4] test(graphql): add TypedDocumentNode inference tests --- test/typings/graphql-typed-document.test-d.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 test/typings/graphql-typed-document.test-d.ts diff --git a/test/typings/graphql-typed-document.test-d.ts b/test/typings/graphql-typed-document.test-d.ts new file mode 100644 index 000000000..090b12841 --- /dev/null +++ b/test/typings/graphql-typed-document.test-d.ts @@ -0,0 +1,44 @@ +import { graphql, HttpResponse } from 'msw' +import type { TypedDocumentNode } from '../../src/core/graphql' + +const GetUserQuery = {} as TypedDocumentNode< + { + user: { + name: string + } + }, + { userId: string } +> + +graphql.query(GetUserQuery, ({ variables }) => { + variables.userId.toUpperCase() + + return HttpResponse.json({ + data: { + user: { name: 'John' }, + }, + }) +}) + +graphql.query(GetUserQuery, ({ variables }) => { + // @ts-expect-error Unknown variable + variables.unknownVariable + + return HttpResponse.json({ + data: { + user: { name: 'John' }, + }, + }) +}) + +graphql.query( + GetUserQuery, + // @ts-expect-error Invalid response type. + () => { + return HttpResponse.json({ + data: { + user: { name: 123 }, + }, + }) + }, +) From debd5a32b0d396003bf56d6ec34a6513657b454f Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Tue, 6 Feb 2024 15:10:09 +0100 Subject: [PATCH 2/4] chore: add "dom.iterable" to tsconfig in type tests --- test/typings/tsconfig.4.7.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/typings/tsconfig.4.7.json b/test/typings/tsconfig.4.7.json index 9bea8415d..705ddd91c 100644 --- a/test/typings/tsconfig.4.7.json +++ b/test/typings/tsconfig.4.7.json @@ -1,6 +1,7 @@ { "extends": "./tsconfig.json", "compilerOptions": { - "moduleResolution": "Node16" + "moduleResolution": "Node16", + "lib": ["DOM", "DOM.Iterable"] } } From 3aac416f3099e5360d3bca0e8d206401b7661b9f Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Tue, 6 Feb 2024 15:12:37 +0100 Subject: [PATCH 3/4] test: use "@graphql-typed-document-node/core" for testing --- package.json | 1 + pnpm-lock.yaml | 11 ++++++++++- test/typings/graphql-typed-document.test-d.ts | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 9d9bdf6aa..c5e2d299f 100644 --- a/package.json +++ b/package.json @@ -134,6 +134,7 @@ "devDependencies": { "@commitlint/cli": "^18.4.4", "@commitlint/config-conventional": "^18.4.4", + "@graphql-typed-document-node/core": "^3.2.0", "@open-draft/test-server": "^0.4.2", "@ossjs/release": "^0.8.0", "@playwright/test": "^1.40.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f70a7215f..508a9f882 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,7 @@ specifiers: '@bundled-es-modules/statuses': ^1.0.1 '@commitlint/cli': ^18.4.4 '@commitlint/config-conventional': ^18.4.4 + '@graphql-typed-document-node/core': ^3.2.0 '@mswjs/cookies': ^1.1.0 '@mswjs/interceptors': ^0.25.15 '@open-draft/test-server': ^0.4.2 @@ -92,6 +93,7 @@ dependencies: devDependencies: '@commitlint/cli': 18.4.4_z5x4t2owsiiyng6cl4yau77uc4 '@commitlint/config-conventional': 18.4.4 + '@graphql-typed-document-node/core': 3.2.0_graphql@16.8.1 '@open-draft/test-server': 0.4.2 '@ossjs/release': 0.8.0 '@playwright/test': 1.40.1 @@ -811,6 +813,14 @@ packages: engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dev: true + /@graphql-typed-document-node/core/3.2.0_graphql@16.8.1: + resolution: {integrity: sha512-mB9oAsNCm9aM3/SOv4YtBMqZbYj10R7dkq8byBqxGY/ncFwhf2oQzMV+LCRlWoDSEBJ3COiR1yeDvMtsoOsuFQ==} + peerDependencies: + graphql: ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0 + dependencies: + graphql: 16.8.1 + dev: true + /@humanwhocodes/config-array/0.11.14: resolution: {integrity: sha512-3T8LkOmg45BV5FICb15QQMsyUSWrQ8AygVfC7ZG32zOalnqrilm018ZVCw0eapXux8FtA33q8PSRSstjee3jSg==} engines: {node: '>=10.10.0'} @@ -4325,7 +4335,6 @@ packages: /graphql/16.8.1: resolution: {integrity: sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw==} engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0} - dev: false /hard-rejection/2.1.0: resolution: {integrity: sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==} diff --git a/test/typings/graphql-typed-document.test-d.ts b/test/typings/graphql-typed-document.test-d.ts index 090b12841..6131c174f 100644 --- a/test/typings/graphql-typed-document.test-d.ts +++ b/test/typings/graphql-typed-document.test-d.ts @@ -1,5 +1,5 @@ import { graphql, HttpResponse } from 'msw' -import type { TypedDocumentNode } from '../../src/core/graphql' +import { TypedDocumentNode } from '@graphql-typed-document-node/core' const GetUserQuery = {} as TypedDocumentNode< { From 8ff7ce4b1bd6f1bacabd9ac93860850e4866b20d Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Tue, 6 Feb 2024 15:30:51 +0100 Subject: [PATCH 4/4] test(graphql): use a narrower "user.name" type --- test/typings/graphql-typed-document.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/typings/graphql-typed-document.test-d.ts b/test/typings/graphql-typed-document.test-d.ts index 6131c174f..c8d3ad425 100644 --- a/test/typings/graphql-typed-document.test-d.ts +++ b/test/typings/graphql-typed-document.test-d.ts @@ -4,7 +4,7 @@ import { TypedDocumentNode } from '@graphql-typed-document-node/core' const GetUserQuery = {} as TypedDocumentNode< { user: { - name: string + name: 'John' } }, { userId: string }