mirror of
https://github.com/graphql-hive/console
synced 2026-05-13 12:19:03 +00:00
84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
import { ExecutionResult, print } from 'graphql';
|
|
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
import { createFetch } from '@whatwg-node/fetch';
|
|
import { getServiceHost } from './utils';
|
|
|
|
const { fetch } = createFetch({
|
|
useNodeFetch: true,
|
|
});
|
|
|
|
export async function execute<TResult, TVariables>(
|
|
params: {
|
|
document: TypedDocumentNode<TResult, TVariables>;
|
|
operationName?: string;
|
|
authToken?: string;
|
|
token?: string;
|
|
legacyAuthorizationMode?: boolean;
|
|
} & (TVariables extends Record<string, never>
|
|
? { variables?: never }
|
|
: { variables: TVariables }),
|
|
) {
|
|
const registryAddress = await getServiceHost('server', 8082);
|
|
const endpoint = `http://${registryAddress}/graphql`;
|
|
const queryString = print(params.document);
|
|
const response = await fetch(endpoint, {
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
query: queryString,
|
|
operationName: params.operationName,
|
|
variables: params.variables,
|
|
}),
|
|
headers: {
|
|
accept: 'application/json',
|
|
'content-type': 'application/json',
|
|
...(params.authToken
|
|
? {
|
|
authorization: `Bearer ${params.authToken}`,
|
|
}
|
|
: {}),
|
|
...(params.token
|
|
? params.legacyAuthorizationMode
|
|
? {
|
|
'x-api-token': params.token,
|
|
}
|
|
: {
|
|
authorization: `Bearer ${params.token}`,
|
|
}
|
|
: {}),
|
|
},
|
|
});
|
|
|
|
const body = (await response.json()) as ExecutionResult<TResult>;
|
|
|
|
const detailsDump = `\n\tendpoint: ${endpoint}\n\tquery:\n${queryString}\n\tbody:\n${JSON.stringify(
|
|
body,
|
|
null,
|
|
2,
|
|
)}`;
|
|
|
|
return {
|
|
rawBody: body,
|
|
status: response.status,
|
|
expectGraphQLErrors() {
|
|
if (!body.errors?.length) {
|
|
throw new Error(
|
|
`Expected GraphQL response to have errors, but no errors were found!${detailsDump}`,
|
|
);
|
|
}
|
|
|
|
return body.errors!;
|
|
},
|
|
expectNoGraphQLErrors: async () => {
|
|
if (body.errors?.length) {
|
|
throw new Error(
|
|
`Expected GraphQL response to have no errors, but got ${
|
|
body.errors.length
|
|
} errors:\n\t${body.errors.map(e => e.message).join('\n')}${detailsDump}`,
|
|
);
|
|
}
|
|
|
|
return body.data!;
|
|
},
|
|
};
|
|
}
|