Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const res = mockResponse(mockResponseOptions);
const graphQLOptions = await apolloServer.createGraphQLServerOptions(
req,
res
);
const { graphqlResponse } = await runHttpQuery([req, res], {
method: 'POST',
options: graphQLOptions,
query: {
// operation can be a string or an AST, but `runHttpQuery` only accepts a string
query: typeof operation === 'string' ? operation : print(operation),
variables
},
request: convertNodeHttpToRequest(req)
});
return JSON.parse(graphqlResponse) as T;
};
try {
if (req.method === "POST")
query = req.filePayload || req.body;
else
query = url.parse(req.url, true).query;
} catch (error) {
// Do nothing; `query` stays `undefined`
}
try {
const { graphqlResponse, responseInit } = await runHttpQuery([req, res], {
method: req.method,
options,
query,
request: convertNodeHttpToRequest(req),
});
setHeaders(res, responseInit.headers);
return graphqlResponse;
} catch (error) {
if ("HttpQueryError" === error.name && error.headers) {
setHeaders(res, error.headers);
}
if (!error.statusCode) {
error.statusCode = 500;
}
res.statusCode = error.statusCode || error.code || 500;
handler: async (request, h) => {
try {
const { graphqlResponse, responseInit } = await runHttpQuery(
[request, h],
{
method: request.method.toUpperCase(),
options: options.graphqlOptions,
query:
request.method === 'post'
? // TODO type payload as string or Record
(request.payload as any)
: request.query,
request: convertNodeHttpToRequest(request.raw.req),
},
);
const response = h.response(graphqlResponse);
Object.keys(responseInit.headers).forEach(key =>
response.header(key, responseInit.headers[key]),
);
return response;
} catch (error) {
if ('HttpQueryError' !== error.name) {
throw Boom.boomify(error);
}
if (true === error.isGraphQLError) {
const response = h.response(error.message);
response.code(error.statusCode);
const graphqlHandler = (ctx: Koa.Context): Promise => {
return runHttpQuery([ctx], {
method: ctx.request.method,
options: options,
query:
ctx.request.method === 'POST'
? // fallback to ctx.req.body for koa-multer support
ctx.request.body || (ctx.req as any).body
: ctx.request.query,
request: convertNodeHttpToRequest(ctx.req),
}).then(
({ graphqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
ctx.set(key, responseInit.headers[key]),
);
ctx.body = graphqlResponse;
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
throw error;
}
if (error.headers) {
Object.keys(error.headers).forEach(header => {
ctx.set(header, error.headers[header]);
});
try {
if (req.method === "POST") {
query = req.filePayload || req.body;
} else {
query = url.parse(req.url, true).query;
}
} catch (error) {
// Do nothing; `query` stays `undefined`
}
try {
const { graphqlResponse, responseInit } = await runHttpQuery([req, res], {
method: req.method,
options,
query,
request: convertNodeHttpToRequest(req),
});
setHeaders(res, responseInit.headers);
return graphqlResponse;
} catch (error) {
if ("HttpQueryError" === error.name && error.headers) {
setHeaders(res, error.headers);
}
if (!error.statusCode) {
error.statusCode = 500;
}
res.statusCode = error.statusCode || error.code || 500;
res.end(error.message);
const graphqlHandler = (ctx: Koa.Context): Promise => {
return runHttpQuery([ctx], {
method: ctx.request.method,
options: options,
query:
ctx.request.method === 'POST'
? // fallback to ctx.req.body for koa-multer support
ctx.request.body || (ctx.req as any).body
: ctx.request.query,
request: convertNodeHttpToRequest(ctx.req),
}).then(
({ graphqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
ctx.set(key, responseInit.headers[key]),
);
ctx.body = graphqlResponse;
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
throw error;
}
if (error.headers) {
Object.keys(error.headers).forEach(header => {
ctx.set(header, error.headers[header]);
});
let query;
try {
query =
req.method === 'POST'
? req.filePayload || (await json(req))
: url.parse(req.url, true).query;
} catch (error) {
// Do nothing; `query` stays `undefined`
}
try {
const { graphqlResponse, responseInit } = await runHttpQuery([req, res], {
method: req.method,
options,
query,
request: convertNodeHttpToRequest(req),
});
setHeaders(res, responseInit.headers);
return graphqlResponse;
} catch (error) {
if ('HttpQueryError' === error.name && error.headers) {
setHeaders(res, error.headers);
}
if (!error.statusCode) {
error.statusCode = 500;
}
send(res, error.statusCode, error.message);
return undefined;
}
};
let query;
try {
query =
req.method === 'POST'
? req.filePayload || (await json(req))
: url.parse(req.url, true).query;
} catch (error) {
// Do nothing; `query` stays `undefined`
}
try {
const { graphqlResponse, responseInit } = await runHttpQuery([req, res], {
method: req.method,
options,
query,
request: convertNodeHttpToRequest(req),
});
setHeaders(res, responseInit.headers);
return graphqlResponse;
} catch (error) {
if ('HttpQueryError' === error.name && error.headers) {
setHeaders(res, error.headers);
}
if (!error.statusCode) {
error.statusCode = 500;
}
send(res, error.statusCode, error.message);
return undefined;
}
};
return (req, res, next): void => {
runHttpQuery([req, res], {
method: req.method,
options: options,
query: req.method === 'POST' ? req.body : req.query,
request: convertNodeHttpToRequest(req),
}).then(
({ graphqlResponse, responseInit }) => {
if (responseInit.headers) {
for (const [name, value] of Object.entries(responseInit.headers)) {
res.setHeader(name, value);
}
}
res.write(graphqlResponse);
res.end();
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
return next(error);
}
if (error.headers) {
const graphqlHandler = (
req: restify.Request,
res: restify.Response,
next: restify.Next,
): void => {
runHttpQuery([req, res], {
method: req.method,
options: options,
query: req.method === 'POST' ? req.body : req.query,
request: convertNodeHttpToRequest(req),
}).then(
gqlResponse => {
res.setHeader('Content-Type', 'application/json');
res.write(gqlResponse);
res.end();
next();
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
throw error;
}
if (error.headers) {
Object.keys(error.headers).forEach(header => {
res.setHeader(header, error.headers[header]);
});