Skip to content

Commit 9622d89

Browse files
committed
feat(DEV-248): added dynamic headers as options in template generation
1 parent c902259 commit 9622d89

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/generate.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Types } from '@graphql-codegen/plugin-helpers';
2+
import type { AsyncDataOptions } from 'nuxt/app';
23
import type { Import } from 'unimport';
34

45
import { generate } from '@graphql-codegen/cli';
@@ -8,6 +9,12 @@ import { ofetch } from 'ofetch';
89

910
import { useGraphQLMeta } from './runtime/composables/use-graphql-meta';
1011

12+
interface GraphQLOptions {
13+
asyncDataOptions?: AsyncDataOptions;
14+
headers?: Record<string, string>;
15+
log?: boolean;
16+
}
17+
1118
export type GraphQLMeta = ReturnType<typeof useGraphQLMeta>;
1219

1320
const { loadSchema } = require('@graphql-tools/load');
@@ -101,13 +108,13 @@ export async function generateComposables(meta: GraphQLMeta): Promise<string> {
101108
template.push(
102109
`export const use${capitalizeFirstLetter(query)}Query = (${
103110
types.argType ? 'variables: { ' + types.argType + ' },' : ''
104-
} ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} log?: boolean): Promise<{data: ${types.returnType}; error: GraphqlError | null}> => gqlQuery<${types.returnType}>('${query}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, log})`,
111+
} ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} options?: GraphQLOptions): Promise<{data: ${types.returnType}; error: GraphqlError | null}> => gqlQuery<${types.returnType}>('${query}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, ...options})`,
105112
);
106113

107114
template.push(
108115
`export const useAsync${capitalizeFirstLetter(query)}Query = (${
109116
types.argType ? 'variables: { ' + types.argType + ' },' : ''
110-
} ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} log?: boolean, asyncDataOptions?: AsyncDataOptions): Promise<AsyncData<${types.returnType}, Error>> => gqlAsyncQuery<${types.returnType}>('${query}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, asyncDataOptions, log})`,
117+
} ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} options?: GraphQLOptions): Promise<AsyncData<${types.returnType}, Error>> => gqlAsyncQuery<${types.returnType}>('${query}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, ...options})`,
111118
);
112119
}
113120
}
@@ -122,7 +129,7 @@ export async function generateComposables(meta: GraphQLMeta): Promise<string> {
122129
template.push(
123130
`export const use${capitalizeFirstLetter(mutation)}Mutation = (${
124131
types.argType ? 'variables: { ' + types.argType + ' },' : ''
125-
} ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} log?: boolean): Promise<{data: ${types.returnType}; error: GraphqlError}> => gqlMutation<${types.returnType}>('${mutation}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, log})`,
132+
} ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} options?: GraphQLOptions): Promise<{data: ${types.returnType}; error: GraphqlError}> => gqlMutation<${types.returnType}>('${mutation}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, ...options})`,
126133
);
127134
}
128135
}
@@ -137,7 +144,7 @@ export async function generateComposables(meta: GraphQLMeta): Promise<string> {
137144
template.push(
138145
`export const use${capitalizeFirstLetter(subscription)}Subscription = (${
139146
types.argType ? 'variables: { ' + types.argType + ' },' : ''
140-
} ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} log?: boolean): Promise<ReturnTypeOfSubscription<${types.returnType}>> => gqlSubscription<${types.returnType}>('${subscription}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, log})`,
147+
} ${returnTypeIsDefaultType ? '' : `fields?: InputFields<${inputFieldsType}>[] | null,`} options?: GraphQLOptions): Promise<ReturnTypeOfSubscription<${types.returnType}>> => gqlSubscription<${types.returnType}>('${subscription}', {${types.argType ? 'variables,' : ''} ${returnTypeIsDefaultType ? 'fields: null' : 'fields'}, ...options})`,
141148
);
142149
}
143150
}

0 commit comments

Comments
 (0)