Skip to content

Commit 5be88d7

Browse files
committed
added utils types, fix signature
update README, update examples
1 parent 32e9aab commit 5be88d7

File tree

8 files changed

+4114
-29
lines changed

8 files changed

+4114
-29
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ yarn add -D @graphql-typed-document-node/patch-cli
100100

101101
Now, after installing your projects' dependencies, it will make sure to patch all relevant packages and make it available for use with `TypedDocumentNode`.
102102

103+
## Utils
104+
105+
The `core` package of typed-document-node exports 3 types only:
106+
107+
* `TypedDocumentNode` - the base of this library.
108+
* `ResultOf` - a utils for extracting the result type from an existing `TypeDocumentNode` instance (`ResultOf<typeof MyQueryDocument>`)
109+
* `VariablesOf` - a utils for extracting the variables type from an existing `TypeDocumentNode` instance (`VariablesOf<typeof MyQueryDocument>`)
110+
103111
## How can I support this in my library?
104112

105113
If you are a library maintainer, and you wish to have built-in TS support in your library, you can add support for `TypedDocumentNode` without having any breaking changes to your API.

examples/apollo-client-3/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
"compile": "tsc"
1010
},
1111
"dependencies": {
12-
"@types/node": "14.0.23",
12+
"@apollo/client": "3.1.3",
13+
"@types/node": "14.0.27",
1314
"graphql": "15.3.0",
1415
"typescript": "3.9.7",
15-
"@apollo/client": "3.1.2",
16-
"@graphql-typed-document-node/core": "3.0.0",
17-
"@graphql-typed-document-node/patch-cli": "3.0.0",
18-
"@graphql-codegen/typed-document-node": "1.17.7",
19-
"@graphql-codegen/cli": "1.17.7",
20-
"@graphql-codegen/typescript": "1.17.7",
21-
"@graphql-codegen/typescript-operations": "1.17.7"
16+
"@graphql-typed-document-node/core": "^3.1.0",
17+
"@graphql-typed-document-node/patch-cli": "^3.0.3",
18+
"@graphql-codegen/typed-document-node": "1.17.9",
19+
"@graphql-codegen/cli": "1.17.8",
20+
"@graphql-codegen/typescript": "1.17.8",
21+
"@graphql-codegen/typescript-operations": "1.17.8"
2222
}
2323
}

examples/graphql/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import { readFileSync } from 'fs';
55
const schema = buildSchema(readFileSync('./schema.graphql', 'utf-8'));
66

77
async function main(): Promise<void> {
8-
// We are using the regular `execute` from `graphql` library, and we loaded an overloading
9-
// module using `tsconfig.json` => compilerOptions.types. This makes sure to add support for
10-
// the new TypedDocumentNode.
8+
// We are using the regular `execute` from `graphql` library, but with patch applied.
119
const result = await execute({
12-
document: RatesDocument, // This is the TypedDocumentNode containting the result type
10+
document: RatesDocument, // This is the TypedDocumentNode containting the result type and variables type
1311
schema,
1412
variableValues: {
1513
currency: 'USD'
1614
}
1715
});
1816

17+
result.data.rates
18+
1919
// We now have types support and auto complete for the
2020
// result type, just by passing `RatesDocument` as `document` to execute function.
2121
const rates = result.data.rates;

examples/graphql/package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
"compile": "tsc"
1010
},
1111
"dependencies": {
12-
"@types/node": "14.0.23",
12+
"@types/node": "14.0.27",
1313
"graphql": "15.3.0",
1414
"typescript": "3.9.7",
15-
"@graphql-typed-document-node/core": "3.0.0",
16-
"@graphql-typed-document-node/patch-cli": "3.0.0",
17-
"@graphql-codegen/typed-document-node": "1.17.7",
18-
"@graphql-codegen/cli": "1.17.7",
19-
"@graphql-codegen/typescript": "1.17.7",
20-
"@graphql-codegen/typescript-operations": "1.17.7"
15+
"@graphql-typed-document-node/core": "^3.1.0",
16+
"@graphql-typed-document-node/patch-cli": "^3.0.3",
17+
"@graphql-codegen/typed-document-node": "1.17.9",
18+
"@graphql-codegen/cli": "1.17.8",
19+
"@graphql-codegen/typescript": "1.17.8",
20+
"@graphql-codegen/typescript-operations": "1.17.8"
2121
}
2222
}

examples/graphql/typed-document-nodes.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { DocumentNode } from 'graphql';
2-
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
1+
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
32
export type Maybe<T> = T | null;
4-
export type Exact<T extends { [key: string]: any }> = { [K in keyof T]: T[K] };
3+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
54
/** All built-in and custom scalars, mapped to their actual values */
65
export type Scalars = {
76
ID: string;
@@ -35,11 +34,13 @@ export type RatesQueryVariables = Exact<{
3534

3635
export type RatesQuery = (
3736
{ __typename?: 'Query' }
38-
& { rates?: Maybe<Array<Maybe<(
39-
{ __typename?: 'ExchangeRate' }
40-
& Pick<ExchangeRate, 'currency' | 'rate'>
41-
)>>> }
37+
& {
38+
rates?: Maybe<Array<Maybe<(
39+
{ __typename?: 'ExchangeRate' }
40+
& Pick<ExchangeRate, 'currency' | 'rate'>
41+
)>>>
42+
}
4243
);
4344

4445

45-
export const RatesDocument: TypedDocumentNode<RatesQuery, RatesQueryVariables> = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"rates"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"currency"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}},"directives":[]}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"rates"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"currency"},"value":{"kind":"Variable","name":{"kind":"Name","value":"currency"}}}],"directives":[],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"currency"},"arguments":[],"directives":[]},{"kind":"Field","name":{"kind":"Name","value":"rate"},"arguments":[],"directives":[]}]}}]}}]};
46+
export const RatesDocument: DocumentNode<RatesQuery, RatesQueryVariables> = { "kind": "Document", "definitions": [{ "kind": "OperationDefinition", "operation": "query", "name": { "kind": "Name", "value": "rates" }, "variableDefinitions": [{ "kind": "VariableDefinition", "variable": { "kind": "Variable", "name": { "kind": "Name", "value": "currency" } }, "type": { "kind": "NonNullType", "type": { "kind": "NamedType", "name": { "kind": "Name", "value": "String" } } }, "directives": [] }], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "rates" }, "arguments": [{ "kind": "Argument", "name": { "kind": "Name", "value": "currency" }, "value": { "kind": "Variable", "name": { "kind": "Name", "value": "currency" } } }], "directives": [], "selectionSet": { "kind": "SelectionSet", "selections": [{ "kind": "Field", "name": { "kind": "Name", "value": "currency" }, "arguments": [], "directives": [] }, { "kind": "Field", "name": { "kind": "Name", "value": "rate" }, "arguments": [], "directives": [] }] } }] } }] };

0 commit comments

Comments
 (0)