Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including apollo-datasource-lambda with all npm packages installed. Try it out:

require("graphql/package.json"); // graphql is a peer dependency. var apolloDatasourceLambda = require("apollo-datasource-lambda")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

apollo-datasource-lambda v1.0.5

AWS Lambda DataSource for Apollo GraphQL projects

npm version

AWSLambdaDataSource

Use AWS Lambda as an Apollo DataSources.

Based on SQLDataSource.

Installation

To install AWSLambdaDataSource:

npm i apollo-datasource-lambda

Or if you prefer yarn

yarn add apollo-datasource-lambda

Usage

invoke(payload, ttl = 5):

payload: Buffer.from('...') || 'STRING_VALUE' Strings will be Base-64 encoded on your behalf

ttl: the number of seconds to retain the data in the cache (DEFAULT: 5)

Configure Apollo:

const { AWSLambdaDataSource } = require("apollo-datasource-lambda");

const lambdaAwesome = new AWSLambdaDataSource(
  { region: "us-west-2" },
  { FunctionName: "my-awesome-function" }
);

const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache,
  context,
  dataSources: () => ({ lambdaAwesome })
});

Use:

const TTL_ONE_MINUTE = 60;

class Awesome {
  static async getAwesomeMessage(source, args, { dataSources }) {
    const event = {
      question: "What is the meaning of life?"
    };

    // Returns Promise as if you called AWS.Lambda().invoke().promise()
    const response = await dataSources.lambdaAwesome.invoke(
      JSON.stringify(event),
      TTL_ONE_MINUTE
    );

    return response["Payload"];
  }
}

module.exports = Awesome;

Configuration

If no cache is provided in your Apollo server configuration, AWSLambdaDataSource falls back to the same InMemoryLRUCache leveraged by Apollo's RESTDataSource.

Instance Variables

context

The context from your Apollo server.

lambda

The AWS.Lambda instance.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free