Skip to content

Commit 2a33fc7

Browse files
ElvisUpUpelvishe
andauthored
change the client-preset generated template. (#8763)
* fix: client preset generated template fix * lint * prettier: fix prettier probelm * changeset: add changeset * test: pass dev-test Co-authored-by: elvishe <[email protected]>
1 parent dd0374e commit 2a33fc7

File tree

8 files changed

+239
-228
lines changed

8 files changed

+239
-228
lines changed

.changeset/real-ties-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/gql-tag-operations': minor
3+
---
4+
5+
change the client-preset generated template

dev-test/gql-tag-operations-masking-star-wars/gql/gql.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ const documents = {
1919
types.HeroDetailsFragmentDoc,
2020
};
2121

22-
/**
23-
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
24-
*/
25-
export function gql(
26-
source: '\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n'
27-
): typeof documents['\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n'];
28-
/**
29-
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
30-
*/
31-
export function gql(
32-
source: '\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n'
33-
): typeof documents['\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n'];
34-
3522
/**
3623
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
3724
*
@@ -46,6 +33,19 @@ export function gql(
4633
**/
4734
export function gql(source: string): unknown;
4835

36+
/**
37+
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
38+
*/
39+
export function gql(
40+
source: '\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n'
41+
): typeof documents['\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n'];
42+
/**
43+
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
44+
*/
45+
export function gql(
46+
source: '\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n'
47+
): typeof documents['\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n'];
48+
4949
export function gql(source: string) {
5050
return (documents as any)[source] ?? {};
5151
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,20 @@ const documents = {
2222
'\n query TweetAppQuery {\n ...TweetsFragment\n }\n': types.TweetAppQueryDocument,
2323
};
2424

25+
/**
26+
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
27+
*
28+
*
29+
* @example
30+
* ```ts
31+
* const query = gql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
32+
* ```
33+
*
34+
* The query argument is unknown!
35+
* Please regenerate the types.
36+
**/
37+
export function gql(source: string): unknown;
38+
2539
/**
2640
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
2741
*/
@@ -47,20 +61,6 @@ export function gql(
4761
source: '\n query TweetAppQuery {\n ...TweetsFragment\n }\n'
4862
): typeof documents['\n query TweetAppQuery {\n ...TweetsFragment\n }\n'];
4963

50-
/**
51-
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
52-
*
53-
*
54-
* @example
55-
* ```ts
56-
* const query = gql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
57-
* ```
58-
*
59-
* The query argument is unknown!
60-
* Please regenerate the types.
61-
**/
62-
export function gql(source: string): unknown;
63-
6464
export function gql(source: string) {
6565
return (documents as any)[source] ?? {};
6666
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ const documents = {
1818
'\n query Bar {\n Tweets {\n ...Lel\n }\n }\n': types.BarDocument,
1919
};
2020

21+
/**
22+
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
23+
*
24+
*
25+
* @example
26+
* ```ts
27+
* const query = gql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
28+
* ```
29+
*
30+
* The query argument is unknown!
31+
* Please regenerate the types.
32+
**/
33+
export function gql(source: string): unknown;
34+
2135
/**
2236
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
2337
*/
@@ -37,20 +51,6 @@ export function gql(
3751
source: '\n query Bar {\n Tweets {\n ...Lel\n }\n }\n'
3852
): typeof documents['\n query Bar {\n Tweets {\n ...Lel\n }\n }\n'];
3953

40-
/**
41-
* The gql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
42-
*
43-
*
44-
* @example
45-
* ```ts
46-
* const query = gql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
47-
* ```
48-
*
49-
* The query argument is unknown!
50-
* Please regenerate the types.
51-
**/
52-
export function gql(source: string): unknown;
53-
5454
export function gql(source: string) {
5555
return (documents as any)[source] ?? {};
5656
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ const documents = {
1818
'\n query Bar {\n Tweets {\n ...Lel\n }\n }\n': types.BarDocument,
1919
};
2020

21+
/**
22+
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
23+
*
24+
*
25+
* @example
26+
* ```ts
27+
* const query = gql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
28+
* ```
29+
*
30+
* The query argument is unknown!
31+
* Please regenerate the types.
32+
**/
33+
export function graphql(source: string): unknown;
34+
2135
/**
2236
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
2337
*/
@@ -37,20 +51,6 @@ export function graphql(
3751
source: '\n query Bar {\n Tweets {\n ...Lel\n }\n }\n'
3852
): typeof documents['\n query Bar {\n Tweets {\n ...Lel\n }\n }\n'];
3953

40-
/**
41-
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
42-
*
43-
*
44-
* @example
45-
* ```ts
46-
* const query = gql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
47-
* ```
48-
*
49-
* The query argument is unknown!
50-
* Please regenerate the types.
51-
**/
52-
export function graphql(source: string): unknown;
53-
5454
export function graphql(source: string) {
5555
return (documents as any)[source] ?? {};
5656
}

packages/plugins/typescript/gql-tag-operations/src/index.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ export const plugin: PluginFunction<{
4343
];
4444

4545
if (sourcesWithOperations.length > 0) {
46-
code.push(
47-
[
48-
...getDocumentRegistryChunk(sourcesWithOperations),
49-
`\n`,
50-
...getGqlOverloadChunk(sourcesWithOperations, gqlTagName, 'lookup', emitLegacyCommonJSImports),
51-
].join('')
52-
);
46+
code.push([...getDocumentRegistryChunk(sourcesWithOperations)].join(''));
5347
} else {
5448
code.push('const documents = [];');
5549
}
@@ -67,13 +61,25 @@ export const plugin: PluginFunction<{
6761
`**/\n`,
6862
`export function ${gqlTagName}(source: string): unknown;\n`,
6963
`\n`,
64+
].join('')
65+
);
66+
67+
if (sourcesWithOperations.length > 0) {
68+
code.push(
69+
[...getGqlOverloadChunk(sourcesWithOperations, gqlTagName, 'lookup', emitLegacyCommonJSImports), `\n`].join('')
70+
);
71+
}
72+
73+
code.push(
74+
[
7075
`export function ${gqlTagName}(source: string) {\n`,
7176
` return (documents as any)[source] ?? {};\n`,
7277
`}\n`,
7378
`\n`,
7479
...documentTypePartial,
7580
].join('')
7681
);
82+
7783
return code.join('');
7884
}
7985

0 commit comments

Comments
 (0)