How to use apollo-server-lambda - 10 common examples

To help you get started, we’ve selected a few apollo-server-lambda 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 hasura / graphql-serverless / aws-lambda-nodejs / src / graphql.js View on Github external
content: content,
                    author_id: author_id
                });
                return article;
            } catch (e) {
                console.log(e);
                throw new Error(e);
            }
        }
    }
};

// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers
// responsible for fetching the data for those types.
const server = new ApolloServer({
    typeDefs,
    resolvers,
    context: ({ event, context }) => ({
        headers: event.headers,
        functionName: context.functionName,
        event,
        context,
    }),
});

exports.handler = server.createHandler({
    cors: {
        origin: '*',
        credentials: true,
        allowedHeaders: 'Content-Type, Authorization'
    },
github Brikl / serverless-apollo-datasource-redis / handler.js View on Github external
type Doc @cacheControl(maxAge: 6000) {
    web_url: String
    snippet: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    searchNYTimes: async (_source, { q }, { dataSources }) => {
      return dataSources.nytimesAPI.searchNYArticle(q);
    }    
  }
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  tracing: true,
  cacheControl: {
    defaultMaxAge: 5,
    stripFormattedExtensions: false,
    calculateCacheControlHeaders: false,
  },
  dataSources: () => {
    return {
      nytimesAPI: new NYTIMESAPI(),
    };
  },  
  cache: new RedisCache({
    connectTimeout: 5000,
    reconnectOnError: function (err) {
github anttiviljami / serverless-type-graphql-boilerplate / src / handler.ts View on Github external
synchronize: true,
      logger: 'advanced-console',
      logging: 'all',
      dropSchema: true,
      cache: true,
    });
  }

  // build TypeGraphQL executable schema
  (global as any).schema = (global as any).schema || await TypeGraphQL.buildSchema({
    resolvers: [RecipeResolver, RateResolver],
    validate: false,
  });
  const schema = (global as any).schema;

  const server = new ApolloServer({ schema });
  server.createHandler()(event, context, callback);
}
github serverless / serverless-graphql / app-backend / dynamodb / handler.js View on Github external
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
  function callbackFilter(error, output) {
    // eslint-disable-next-line no-param-reassign
    output.headers['Access-Control-Allow-Origin'] = '*';
    callback(error, output);
  }

  const handler = graphqlLambda({ schema: myGraphQLSchema });
  return handler(event, context, callbackFilter);
};
github abernix / serverless-graphql-lambda-simple / functions / helpers.js View on Github external
event.path.startsWith("/graphiql")) {

      const endpointURL =
        urlResolve(process.env.ENGINE_PROXY_URL, "/graphql");

      return graphiqlLambda({ endpointURL })(...args);
    }

    const query = queryFromEvent(event);

    // This forces the otherwise Lambda shaped query into what Apollo Server
    // expects (it refuses to process it otherwise!)
    event.httpMethod = 'POST';
    event.body = JSON.stringify({ query });

    return graphqlLambda(method)(...args);
  }
}
github freeCodeCamp / open-api / src / handler.js View on Github external
/* eslint-enable max-len */
  context.callbackWaitsForEmptyEventLoop = false;

  function callbackFilter(error, output) {
    if (!output.headers) {
      output.headers = {};
    }
    // eslint-disable-next-line no-param-reassign
    output.headers['Access-Control-Allow-Origin'] = '*';
    output.headers['Access-Control-Allow-Credentials'] = true;
    output.headers['Content-Type'] = 'application/json';

    callback(error, output);
  }

  const handler = graphqlLambda((event, context) => {
    const { headers } = event;
    const { functionName } = context;

    return {
      schema: graphqlSchema,
      context: {
        headers,
        functionName,
        event,
        context
      }
    };
  });

  try {
    await connectToDatabase();
github serverless / serverless-graphql / app-backend / rds / handler.js View on Github external
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
  context.callbackWaitsForEmptyEventLoop = false;

  function callbackFilter(error, output) {
    if (!output.headers) {
      output.headers = {};
    }
    // eslint-disable-next-line no-param-reassign
    output.headers['Access-Control-Allow-Origin'] = '*';
    callback(error, output);
  }

  const handler = graphqlLambda({ schema: myGraphQLSchema, tracing: true });
  return handler(event, context, callbackFilter);
};
github prisma-labs / graphql-playground / packages / graphql-playground-middleware-lambda / examples / basic / handler.js View on Github external
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
  function callbackFilter(error, output) {
    // eslint-disable-next-line no-param-reassign
    output.headers['Access-Control-Allow-Origin'] = '*'
    callback(error, output)
  }

  const myGraphQLSchema = makeExecutableSchema({ typeDefs, resolvers })

  const handler = graphqlLambda({ schema: myGraphQLSchema })
  return handler(event, context, callbackFilter)
}
github muriloamendola / graphql-serverless / src / handler.js View on Github external
const { graphqlLambda, graphiqlLambda } = require('apollo-server-lambda');
const myGraphQLSchema = require('./graphql/schema');

const graphqlHandler = graphqlLambda({ schema: myGraphQLSchema });
 
const graphiqlHandler = graphiqlLambda({ endpointURL: 'http://localhost:3000/graphql' });

module.exports = {
  graphqlHandler,
  graphiqlHandler
};


// Avoid CORS problem when using Angular Apollo Client for example
/* const graphqlHandler = (event, context, callback) => {
  const callbackFilter = (error, output) => {
    output.headers['Access-Control-Allow-Origin'] = '*';
    callback(error, output);
  };
github serverless / serverless-graphql / app-backend / rest-api / handler.js View on Github external
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
  function callbackFilter(error, output) {
    // eslint-disable-next-line no-param-reassign
    output.headers['Access-Control-Allow-Origin'] = '*';
    callback(error, output);
  }

  const handler = graphqlLambda({ schema: myGraphQLSchema });
  return handler(event, context, callbackFilter);
};