mirror of
https://github.com/graphql-hive/console
synced 2026-04-21 22:47:17 +00:00
* 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>
109 lines
2.6 KiB
TypeScript
109 lines
2.6 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from 'next';
|
|
import { buildSchema, execute, GraphQLError, parse } from 'graphql';
|
|
import { addMocksToSchema } from '@graphql-tools/mock';
|
|
import { extractAccessTokenFromRequest } from '@/lib/api/extract-access-token-from-request';
|
|
|
|
async function lab(req: NextApiRequest, res: NextApiResponse) {
|
|
const url = process.env.GRAPHQL_ENDPOINT;
|
|
const labParams = req.query.lab || [];
|
|
|
|
if (labParams.length < 3) {
|
|
res.status(400).json({
|
|
error: 'Missing Lab Params',
|
|
});
|
|
return;
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
const [organization, project, target, mock] = labParams as string[];
|
|
const headers: Record<string, string> = {};
|
|
|
|
if (req.headers['x-hive-key']) {
|
|
// TODO: change that to Authorization: Bearer
|
|
headers['X-API-Token'] = req.headers['x-hive-key'] as string;
|
|
} else {
|
|
try {
|
|
const accessToken = await extractAccessTokenFromRequest(req, res);
|
|
|
|
if (!accessToken) {
|
|
throw 'Invalid Token!';
|
|
}
|
|
|
|
headers['Authorization'] = `Bearer ${accessToken}`;
|
|
} catch (error) {
|
|
console.warn('Lab auth failed:', error);
|
|
res.status(200).send({
|
|
errors: [new GraphQLError('Invalid or missing X-Hive-Key authentication')],
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
|
|
const body = {
|
|
operationName: 'lab',
|
|
query: /* GraphQL */ `
|
|
query lab($selector: TargetSelectorInput!) {
|
|
lab(selector: $selector) {
|
|
schema
|
|
mocks
|
|
}
|
|
}
|
|
`,
|
|
variables: {
|
|
selector: {
|
|
organization,
|
|
project,
|
|
target,
|
|
},
|
|
},
|
|
};
|
|
|
|
const response = await fetch(url, {
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
...headers,
|
|
},
|
|
method: 'POST',
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
const parsedData = await response.json();
|
|
|
|
if (!parsedData.data?.lab?.schema) {
|
|
res.status(200).json({
|
|
errors: [new GraphQLError('Please publish your first schema to Hive')],
|
|
});
|
|
|
|
return;
|
|
}
|
|
|
|
if (parsedData.data?.errors?.length > 0) {
|
|
res.status(200).json(parsedData.data);
|
|
}
|
|
|
|
try {
|
|
const rawSchema = buildSchema(parsedData.data.lab?.schema);
|
|
const document = parse(req.body.query);
|
|
|
|
const mockedSchema = addMocksToSchema({
|
|
schema: rawSchema,
|
|
preserveResolvers: false,
|
|
});
|
|
|
|
const result = await execute({
|
|
schema: mockedSchema,
|
|
document,
|
|
variableValues: req.body.variables || {},
|
|
contextValue: {},
|
|
});
|
|
|
|
res.status(200).json(result);
|
|
} catch (e) {
|
|
console.log(e);
|
|
res.status(200).json({
|
|
errors: [e],
|
|
});
|
|
}
|
|
}
|
|
|
|
export default lab;
|