refactor(graphql): codegen (#10626)

This commit is contained in:
liuyi
2025-03-06 12:06:19 +08:00
committed by GitHub
parent fb084a9569
commit 7e61a0b2fc
10 changed files with 487 additions and 715 deletions

View File

@@ -7,8 +7,7 @@ import type { GraphQLQuery } from '../graphql';
const query: GraphQLQuery = {
id: 'query',
query: 'query { field }',
operationName: 'query',
definitionName: 'query',
op: 'query',
};
let fetch: Mock;
@@ -55,7 +54,6 @@ describe('GraphQL fetcher', () => {
body: '{"query":"query { field }","variables":{"a":1,"b":"2","c":{"d":false}},"operationName":"query"}',
headers: expect.objectContaining({
'content-type': 'application/json',
'x-definition-name': 'query',
'x-operation-name': 'query',
}),
method: 'POST',

View File

@@ -1,3 +1,4 @@
import { DebugLogger } from '@affine/debug';
import type { ExecutionResult } from 'graphql';
import { isNil, isObject, merge } from 'lodash-es';
@@ -156,11 +157,11 @@ function formatRequestBody<Q extends GraphQLQuery>({
(keepNilVariables ?? true) ? variables : filterEmptyValue(variables),
};
if (query.operationName) {
body.operationName = query.operationName;
if (query.op) {
body.operationName = query.op;
}
if (query.containsFile) {
if (query.file) {
return transformToForm(body);
}
return body;
@@ -170,15 +171,24 @@ export const gqlFetcherFactory = (
endpoint: string,
fetcher: (input: string, init?: RequestInit) => Promise<Response> = fetch
) => {
const logger = new DebugLogger('GraphQL');
const gqlFetch = async <Query extends GraphQLQuery>(
options: QueryOptions<Query>
): Promise<QueryResponse<Query>> => {
if (
BUILD_CONFIG.appBuildType === 'canary' &&
options.query.deprecations?.length
) {
options.query.deprecations.forEach(deprecation => {
logger.warn(deprecation);
});
}
const body = formatRequestBody(options);
const isFormData = body instanceof FormData;
const headers: Record<string, string> = {
'x-operation-name': options.query.operationName,
'x-definition-name': options.query.definitionName,
'x-operation-name': options.query.op,
};
if (!isFormData) {
headers['content-type'] = 'application/json';
@@ -208,8 +218,7 @@ export const gqlFetcherFactory = (
}
throw new GraphQLError(
'GraphQL query responds unexpected result, query ' +
options.query.operationName
'GraphQL query responds unexpected result, query ' + options.query.op
);
});

File diff suppressed because it is too large Load Diff