Skip to content

Use an AWS Lambda function as endpoint for your Apollo client.

License

Notifications You must be signed in to change notification settings

dealmore/apollo-link-lambda

 
 

Repository files navigation

Apollo Link Lambda

Current npm version CI status

When your GraphQL endpoint is served by an AWS Lambda function, this Apollo link can be used as an in-place replacement for the HTTPLink. Instead of sending the GraphQL request over HTTP and API Gateway to your Lambda it uses the JavaScript AWS SDK to invoke the GraphQL Lambda directly.

This reduces the network overhead and costs because the requests are routed inside of AWS rather than over the public internet. Internally it creates an invoke event that has the same schema as an API Gateway proxy event.

Functionality of the Apollo Link Lambda

Features

✅  Support for @apollo/client 3.0+

✅  Fully compatible to HTTPLink

✅  Support for AWS API Gateway Events 1.0 & 2.0

Usage

npm i --save @dealmore/apollo-link-lambda   # npm or
yarn add @dealmore/apollo-link-lambda       # yarn

Please note that this package has peerDependencies to @apollo/client, aws-sdk and graphql. So you might need to install this packages too if they are not already installed.

import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
import { createLambdaLink } from '@dealmore/apollo-link-lambda';

const isServer = typeof window === 'undefined';

const client = new ApolloClient({
  ssrMode: isServer,
  link: isServer
    ? createLambdaLink({
        functionName: 'MyLambdaFunc',
      })
    : createHttpLink({
        uri:
          'https://psot142kj1.execute-api.eu-central-1.amazonaws.com/graphql',
      }),
  cache: new InMemoryCache(),
});

Options

Option Default Description
functionName (required) The name of the Lambda function.
Possible name formats:
  • Function name
  • Function ARN
  • Partial ARN
For examples refer to the AWS SDK documentation.
httpMethod POST Sets the type of HTTP method for the invoke event.
Possible values:
  • POST
  • GET
payloadFormatVersion 1.0 Sets the payload format version.
Possible values:
  • 1.0
  • 2.0
headers {} You can add custom headers here that should be included in every request.
{  "key": "value"  }
lambda void Allows to pass in a pre configured Lambda instance.

AWS IAM Policy

To invoke the GraphQL Lambda function make sure that associated AWS account or the AWS role of the client has the permission for the lambda:InvokeFunction action:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "InvokeLambda",
      "Effect": "Allow",
      "Action": "lambda:InvokeFunction",
      "Resource": "arn:aws:lambda:*:*:function:MyLambdaFunc"
    }
  ]
}

License

MIT - see LICENSE for details.

About

Use an AWS Lambda function as endpoint for your Apollo client.

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • TypeScript 91.1%
  • JavaScript 8.9%