Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async execute(
operation: string,
variables?: { [name: string]: string },
additionalHeaders?: object,
) {
const body = {
query: operation,
variables,
};
const headers = new Headers({ 'Content-Type': 'application/json' });
if (additionalHeaders) {
for (const [name, value] of new Headers(additionalHeaders)) {
headers.append(name, value);
}
}
const request = new Request('http://localhost', {
method: 'POST',
headers,
body: JSON.stringify(body),
});
// create the options and run the operation
const options = this.createGraphQLServerOptions.bind(this);
const response = await graphqlTesting(options)(request);
return (
event: APIGatewayProxyEvent,
context: LambdaContext,
callback: APIGatewayProxyCallback,
) => {
// We re-load the headers into a Fetch API-compatible `Headers`
// interface within `graphqlLambda`, but we still need to respect the
// case-insensitivity within this logic here, so we'll need to do it
// twice since it's not accessible to us otherwise, right now.
const eventHeaders = new Headers(event.headers);
// Make a request-specific copy of the CORS headers, based on the server
// global CORS headers we've set above.
const requestCorsHeaders = new Headers(corsHeaders);
if (cors && cors.origin) {
const requestOrigin = eventHeaders.get('origin');
if (typeof cors.origin === 'string') {
requestCorsHeaders.set('access-control-allow-origin', cors.origin);
} else if (
requestOrigin &&
(typeof cors.origin === 'boolean' ||
(Array.isArray(cors.origin) &&
requestOrigin &&
cors.origin.includes(requestOrigin)))
) {
requestCorsHeaders.set('access-control-allow-origin', requestOrigin);
}
const requestAccessControlRequestHeaders = eventHeaders.get(
async function sendOperation(
context: ExecutionContext,
operation: OperationDefinitionNode,
variables: Record,
): Promise {
const source = print(operation);
// We declare this as 'any' because it is missing url and method, which
// GraphQLRequest.http is supposed to have if it exists.
let http: any;
// If we're capturing a trace for Engine, then save the operation text to
// the node we're building and tell the federated service to include a trace
// in its response.
if (traceNode) {
http = {
headers: new Headers({ 'apollo-federation-include-trace': 'ftv1' }),
};
if (
context.requestContext.metrics &&
context.requestContext.metrics.startHrTime
) {
traceNode.sentTimeOffset = durationHrTimeToNanos(
process.hrtime(context.requestContext.metrics.startHrTime),
);
}
traceNode.sentTime = dateToProtoTimestamp(new Date());
}
const response = await service.process({
request: {
query: source,
variables,
return (
event: APIGatewayProxyEvent,
context: LambdaContext,
callback: APIGatewayProxyCallback,
) => {
// We re-load the headers into a Fetch API-compatible `Headers`
// interface within `graphqlLambda`, but we still need to respect the
// case-insensitivity within this logic here, so we'll need to do it
// twice since it's not accessible to us otherwise, right now.
const eventHeaders = new Headers(event.headers);
// Make a request-specific copy of the CORS headers, based on the server
// global CORS headers we've set above.
const requestCorsHeaders = new Headers(corsHeaders);
if (cors && cors.origin) {
const requestOrigin = eventHeaders.get('origin');
if (typeof cors.origin === 'string') {
requestCorsHeaders.set('access-control-allow-origin', cors.origin);
} else if (
requestOrigin &&
(typeof cors.origin === 'boolean' ||
(Array.isArray(cors.origin) &&
requestOrigin &&
cors.origin.includes(requestOrigin)))
) {
public createHandler({ cors, onHealthCheck }: CreateHandlerOptions = { cors: undefined, onHealthCheck: undefined }) {
// We will kick off the `willStart` event once for the server, and then
// await it before processing any requests by incorporating its `await` into
// the GraphQLServerOptions function which is called before each request.
const promiseWillStart = this.willStart();
const corsHeaders = new Headers();
if (cors) {
if (cors.methods) {
if (typeof cors.methods === 'string') {
corsHeaders.set('access-control-allow-methods', cors.methods);
} else if (Array.isArray(cors.methods)) {
corsHeaders.set(
'access-control-allow-methods',
cors.methods.join(','),
);
}
}
if (cors.allowedHeaders) {
if (typeof cors.allowedHeaders === 'string') {
corsHeaders.set('access-control-allow-headers', cors.allowedHeaders);
const graphqlHandler: any = (req: Request, res: Response): void => {
const hasPostBody = req.body && Object.keys(req.body).length > 0;
if (req.method === 'POST' && !hasPostBody) {
res.status(500).send('POST body missing.');
return;
}
runHttpQuery([req, res], {
method: req.method,
options: options,
query: hasPostBody ? req.body : (req.query as any),
request: {
url: req.url,
method: req.method,
headers: new Headers(req.headers as any), // ? Check if this actually works
},
}).then(
({ graphqlResponse, responseInit }) => {
res
.status(200)
.set(responseInit.headers)
.send(graphqlResponse);
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
res.status(500).send(error);
return;
}
res
.status(error.statusCode)
.set(error.headers)
} catch (e) {
e.message = `Invalid options provided to ApolloServer: ${e.message}`;
throw new Error(e);
}
if (typeof options.context === 'function') {
options.context = (options.context as () => never)();
}
const requestCtx: GraphQLRequestContext = {
request,
context: options.context || Object.create(null),
cache: options.cache!,
response: {
http: {
headers: new Headers(),
},
},
};
return processGraphQLRequest(options, requestCtx);
}
}
return callback(null, {
body: 'POST body missing.',
statusCode: 500,
});
}
runHttpQuery([event, context], {
method: event.httpMethod,
options: options,
query:
event.httpMethod === 'POST' && event.body
? JSON.parse(event.body)
: event.queryStringParameters,
request: {
url: event.path,
method: event.httpMethod,
headers: new Headers(event.headers),
},
}).then(
({ graphqlResponse, responseInit }) => {
callback(null, {
body: graphqlResponse,
statusCode: 200,
headers: responseInit.headers,
});
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) return callback(error);
callback(null, {
body: error.message,
statusCode: error.statusCode,
headers: error.headers,
});
export function convertNodeHttpToRequest(req: IncomingMessage): Request {
const headers = new Headers();
Object.keys(req.headers).forEach(key => {
const values = req.headers[key]!;
if (Array.isArray(values)) {
values.forEach(value => headers.append(key, value));
} else {
headers.append(key, values);
}
});
return new Request(req.url!, {
headers,
method: req.method,
});
}