Skip to content

Commit 22f6e4a

Browse files
fix(graphql-request): don't import print as type when supporting useTypeImports & rawRequest and documentMode is not a string (#7059)
* test: add failing test * fix(graphql-request): don't import `print` as type when supporting `useTypeImports` & `rawRequest` and `documentMode` is not a `string` * chore: add changeset
1 parent 759c4de commit 22f6e4a

File tree

4 files changed

+321
-1
lines changed

4 files changed

+321
-1
lines changed

‎.changeset/funny-vans-collect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-graphql-request': patch
3+
---
4+
5+
Don't import `print` as type when supporting `useTypeImports` & `rawRequest` and `documentMode` is not a `string`.

‎packages/plugins/typescript/graphql-request/src/visitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
4747
if (this.config.rawRequest) {
4848
this._additionalImports.push(`${typeImport} { GraphQLError } from 'graphql-request/dist/types';`);
4949
if (this.config.documentMode !== DocumentMode.string) {
50-
this._additionalImports.push(`${typeImport} { print } from 'graphql'`);
50+
this._additionalImports.push(`import { print } from 'graphql'`);
5151
}
5252
}
5353
}

‎packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,291 @@ async function test() {
842842
}"
843843
`;
844844
845+
exports[`graphql-request sdk Should not import print as type when supporting useTypeImports and rawRequest and documentMode = "documentNode" 1`] = `
846+
"export type Maybe<T> = T | null;
847+
export type InputMaybe<T> = Maybe<T>;
848+
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
849+
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
850+
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
851+
import type { GraphQLClient } from 'graphql-request';
852+
import type * as Dom from 'graphql-request/dist/types.dom';
853+
import type { GraphQLError } from 'graphql-request/dist/types';
854+
import { print } from 'graphql'
855+
import gql from 'graphql-tag';
856+
/** All built-in and custom scalars, mapped to their actual values */
857+
export type Scalars = {
858+
ID: string;
859+
String: string;
860+
Boolean: boolean;
861+
Int: number;
862+
Float: number;
863+
};
864+
865+
export type Query = {
866+
__typename?: 'Query';
867+
/** A feed of repository submissions */
868+
feed?: Maybe<Array<Maybe<Entry>>>;
869+
/** A single entry */
870+
entry?: Maybe<Entry>;
871+
/** Return the currently logged in user, or null if nobody is logged in */
872+
currentUser?: Maybe<User>;
873+
};
874+
875+
876+
export type QueryFeedArgs = {
877+
type: FeedType;
878+
offset?: InputMaybe<Scalars['Int']>;
879+
limit?: InputMaybe<Scalars['Int']>;
880+
};
881+
882+
883+
export type QueryEntryArgs = {
884+
repoFullName: Scalars['String'];
885+
};
886+
887+
/** A list of options for the sort order of the feed */
888+
export enum FeedType {
889+
/** Sort by a combination of freshness and score, using Reddit's algorithm */
890+
Hot = 'HOT',
891+
/** Newest entries first */
892+
New = 'NEW',
893+
/** Highest score entries first */
894+
Top = 'TOP'
895+
}
896+
897+
/** Information about a GitHub repository submitted to GitHunt */
898+
export type Entry = {
899+
__typename?: 'Entry';
900+
/** Information about the repository from GitHub */
901+
repository: Repository;
902+
/** The GitHub user who submitted this entry */
903+
postedBy: User;
904+
/** A timestamp of when the entry was submitted */
905+
createdAt: Scalars['Float'];
906+
/** The score of this repository, upvotes - downvotes */
907+
score: Scalars['Int'];
908+
/** The hot score of this repository */
909+
hotScore: Scalars['Float'];
910+
/** Comments posted about this repository */
911+
comments: Array<Maybe<Comment>>;
912+
/** The number of comments posted about this repository */
913+
commentCount: Scalars['Int'];
914+
/** The SQL ID of this entry */
915+
id: Scalars['Int'];
916+
/** XXX to be changed */
917+
vote: Vote;
918+
};
919+
920+
921+
/** Information about a GitHub repository submitted to GitHunt */
922+
export type EntryCommentsArgs = {
923+
limit?: InputMaybe<Scalars['Int']>;
924+
offset?: InputMaybe<Scalars['Int']>;
925+
};
926+
927+
/**
928+
* A repository object from the GitHub API. This uses the exact field names returned by the
929+
* GitHub API for simplicity, even though the convention for GraphQL is usually to camel case.
930+
*/
931+
export type Repository = {
932+
__typename?: 'Repository';
933+
/** Just the name of the repository, e.g. GitHunt-API */
934+
name: Scalars['String'];
935+
/** The full name of the repository with the username, e.g. apollostack/GitHunt-API */
936+
full_name: Scalars['String'];
937+
/** The description of the repository */
938+
description?: Maybe<Scalars['String']>;
939+
/** The link to the repository on GitHub */
940+
html_url: Scalars['String'];
941+
/** The number of people who have starred this repository on GitHub */
942+
stargazers_count: Scalars['Int'];
943+
/** The number of open issues on this repository on GitHub */
944+
open_issues_count?: Maybe<Scalars['Int']>;
945+
/** The owner of this repository on GitHub, e.g. apollostack */
946+
owner?: Maybe<User>;
947+
};
948+
949+
/** A user object from the GitHub API. This uses the exact field names returned from the GitHub API. */
950+
export type User = {
951+
__typename?: 'User';
952+
/** The name of the user, e.g. apollostack */
953+
login: Scalars['String'];
954+
/** The URL to a directly embeddable image for this user's avatar */
955+
avatar_url: Scalars['String'];
956+
/** The URL of this user's GitHub page */
957+
html_url: Scalars['String'];
958+
};
959+
960+
/** A comment about an entry, submitted by a user */
961+
export type Comment = {
962+
__typename?: 'Comment';
963+
/** The SQL ID of this entry */
964+
id: Scalars['Int'];
965+
/** The GitHub user who posted the comment */
966+
postedBy: User;
967+
/** A timestamp of when the comment was posted */
968+
createdAt: Scalars['Float'];
969+
/** The text of the comment */
970+
content: Scalars['String'];
971+
/** The repository which this comment is about */
972+
repoName: Scalars['String'];
973+
};
974+
975+
/** XXX to be removed */
976+
export type Vote = {
977+
__typename?: 'Vote';
978+
vote_value: Scalars['Int'];
979+
};
980+
981+
export type Mutation = {
982+
__typename?: 'Mutation';
983+
/** Submit a new repository, returns the new submission */
984+
submitRepository?: Maybe<Entry>;
985+
/** Vote on a repository submission, returns the submission that was voted on */
986+
vote?: Maybe<Entry>;
987+
/** Comment on a repository, returns the new comment */
988+
submitComment?: Maybe<Comment>;
989+
};
990+
991+
992+
export type MutationSubmitRepositoryArgs = {
993+
repoFullName: Scalars['String'];
994+
};
995+
996+
997+
export type MutationVoteArgs = {
998+
repoFullName: Scalars['String'];
999+
type: VoteType;
1000+
};
1001+
1002+
1003+
export type MutationSubmitCommentArgs = {
1004+
repoFullName: Scalars['String'];
1005+
commentContent: Scalars['String'];
1006+
};
1007+
1008+
/** The type of vote to record, when submitting a vote */
1009+
export enum VoteType {
1010+
Up = 'UP',
1011+
Down = 'DOWN',
1012+
Cancel = 'CANCEL'
1013+
}
1014+
1015+
export type Subscription = {
1016+
__typename?: 'Subscription';
1017+
/** Subscription fires on every comment added */
1018+
commentAdded?: Maybe<Comment>;
1019+
};
1020+
1021+
1022+
export type SubscriptionCommentAddedArgs = {
1023+
repoFullName: Scalars['String'];
1024+
};
1025+
export type FeedQueryVariables = Exact<{ [key: string]: never; }>;
1026+
1027+
1028+
export type FeedQuery = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number, commentCount: number, repository: { __typename?: 'Repository', owner?: { __typename?: 'User', avatar_url: string } | null | undefined } } | null | undefined> | null | undefined };
1029+
1030+
export type Feed2QueryVariables = Exact<{
1031+
v: Scalars['String'];
1032+
}>;
1033+
1034+
1035+
export type Feed2Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined };
1036+
1037+
export type Feed3QueryVariables = Exact<{
1038+
v?: InputMaybe<Scalars['String']>;
1039+
}>;
1040+
1041+
1042+
export type Feed3Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined };
1043+
1044+
export type Feed4QueryVariables = Exact<{
1045+
v?: Scalars['String'];
1046+
}>;
1047+
1048+
1049+
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined };
1050+
1051+
export const FeedDocument = gql\`
1052+
query feed {
1053+
feed {
1054+
id
1055+
commentCount
1056+
repository {
1057+
owner {
1058+
avatar_url
1059+
}
1060+
}
1061+
}
1062+
}
1063+
\`;
1064+
export const Feed2Document = gql\`
1065+
query feed2($v: String!) {
1066+
feed {
1067+
id
1068+
}
1069+
}
1070+
\`;
1071+
export const Feed3Document = gql\`
1072+
query feed3($v: String) {
1073+
feed {
1074+
id
1075+
}
1076+
}
1077+
\`;
1078+
export const Feed4Document = gql\`
1079+
query feed4($v: String! = \\"TEST\\") {
1080+
feed {
1081+
id
1082+
}
1083+
}
1084+
\`;
1085+
1086+
export type SdkFunctionWrapper = <T>(action: (requestHeaders?:Record<string, string>) => Promise<T>, operationName: string) => Promise<T>;
1087+
1088+
1089+
const defaultWrapper: SdkFunctionWrapper = (action, _operationName) => action();
1090+
const FeedDocumentString = print(FeedDocument);
1091+
const Feed2DocumentString = print(Feed2Document);
1092+
const Feed3DocumentString = print(Feed3Document);
1093+
const Feed4DocumentString = print(Feed4Document);
1094+
export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
1095+
return {
1096+
feed(variables?: FeedQueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: FeedQuery | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> {
1097+
return withWrapper((wrappedRequestHeaders) => client.rawRequest<FeedQuery>(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed');
1098+
},
1099+
feed2(variables: Feed2QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed2Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> {
1100+
return withWrapper((wrappedRequestHeaders) => client.rawRequest<Feed2Query>(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2');
1101+
},
1102+
feed3(variables?: Feed3QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed3Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> {
1103+
return withWrapper((wrappedRequestHeaders) => client.rawRequest<Feed3Query>(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3');
1104+
},
1105+
feed4(variables?: Feed4QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed4Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> {
1106+
return withWrapper((wrappedRequestHeaders) => client.rawRequest<Feed4Query>(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4');
1107+
}
1108+
};
1109+
}
1110+
export type Sdk = ReturnType<typeof getSdk>;
1111+
async function test() {
1112+
const Client = require('graphql-request').GraphQLClient;
1113+
const client = new Client('');
1114+
const sdk = getSdk(client);
1115+
1116+
await sdk.feed();
1117+
await sdk.feed3();
1118+
await sdk.feed4();
1119+
1120+
const result = await sdk.feed2({ v: \\"1\\" });
1121+
1122+
if (result.feed) {
1123+
if (result.feed[0]) {
1124+
const id = result.feed[0].id
1125+
}
1126+
}
1127+
}"
1128+
`;
1129+
8451130
exports[`graphql-request sdk Should support extensionType when rawRequest is true and documentMode = "DocumentNode" 1`] = `
8461131
"export type Maybe<T> = T | null;
8471132
export type InputMaybe<T> = Maybe<T>;

‎packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,36 @@ async function test() {
207207
208208
const result = await sdk.feed2({ v: "1" });
209209
210+
if (result.feed) {
211+
if (result.feed[0]) {
212+
const id = result.feed[0].id
213+
}
214+
}
215+
}`;
216+
const output = await validate(result, config, docs, schema, usage);
217+
218+
expect(output).toMatchSnapshot();
219+
});
220+
221+
it('Should not import print as type when supporting useTypeImports and rawRequest and documentMode = "documentNode"', async () => {
222+
const config = { rawRequest: true, useTypeImports: true };
223+
const docs = [{ location: '', document: basicDoc }];
224+
const result = (await plugin(schema, docs, config, {
225+
outputFile: 'graphql.ts',
226+
})) as Types.ComplexPluginOutput;
227+
228+
const usage = `
229+
async function test() {
230+
const Client = require('graphql-request').GraphQLClient;
231+
const client = new Client('');
232+
const sdk = getSdk(client);
233+
234+
await sdk.feed();
235+
await sdk.feed3();
236+
await sdk.feed4();
237+
238+
const result = await sdk.feed2({ v: "1" });
239+
210240
if (result.feed) {
211241
if (result.feed[0]) {
212242
const id = result.feed[0].id

0 commit comments

Comments
 (0)