console/integration-tests/testkit/graphql.ts

90 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-03-28 11:49:56 +00:00
import { ExecutionResult, parse, print } from 'graphql';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
2025-03-28 11:49:56 +00:00
import { sortSDL } from '@theguild/federation-composition';
import { getServiceHost } from './utils';
2022-05-18 07:26:57 +00:00
2025-03-28 11:49:56 +00:00
/**
* Sorts the SDL of a supergraph schema and removes any extra whitespace.
* Helps with schema assertions, especially the snapshot tests.
* @param sdl The SDL of a supergraph schema.
* @returns The normalized SDL.
*/
export function normalizeSupergraph(sdl: string): string {
return print(sortSDL(parse(sdl, { noLocation: true })));
}
feat: replace auth0 with supertokens (#303) * add supertoken container to docker-compose file * yeah I am sorry this one big commit and I am ashamed of it * use logOut function * feat: show header on 404 page * feat: better handling for organization cookie when not authenticated * wrap it * check session within server side props * add is_admin flag user migration * simplify and annotate the config * fix: handle status codes + fix email/password sign up with import from auth0 * no hardcoded env pls * decode process.env * secure update user id mapping via a key * fix: login form * lol we don't need to hit the API * fix: do graphql api authorization via authorization header instead of cookie * implement isAdmin flag * fix: types :) * skipit * yo we can run this * set env variables * disable because it annoys the hell out of me * use the right host * add not about token length * refactor: decode environment variables * feat: store external user id from guthub/google provider in the database * workaround supertokens omitting null values from the token * re-enable check * i have no time for this shit * add missing env variable * fix: email test; missing domain extension * configure pulumi deployment Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> Co-authored-by: Dotan Simha <dotansimha@gmail.com> * configure pulumi deployment Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> Co-authored-by: Dotan Simha <dotansimha@gmail.com> * configure pulumi deployment Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> Co-authored-by: Dotan Simha <dotansimha@gmail.com> * configure pulumi deployment Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> * fix: env names * fix: link google account to the correct db record * feat: email confirmation emails * ? * bump ts-node * fix types * omit package form the bundle * remove it from dependencies... * add emails apckage to dev deps * resolve eslint issues * remove comments * update dev info + change env variable (no need to expose it on the frontend) * use correct user id lol Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> Co-authored-by: Dotan Simha <dotansimha@gmail.com>
2022-09-06 07:38:31 +00:00
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 }),
feat: replace auth0 with supertokens (#303) * add supertoken container to docker-compose file * yeah I am sorry this one big commit and I am ashamed of it * use logOut function * feat: show header on 404 page * feat: better handling for organization cookie when not authenticated * wrap it * check session within server side props * add is_admin flag user migration * simplify and annotate the config * fix: handle status codes + fix email/password sign up with import from auth0 * no hardcoded env pls * decode process.env * secure update user id mapping via a key * fix: login form * lol we don't need to hit the API * fix: do graphql api authorization via authorization header instead of cookie * implement isAdmin flag * fix: types :) * skipit * yo we can run this * set env variables * disable because it annoys the hell out of me * use the right host * add not about token length * refactor: decode environment variables * feat: store external user id from guthub/google provider in the database * workaround supertokens omitting null values from the token * re-enable check * i have no time for this shit * add missing env variable * fix: email test; missing domain extension * configure pulumi deployment Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> Co-authored-by: Dotan Simha <dotansimha@gmail.com> * configure pulumi deployment Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> Co-authored-by: Dotan Simha <dotansimha@gmail.com> * configure pulumi deployment Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> Co-authored-by: Dotan Simha <dotansimha@gmail.com> * configure pulumi deployment Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> * fix: env names * fix: link google account to the correct db record * feat: email confirmation emails * ? * bump ts-node * fix types * omit package form the bundle * remove it from dependencies... * add emails apckage to dev deps * resolve eslint issues * remove comments * update dev info + change env variable (no need to expose it on the frontend) * use correct user id lol Co-authored-by: Kamil Kisiela <kamil.kisiela@gmail.com> Co-authored-by: Dotan Simha <dotansimha@gmail.com>
2022-09-06 07:38:31 +00:00
) {
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,
2022-05-18 07:26:57 +00:00
operationName: params.operationName,
variables: params.variables,
}),
headers: {
accept: 'application/json',
'content-type': 'application/json',
...(params.authToken
? {
authorization: `Bearer ${params.authToken}`,
}
: {}),
...(params.token
? params.legacyAuthorizationMode
2022-05-18 07:26:57 +00:00
? {
'x-api-token': params.token,
2022-05-18 07:26:57 +00:00
}
: {
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,
)}\n\trequest-id: ${response.headers.get('x-request-id')}\n`;
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!;
},
2022-05-18 07:26:57 +00:00
};
}