3.0.0 • Published 4 years ago

@dealmore/apollo-link-lambda v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

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

OptionDefaultDescription
functionName(required)The name of the Lambda function.Possible name formats:Function nameFunction ARNPartial ARNFor examples refer to the AWS SDK documentation.
httpMethodPOSTSets the type of HTTP method for the invoke event.Possible values:POSTGET
payloadFormatVersion1.0Sets the payload format version.Possible values:1.02.0
headers{}You can add custom headers here that should be included in every request.{ "key": "value" }
lambdavoidAllows 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.