Miss any of our Open RFC calls?Watch the recordings here! »

apollo-datasource-lambda

1.0.5 • Public • Published

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.

Install

npm i apollo-datasource-lambda

DownloadsWeekly Downloads

3

Version

1.0.5

License

MIT

Unpacked Size

9.52 kB

Total Files

7

Last publish

Collaborators

  • avatar