Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/plugins/typescript/generic-sdk/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DocumentMode,
getConfigValue,
indentMultiline,
transformComment,
LoadedFragment,
} from '@graphql-codegen/visitor-plugin-common';
import { RawGenericSdkPluginConfig } from './config.js';
Expand Down Expand Up @@ -110,6 +111,7 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor<
const allPossibleActions = this._operationsToInclude
.map(o => {
const operationName = o.node.name.value;
const operationDocComment = transformComment(o.node.description);
const optionalVariables =
!o.node.variableDefinitions ||
o.node.variableDefinitions.length === 0 ||
Expand All @@ -125,7 +127,7 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor<
const resultData = this.config.rawRequest
? `ExecutionResult<${o.operationResultType}, E>`
: o.operationResultType;
return `${operationName}(variables${optionalVariables ? '?' : ''}: ${
return `${operationDocComment}${operationName}(variables${optionalVariables ? '?' : ''}: ${
o.operationVariablesTypes
}, options?: C): ${returnType}<${resultData}> {
return requester<${o.operationResultType}, ${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -240,6 +241,7 @@ export const Feed4Document = gql\`
export type Requester<C = {}> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
export function getSdk<C>(requester: Requester<C>) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, options?: C): Promise<FeedQuery> {
return requester<FeedQuery, FeedQueryVariables>(FeedDocument, variables, options) as Promise<FeedQuery>;
},
Expand Down Expand Up @@ -945,7 +947,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = \`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -981,6 +984,7 @@ export const Feed4Document = \`
export type Requester<C = {}> = <R, V>(doc: string, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
export function getSdk<C>(requester: Requester<C>) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, options?: C): Promise<FeedQuery> {
return requester<FeedQuery, FeedQueryVariables>(FeedDocument, variables, options) as Promise<FeedQuery>;
},
Expand Down Expand Up @@ -1218,7 +1222,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -1254,6 +1259,7 @@ export const Feed4Document = gql\`
export type Requester<C = {}, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<ExecutionResult<R, E>> | AsyncIterable<ExecutionResult<R, E>>
export function getSdk<C, E>(requester: Requester<C, E>) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, options?: C): Promise<ExecutionResult<FeedQuery, E>> {
return requester<FeedQuery, FeedQueryVariables>(FeedDocument, variables, options) as Promise<ExecutionResult<FeedQuery, E>>;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const schema = extendSchema(
`),
);
const basicDoc = parse(/* GraphQL */ `
"""description (becomes JSDoc)"""
query feed {
feed {
id
Expand Down
6 changes: 4 additions & 2 deletions packages/plugins/typescript/graphql-request/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
DocumentMode,
getConfigValue,
indentMultiline,
transformComment,
LoadedFragment,
} from '@graphql-codegen/visitor-plugin-common';
import { RawGraphQLRequestPluginConfig } from './config.js';
Expand Down Expand Up @@ -116,6 +117,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
.map(o => {
const operationType = o.node.operation;
const operationName = o.node.name.value;
const operationDocComment = transformComment(o.node.description);
const optionalVariables =
!o.node.variableDefinitions ||
o.node.variableDefinitions.length === 0 ||
Expand All @@ -130,7 +132,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
docArg = `${docVarName}String`;
extraVariables.push(`const ${docArg} = print(${docVarName});`);
}
return `${operationName}(variables${optionalVariables ? '?' : ''}: ${
return `${operationDocComment}${operationName}(variables${optionalVariables ? '?' : ''}: ${
o.operationVariablesTypes
}, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: ${
o.operationResultType
Expand All @@ -142,7 +144,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
}>(${docArg}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}', '${operationType}', variables);
}`;
}
return `${operationName}(variables${optionalVariables ? '?' : ''}: ${
return `${operationDocComment}${operationName}(variables${optionalVariables ? '?' : ''}: ${
o.operationVariablesTypes
}, requestHeaders?: GraphQLClientRequestHeaders, signal?: RequestInit['signal']): Promise<${o.operationResultType}> {
return withWrapper((wrappedRequestHeaders) => client.request<${
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = \`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -245,6 +246,7 @@ const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationTy

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders, signal?: RequestInit['signal']): Promise<FeedQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<FeedQuery>({ document: FeedDocument, variables, requestHeaders: { ...requestHeaders, ...wrappedRequestHeaders }, signal }), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -490,7 +492,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -531,6 +534,7 @@ const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationTy

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders, signal?: RequestInit['signal']): Promise<FeedQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<FeedQuery>({ document: FeedDocument, variables, requestHeaders: { ...requestHeaders, ...wrappedRequestHeaders }, signal }), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -768,7 +772,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = \`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -809,6 +814,7 @@ const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationTy

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders, signal?: RequestInit['signal']): Promise<FeedQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<FeedQuery>({ document: FeedDocument, variables, requestHeaders: { ...requestHeaders, ...wrappedRequestHeaders }, signal }), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -1048,7 +1054,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -1092,6 +1099,7 @@ const Feed3DocumentString = print(Feed3Document);
const Feed4DocumentString = print(Feed4Document);
export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: FeedQuery; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> {
return withWrapper((wrappedRequestHeaders) => client.rawRequest<FeedQuery>(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -1331,7 +1339,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = \`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -1372,6 +1381,7 @@ const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationTy

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: FeedQuery; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> {
return withWrapper((wrappedRequestHeaders) => client.rawRequest<FeedQuery>(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -1611,7 +1621,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -1652,6 +1663,7 @@ const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationTy

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders, signal?: RequestInit['signal']): Promise<FeedQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<FeedQuery>({ document: FeedDocument, variables, requestHeaders: { ...requestHeaders, ...wrappedRequestHeaders }, signal }), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -1892,7 +1904,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -1936,6 +1949,7 @@ const Feed3DocumentString = print(Feed3Document);
const Feed4DocumentString = print(Feed4Document);
export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: FeedQuery; errors?: GraphQLError[]; extensions?: unknown; headers: Headers; status: number; }> {
return withWrapper((wrappedRequestHeaders) => client.rawRequest<FeedQuery>(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -2176,7 +2190,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -2220,6 +2235,7 @@ const Feed3DocumentString = print(Feed3Document);
const Feed4DocumentString = print(Feed4Document);
export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: FeedQuery; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> {
return withWrapper((wrappedRequestHeaders) => client.rawRequest<FeedQuery>(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -2459,7 +2475,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -2500,6 +2517,7 @@ const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationTy

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders, signal?: RequestInit['signal']): Promise<FeedQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<FeedQuery>({ document: FeedDocument, variables, requestHeaders: { ...requestHeaders, ...wrappedRequestHeaders }, signal }), 'feed', 'query', variables);
},
Expand Down Expand Up @@ -2739,7 +2757,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -2780,6 +2799,7 @@ const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationTy

export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) {
return {
/** description (becomes JSDoc) */
feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders, signal?: RequestInit['signal']): Promise<FeedQuery> {
return withWrapper((wrappedRequestHeaders) => client.request<FeedQuery>({ document: FeedDocument, variables, requestHeaders: { ...requestHeaders, ...wrappedRequestHeaders }, signal }), 'feed', 'query', variables);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { plugin } from '../src/index.js';
describe('graphql-request', () => {
const schema = buildClientSchema(require('../../../../../dev-test/githunt/schema.json'));
const basicDoc = parse(/* GraphQL */ `
"""description (becomes JSDoc)"""
query feed {
feed {
id
Expand Down
4 changes: 3 additions & 1 deletion packages/plugins/typescript/jit-sdk/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ClientSideBaseVisitor,
DocumentMode,
indentMultiline,
transformComment,
LoadedFragment,
} from '@graphql-codegen/visitor-plugin-common';
import { RawJitSdkPluginConfig } from './config.js';
Expand Down Expand Up @@ -83,6 +84,7 @@ export class JitSdkVisitor extends ClientSideBaseVisitor<
let hasSubscription = false;
for (const o of this._operationsToInclude) {
const operationName = o.node.name.value;
const operationDocComment = transformComment(o.node.description);
const compiledQueryVariableName = `${operationName}Compiled`;
compiledQueries.push(
indentMultiline(
Expand Down Expand Up @@ -115,7 +117,7 @@ if(!(isCompiledQuery(${compiledQueryVariableName}))) {
}
sdkMethods.push(
indentMultiline(
`async ${operationName}(variables${optionalVariables ? '?' : ''}: ${
`${operationDocComment}async ${operationName}(variables${optionalVariables ? '?' : ''}: ${
o.operationVariablesTypes
}, context?: TOperationContext, root?: TOperationRoot): Promise<${returnType}> {
const result = await ${compiledQueryVariableName}.${methodName}({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = gql\`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -277,6 +278,7 @@ export function getSdk<TGlobalContext = any, TGlobalRoot = any, TOperationContex
}

return {
/** description (becomes JSDoc) */
async feed(variables?: FeedQueryVariables, context?: TOperationContext, root?: TOperationRoot): Promise<FeedQuery> {
const result = await feedCompiled.query({
...globalRoot,
Expand Down Expand Up @@ -991,7 +993,8 @@ export type Feed4QueryVariables = Exact<{
export type Feed4Query = { __typename?: 'Query', feed?: Array<{ __typename?: 'Entry', id: number } | null> | null };

export const FeedDocument = \`
query feed {
"""description (becomes JSDoc)"""
query feed {
feed {
id
commentCount
Expand Down Expand Up @@ -1062,6 +1065,7 @@ export function getSdk<TGlobalContext = any, TGlobalRoot = any, TOperationContex
}

return {
/** description (becomes JSDoc) */
async feed(variables?: FeedQueryVariables, context?: TOperationContext, root?: TOperationRoot): Promise<FeedQuery> {
const result = await feedCompiled.query({
...globalRoot,
Expand Down
1 change: 1 addition & 0 deletions packages/plugins/typescript/jit-sdk/tests/jit-sdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { plugin } from '../src/index.js';

const schema = buildClientSchema(require('../../../../../dev-test/githunt/schema.json'));
const basicDoc = parse(/* GraphQL */ `
"""description (becomes JSDoc)"""
query feed {
feed {
id
Expand Down
4 changes: 3 additions & 1 deletion packages/plugins/typescript/react-apollo/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ClientSideBaseVisitor,
DocumentMode,
getConfigValue,
transformComment,
LoadedFragment,
OMIT_TYPE,
} from '@graphql-codegen/visitor-plugin-common';
Expand Down Expand Up @@ -526,10 +527,11 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<
suffix: this._getHookSuffix(nodeName, operationType),
useTypesPrefix: false,
}) + this.config.hooksSuffix;
const operationDocComment = transformComment(node.description);

const optional = hasRequiredVariables(node) ? '' : '?';

return `export function refetch${operationName}(variables${optional}: ${operationVariablesTypes}) {
return `${operationDocComment}export function refetch${operationName}(variables${optional}: ${operationVariablesTypes}) {
return { query: ${this.getDocumentNodeVariable(
node,
documentVariableName,
Expand Down
Loading