How to use the apollo-server-core.runHttpQuery function in apollo-server-core

To help you get started, we’ve selected a few apollo-server-core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vesper-framework / vesper / src / index.ts View on Github external
return (req: Request, res: Response, next: NextFunction) => {
        const container = Container.of(req);
        container.set(CurrentRequest, req);
        container.set(CurrentResponse, res);
        allOptions.context.container = container;
        allOptions.context.dataLoaders = {};

        return runHttpQuery([req, res], {
            method: req.method,
            options: allOptions,
            query: req.method === "POST" ? req.body : req.query,
        }).then((gqlResponse) => {

            // commit transaction
            const transactionEntityManager = container.has(EntityManager) ? container.get(EntityManager) : undefined;
            if (transactionEntityManager &&
                transactionEntityManager.connection.options.type !== "mongodb" &&
                transactionEntityManager.queryRunner &&
                transactionEntityManager.queryRunner.isTransactionActive &&
                transactionEntityManager.queryRunner.isReleased === false) {
                return transactionEntityManager.queryRunner
                    .commitTransaction()
                    .then(() => transactionEntityManager.queryRunner.release())
                    .then(() => gqlResponse);
github icebob / kantab / backend / mixins / apollo-server-moleculer / moleculerApollo.js View on Github external
return async function graphqlHandler(req, res) {
		let query;
		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) {
github apollographql / apollo-server / packages / apollo-server-koa / src / koaApollo.ts View on Github external
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) => {
github moleculerjs / moleculer-apollo-server / src / moleculerApollo.js View on Github external
return async function graphqlHandler(req, res) {
		let query;
		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;
github codejie / fastify-apollo-step / src / apollo-graphql.js View on Github external
handler: function (request, reply) {
            runHttpQuery([request, reply], {
                method: request.req.method,
                options: options.apollo,
                query: request.req.method === 'POST' ? request.body : request.query
            }).then((response) => {
                reply.type('application/graphql').send(response.graphqlResponse);
            }, (err) => {
                if (err.name === 'HttpQueryError') {
                    if (err.headers) {
                        Object.keys(err.headers).forEach(header => {
                            reply.headers(header, err.headers[header]);
                        });
                    }
                }
                reply.type('application/graphql').code(500).send(err.message);
            });
        }
github apollographql / apollo-server / packages / apollo-server-azure-functions / src / azureFunctionsApollo.ts View on Github external
const graphqlHandler = (
    httpContext: HttpContext,
    request: IFunctionRequest,
  ) => {
    const queryRequest = {
      method: request.method,
      options: options,
      query: request.method === 'POST' ? request.body : request.query,
      request,
    };

    if (queryRequest.query && typeof queryRequest.query === 'string') {
      queryRequest.query = JSON.parse(queryRequest.query);
    }

    return runHttpQuery([httpContext, request], queryRequest as any)
      .then(gqlResponse => {
        const result = {
          status: HttpStatusCodes.OK,
          headers: {
            'Content-Type': 'application/json',
          },
          body: gqlResponse,
          isRaw: true,
        };
        httpContext.res = result;
        httpContext.done(null, result);
      })
      .catch(error => {
        const result = {
          status: error.statusCode,
          headers: error.headers,
github ammezie / adonis-apollo-server / src / ApolloServer / index.js View on Github external
graphql (options, request, response) {
        if (!options) {
            throw new Error('Apollo Server requires options.')
        }

        return runHttpQuery([request], {
            method: request.method(),
            options: options,
            query: request.method() === 'POST' ? request.post() : request.get()
        }).then((gqlResponse) => {
            return response.json(gqlResponse)
        }, error => {
            if ('HttpQueryError' !== error.name) {
                throw error
            }

            if (error.headers) {
                Object.keys(error.headers).forEach(header => {
                    response.header(header, error.headers[header])
                })
            }
github apollographql / apollo-server / packages / apollo-server-express / src / expressApollo.ts View on Github external
const graphqlHandler = (
    req: express.Request,
    res: express.Response,
    next,
  ): void => {
    runHttpQuery([req, res], {
      method: req.method,
      options: options,
      query: req.method === 'POST' ? req.body : req.query,
      request: convertNodeHttpToRequest(req),
    }).then(
      ({ graphqlResponse, responseInit }) => {
        Object.keys(responseInit.headers).forEach(key =>
          res.setHeader(key, responseInit.headers[key]),
        );
        res.write(graphqlResponse);
        res.end();
      },
      (error: HttpQueryError) => {
        if ('HttpQueryError' !== error.name) {
          return next(error);
        }
github apollographql / apollo-server / packages / apollo-server-lambda / src / lambdaApollo.ts View on Github external
const graphqlHandler: lambda.APIGatewayProxyHandler = (
    event,
    context,
    callback,
  ): void => {
    context.callbackWaitsForEmptyEventLoop = false;

    if (event.httpMethod === 'POST' && !event.body) {
      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,
github apollographql / apollo-server / packages / apollo-server-lambda / src / lambdaApollo.ts View on Github external
const graphqlHandler: lambda.APIGatewayProxyHandler = (
    event,
    context,
    callback,
  ): void => {
    if (event.httpMethod === 'POST' && !event.body) {
      return callback(null, {
        body: 'POST body missing.',
        statusCode: 500,
      });
    }
    runHttpQuery([event, context], {
      method: event.httpMethod,
      options: options,
      query:
        event.httpMethod === 'POST'
          ? JSON.parse(event.body)
          : (event.queryStringParameters as any),
      request: {
        url: event.path,
        method: event.httpMethod,
        headers: event.headers as any,
      },
    }).then(
      ({ graphqlResponse, responseInit }) => {
        callback(null, {
          body: graphqlResponse,
          statusCode: 200,