From 3b87f049a7418152f98be26644e78e61df0a0594 Mon Sep 17 00:00:00 2001 From: Edoardo Morandi Date: Fri, 5 Nov 2021 08:30:55 +0100 Subject: [PATCH] allow customisation for extensions type (#6969) * Allow customization for extensions type This introduces a new configuration for the `graphql-request` plugin for typescript. It allows to use `unknown` (or a custom type) instead of `any` for the `extensions` parameter without breaking changes. * Add changeset * Add tests for extensionsType config --- .changeset/poor-seas-swim.md | 5 + .../typescript/graphql-request/src/config.ts | 13 + .../typescript/graphql-request/src/visitor.ts | 4 +- .../graphql-request.spec.ts.snap | 579 +++++++++++++++++- .../tests/graphql-request.spec.ts | 60 ++ 5 files changed, 652 insertions(+), 9 deletions(-) create mode 100644 .changeset/poor-seas-swim.md diff --git a/.changeset/poor-seas-swim.md b/.changeset/poor-seas-swim.md new file mode 100644 index 00000000000..068fa91bcb8 --- /dev/null +++ b/.changeset/poor-seas-swim.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/typescript-graphql-request': minor +--- + +Add the extensionsType config in order to change the default type for extensions when rawRequest is true. diff --git a/packages/plugins/typescript/graphql-request/src/config.ts b/packages/plugins/typescript/graphql-request/src/config.ts index cfba69bc091..c9e0a7db230 100644 --- a/packages/plugins/typescript/graphql-request/src/config.ts +++ b/packages/plugins/typescript/graphql-request/src/config.ts @@ -22,4 +22,17 @@ export interface RawGraphQLRequestPluginConfig extends RawClientSideBasePluginCo * ``` */ rawRequest?: boolean; + + /** + * @description Allows you to override the type for extensions when `rawRequest` is enabled. + * @default any + * + * @exampleMarkdown + * ```yml + * config: + * rawRequest: true + * extensionsType: unknown + * ``` + */ + extensionsType?: string; } diff --git a/packages/plugins/typescript/graphql-request/src/visitor.ts b/packages/plugins/typescript/graphql-request/src/visitor.ts index 5dbc50e7a85..f3a612c4ddb 100644 --- a/packages/plugins/typescript/graphql-request/src/visitor.ts +++ b/packages/plugins/typescript/graphql-request/src/visitor.ts @@ -12,6 +12,7 @@ import { RawGraphQLRequestPluginConfig } from './config'; export interface GraphQLRequestPluginConfig extends ClientSideBasePluginConfig { rawRequest: boolean; + extensionsType: string; } const additionalExportedTypes = ` @@ -33,6 +34,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor< constructor(schema: GraphQLSchema, fragments: LoadedFragment[], rawConfig: RawGraphQLRequestPluginConfig) { super(schema, fragments, rawConfig, { rawRequest: getConfigValue(rawConfig.rawRequest, false), + extensionsType: getConfigValue(rawConfig.extensionsType, "any"), }); autoBind(this); @@ -111,7 +113,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor< o.operationVariablesTypes }, requestHeaders?: Dom.RequestInit["headers"]): Promise<{ data?: ${ o.operationResultType - } | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + } | undefined; extensions?: ${this.config.extensionsType}; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { return withWrapper((wrappedRequestHeaders) => client.rawRequest<${ o.operationResultType }>(${docArg}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}'); diff --git a/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap b/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap index 1b9d029d336..ae21ea3a928 100644 --- a/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap +++ b/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap @@ -839,7 +839,7 @@ async function test() { }" `; -exports[`graphql-request sdk Should support rawRequest when documentMode = "documentNode" 1`] = ` +exports[`graphql-request sdk Should support extensionType when rawRequest is true and documentMode = "DocumentNode" 1`] = ` "export type Maybe = T | null; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; @@ -1089,16 +1089,16 @@ const Feed3DocumentString = print(Feed3Document); const Feed4DocumentString = print(Feed4Document); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { - feed(variables?: FeedQueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: FeedQuery | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + feed(variables?: FeedQueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: FeedQuery | undefined; extensions?: unknown; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed'); }, - feed2(variables: Feed2QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed2Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + feed2(variables: Feed2QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed2Query | undefined; extensions?: unknown; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2'); }, - feed3(variables?: Feed3QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed3Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + feed3(variables?: Feed3QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed3Query | undefined; extensions?: unknown; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3'); }, - feed4(variables?: Feed4QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed4Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + feed4(variables?: Feed4QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed4Query | undefined; extensions?: unknown; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4'); } }; @@ -1123,13 +1123,576 @@ async function test() { }" `; -exports[`graphql-request sdk Should support useTypeImports 1`] = ` +exports[`graphql-request sdk Should support rawRequest when documentMode = "documentNode" 1`] = ` "export type Maybe = T | null; export type Exact = { [K in keyof T]: T[K] }; export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; -import type { GraphQLClient } from 'graphql-request'; -import type * as Dom from 'graphql-request/dist/types.dom'; +import { GraphQLClient } from 'graphql-request'; +import * as Dom from 'graphql-request/dist/types.dom'; +import { GraphQLError } from 'graphql-request/dist/types'; +import { print } from 'graphql' +import gql from 'graphql-tag'; +/** All built-in and custom scalars, mapped to their actual values */ +export type Scalars = { + ID: string; + String: string; + Boolean: boolean; + Int: number; + Float: number; +}; + +export type Query = { + __typename?: 'Query'; + /** A feed of repository submissions */ + feed?: Maybe>>; + /** A single entry */ + entry?: Maybe; + /** Return the currently logged in user, or null if nobody is logged in */ + currentUser?: Maybe; +}; + + +export type QueryFeedArgs = { + type: FeedType; + offset?: Maybe; + limit?: Maybe; +}; + + +export type QueryEntryArgs = { + repoFullName: Scalars['String']; +}; + +/** A list of options for the sort order of the feed */ +export enum FeedType { + /** Sort by a combination of freshness and score, using Reddit's algorithm */ + Hot = 'HOT', + /** Newest entries first */ + New = 'NEW', + /** Highest score entries first */ + Top = 'TOP' +} + +/** Information about a GitHub repository submitted to GitHunt */ +export type Entry = { + __typename?: 'Entry'; + /** Information about the repository from GitHub */ + repository: Repository; + /** The GitHub user who submitted this entry */ + postedBy: User; + /** A timestamp of when the entry was submitted */ + createdAt: Scalars['Float']; + /** The score of this repository, upvotes - downvotes */ + score: Scalars['Int']; + /** The hot score of this repository */ + hotScore: Scalars['Float']; + /** Comments posted about this repository */ + comments: Array>; + /** The number of comments posted about this repository */ + commentCount: Scalars['Int']; + /** The SQL ID of this entry */ + id: Scalars['Int']; + /** XXX to be changed */ + vote: Vote; +}; + + +/** Information about a GitHub repository submitted to GitHunt */ +export type EntryCommentsArgs = { + limit?: Maybe; + offset?: Maybe; +}; + +/** + * A repository object from the GitHub API. This uses the exact field names returned by the + * GitHub API for simplicity, even though the convention for GraphQL is usually to camel case. + */ +export type Repository = { + __typename?: 'Repository'; + /** Just the name of the repository, e.g. GitHunt-API */ + name: Scalars['String']; + /** The full name of the repository with the username, e.g. apollostack/GitHunt-API */ + full_name: Scalars['String']; + /** The description of the repository */ + description?: Maybe; + /** The link to the repository on GitHub */ + html_url: Scalars['String']; + /** The number of people who have starred this repository on GitHub */ + stargazers_count: Scalars['Int']; + /** The number of open issues on this repository on GitHub */ + open_issues_count?: Maybe; + /** The owner of this repository on GitHub, e.g. apollostack */ + owner?: Maybe; +}; + +/** A user object from the GitHub API. This uses the exact field names returned from the GitHub API. */ +export type User = { + __typename?: 'User'; + /** The name of the user, e.g. apollostack */ + login: Scalars['String']; + /** The URL to a directly embeddable image for this user's avatar */ + avatar_url: Scalars['String']; + /** The URL of this user's GitHub page */ + html_url: Scalars['String']; +}; + +/** A comment about an entry, submitted by a user */ +export type Comment = { + __typename?: 'Comment'; + /** The SQL ID of this entry */ + id: Scalars['Int']; + /** The GitHub user who posted the comment */ + postedBy: User; + /** A timestamp of when the comment was posted */ + createdAt: Scalars['Float']; + /** The text of the comment */ + content: Scalars['String']; + /** The repository which this comment is about */ + repoName: Scalars['String']; +}; + +/** XXX to be removed */ +export type Vote = { + __typename?: 'Vote'; + vote_value: Scalars['Int']; +}; + +export type Mutation = { + __typename?: 'Mutation'; + /** Submit a new repository, returns the new submission */ + submitRepository?: Maybe; + /** Vote on a repository submission, returns the submission that was voted on */ + vote?: Maybe; + /** Comment on a repository, returns the new comment */ + submitComment?: Maybe; +}; + + +export type MutationSubmitRepositoryArgs = { + repoFullName: Scalars['String']; +}; + + +export type MutationVoteArgs = { + repoFullName: Scalars['String']; + type: VoteType; +}; + + +export type MutationSubmitCommentArgs = { + repoFullName: Scalars['String']; + commentContent: Scalars['String']; +}; + +/** The type of vote to record, when submitting a vote */ +export enum VoteType { + Up = 'UP', + Down = 'DOWN', + Cancel = 'CANCEL' +} + +export type Subscription = { + __typename?: 'Subscription'; + /** Subscription fires on every comment added */ + commentAdded?: Maybe; +}; + + +export type SubscriptionCommentAddedArgs = { + repoFullName: Scalars['String']; +}; +export type FeedQueryVariables = Exact<{ [key: string]: never; }>; + + +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 }; + +export type Feed2QueryVariables = Exact<{ + v: Scalars['String']; +}>; + + +export type Feed2Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined }; + +export type Feed3QueryVariables = Exact<{ + v?: Maybe; +}>; + + +export type Feed3Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined }; + +export type Feed4QueryVariables = Exact<{ + v?: Scalars['String']; +}>; + + +export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined }; + +export const FeedDocument = gql\` + query feed { + feed { + id + commentCount + repository { + owner { + avatar_url + } + } + } +} + \`; +export const Feed2Document = gql\` + query feed2($v: String!) { + feed { + id + } +} + \`; +export const Feed3Document = gql\` + query feed3($v: String) { + feed { + id + } +} + \`; +export const Feed4Document = gql\` + query feed4($v: String! = \\"TEST\\") { + feed { + id + } +} + \`; + +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string) => Promise; + + +const defaultWrapper: SdkFunctionWrapper = (action, _operationName) => action(); +const FeedDocumentString = print(FeedDocument); +const Feed2DocumentString = print(Feed2Document); +const Feed3DocumentString = print(Feed3Document); +const Feed4DocumentString = print(Feed4Document); +export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { + return { + feed(variables?: FeedQueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: FeedQuery | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed'); + }, + feed2(variables: Feed2QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed2Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2'); + }, + feed3(variables?: Feed3QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed3Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3'); + }, + feed4(variables?: Feed4QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise<{ data?: Feed4Query | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> { + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4'); + } + }; +} +export type Sdk = ReturnType; +async function test() { + const Client = require('graphql-request').GraphQLClient; + const client = new Client(''); + const sdk = getSdk(client); + + await sdk.feed(); + await sdk.feed3(); + await sdk.feed4(); + + const result = await sdk.feed2({ v: \\"1\\" }); + + if (result.feed) { + if (result.feed[0]) { + const id = result.feed[0].id + } + } +}" +`; + +exports[`graphql-request sdk Should support useTypeImports 1`] = ` +"export type Maybe = T | null; +export type Exact = { [K in keyof T]: T[K] }; +export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; +export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; +import type { GraphQLClient } from 'graphql-request'; +import type * as Dom from 'graphql-request/dist/types.dom'; +import gql from 'graphql-tag'; +/** All built-in and custom scalars, mapped to their actual values */ +export type Scalars = { + ID: string; + String: string; + Boolean: boolean; + Int: number; + Float: number; +}; + +export type Query = { + __typename?: 'Query'; + /** A feed of repository submissions */ + feed?: Maybe>>; + /** A single entry */ + entry?: Maybe; + /** Return the currently logged in user, or null if nobody is logged in */ + currentUser?: Maybe; +}; + + +export type QueryFeedArgs = { + type: FeedType; + offset?: Maybe; + limit?: Maybe; +}; + + +export type QueryEntryArgs = { + repoFullName: Scalars['String']; +}; + +/** A list of options for the sort order of the feed */ +export enum FeedType { + /** Sort by a combination of freshness and score, using Reddit's algorithm */ + Hot = 'HOT', + /** Newest entries first */ + New = 'NEW', + /** Highest score entries first */ + Top = 'TOP' +} + +/** Information about a GitHub repository submitted to GitHunt */ +export type Entry = { + __typename?: 'Entry'; + /** Information about the repository from GitHub */ + repository: Repository; + /** The GitHub user who submitted this entry */ + postedBy: User; + /** A timestamp of when the entry was submitted */ + createdAt: Scalars['Float']; + /** The score of this repository, upvotes - downvotes */ + score: Scalars['Int']; + /** The hot score of this repository */ + hotScore: Scalars['Float']; + /** Comments posted about this repository */ + comments: Array>; + /** The number of comments posted about this repository */ + commentCount: Scalars['Int']; + /** The SQL ID of this entry */ + id: Scalars['Int']; + /** XXX to be changed */ + vote: Vote; +}; + + +/** Information about a GitHub repository submitted to GitHunt */ +export type EntryCommentsArgs = { + limit?: Maybe; + offset?: Maybe; +}; + +/** + * A repository object from the GitHub API. This uses the exact field names returned by the + * GitHub API for simplicity, even though the convention for GraphQL is usually to camel case. + */ +export type Repository = { + __typename?: 'Repository'; + /** Just the name of the repository, e.g. GitHunt-API */ + name: Scalars['String']; + /** The full name of the repository with the username, e.g. apollostack/GitHunt-API */ + full_name: Scalars['String']; + /** The description of the repository */ + description?: Maybe; + /** The link to the repository on GitHub */ + html_url: Scalars['String']; + /** The number of people who have starred this repository on GitHub */ + stargazers_count: Scalars['Int']; + /** The number of open issues on this repository on GitHub */ + open_issues_count?: Maybe; + /** The owner of this repository on GitHub, e.g. apollostack */ + owner?: Maybe; +}; + +/** A user object from the GitHub API. This uses the exact field names returned from the GitHub API. */ +export type User = { + __typename?: 'User'; + /** The name of the user, e.g. apollostack */ + login: Scalars['String']; + /** The URL to a directly embeddable image for this user's avatar */ + avatar_url: Scalars['String']; + /** The URL of this user's GitHub page */ + html_url: Scalars['String']; +}; + +/** A comment about an entry, submitted by a user */ +export type Comment = { + __typename?: 'Comment'; + /** The SQL ID of this entry */ + id: Scalars['Int']; + /** The GitHub user who posted the comment */ + postedBy: User; + /** A timestamp of when the comment was posted */ + createdAt: Scalars['Float']; + /** The text of the comment */ + content: Scalars['String']; + /** The repository which this comment is about */ + repoName: Scalars['String']; +}; + +/** XXX to be removed */ +export type Vote = { + __typename?: 'Vote'; + vote_value: Scalars['Int']; +}; + +export type Mutation = { + __typename?: 'Mutation'; + /** Submit a new repository, returns the new submission */ + submitRepository?: Maybe; + /** Vote on a repository submission, returns the submission that was voted on */ + vote?: Maybe; + /** Comment on a repository, returns the new comment */ + submitComment?: Maybe; +}; + + +export type MutationSubmitRepositoryArgs = { + repoFullName: Scalars['String']; +}; + + +export type MutationVoteArgs = { + repoFullName: Scalars['String']; + type: VoteType; +}; + + +export type MutationSubmitCommentArgs = { + repoFullName: Scalars['String']; + commentContent: Scalars['String']; +}; + +/** The type of vote to record, when submitting a vote */ +export enum VoteType { + Up = 'UP', + Down = 'DOWN', + Cancel = 'CANCEL' +} + +export type Subscription = { + __typename?: 'Subscription'; + /** Subscription fires on every comment added */ + commentAdded?: Maybe; +}; + + +export type SubscriptionCommentAddedArgs = { + repoFullName: Scalars['String']; +}; +export type FeedQueryVariables = Exact<{ [key: string]: never; }>; + + +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 }; + +export type Feed2QueryVariables = Exact<{ + v: Scalars['String']; +}>; + + +export type Feed2Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined }; + +export type Feed3QueryVariables = Exact<{ + v?: Maybe; +}>; + + +export type Feed3Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined }; + +export type Feed4QueryVariables = Exact<{ + v?: Scalars['String']; +}>; + + +export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null | undefined> | null | undefined }; + +export const FeedDocument = gql\` + query feed { + feed { + id + commentCount + repository { + owner { + avatar_url + } + } + } +} + \`; +export const Feed2Document = gql\` + query feed2($v: String!) { + feed { + id + } +} + \`; +export const Feed3Document = gql\` + query feed3($v: String) { + feed { + id + } +} + \`; +export const Feed4Document = gql\` + query feed4($v: String! = \\"TEST\\") { + feed { + id + } +} + \`; + +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string) => Promise; + + +const defaultWrapper: SdkFunctionWrapper = (action, _operationName) => action(); + +export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { + return { + feed(variables?: FeedQueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise { + return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed'); + }, + feed2(variables: Feed2QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise { + return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2'); + }, + feed3(variables?: Feed3QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise { + return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3'); + }, + feed4(variables?: Feed4QueryVariables, requestHeaders?: Dom.RequestInit[\\"headers\\"]): Promise { + return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4'); + } + }; +} +export type Sdk = ReturnType; +async function test() { + const Client = require('graphql-request').GraphQLClient; + const client = new Client(''); + const sdk = getSdk(client); + + await sdk.feed(); + await sdk.feed3(); + await sdk.feed4(); + + const result = await sdk.feed2({ v: \\"1\\" }); + + if (result.feed) { + if (result.feed[0]) { + const id = result.feed[0].id + } + } +}" +`; + +exports[`graphql-request sdk extensionType should be irrelevant when rawRequest is false 1`] = ` +"export type Maybe = T | null; +export type Exact = { [K in keyof T]: T[K] }; +export type MakeOptional = Omit & { [SubKey in K]?: Maybe }; +export type MakeMaybe = Omit & { [SubKey in K]: Maybe }; +import { GraphQLClient } from 'graphql-request'; +import * as Dom from 'graphql-request/dist/types.dom'; import gql from 'graphql-tag'; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { diff --git a/packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts b/packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts index 608b244e211..375a84e03b2 100644 --- a/packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts +++ b/packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts @@ -207,6 +207,66 @@ async function test() { const result = await sdk.feed2({ v: "1" }); + if (result.feed) { + if (result.feed[0]) { + const id = result.feed[0].id + } + } +}`; + const output = await validate(result, config, docs, schema, usage); + + expect(output).toMatchSnapshot(); + }); + + it('Should support extensionType when rawRequest is true and documentMode = "DocumentNode"', async () => { + const config = { rawRequest: true, extensionsType: 'unknown' }; + const docs = [{ location: '', document: basicDoc }]; + const result = (await plugin(schema, docs, config, { + outputFile: 'graphql.ts', + })) as Types.ComplexPluginOutput; + + const usage = ` +async function test() { + const Client = require('graphql-request').GraphQLClient; + const client = new Client(''); + const sdk = getSdk(client); + + await sdk.feed(); + await sdk.feed3(); + await sdk.feed4(); + + const result = await sdk.feed2({ v: "1" }); + + if (result.feed) { + if (result.feed[0]) { + const id = result.feed[0].id + } + } +}`; + const output = await validate(result, config, docs, schema, usage); + + expect(output).toMatchSnapshot(); + }); + + it('extensionType should be irrelevant when rawRequest is false', async () => { + const config = { rawRequest: false, extensionsType: 'unknown' }; + const docs = [{ location: '', document: basicDoc }]; + const result = (await plugin(schema, docs, config, { + outputFile: 'graphql.ts', + })) as Types.ComplexPluginOutput; + + const usage = ` +async function test() { + const Client = require('graphql-request').GraphQLClient; + const client = new Client(''); + const sdk = getSdk(client); + + await sdk.feed(); + await sdk.feed3(); + await sdk.feed4(); + + const result = await sdk.feed2({ v: "1" }); + if (result.feed) { if (result.feed[0]) { const id = result.feed[0].id