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.
✅ Support for @apollo/client
3.0+
✅ Fully compatible to HTTPLink
✅ Support for AWS API Gateway Events 1.0 & 2.0
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
andgraphql
. 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(),
});
Option | Default | Description |
---|---|---|
functionName |
(required) | The name of the Lambda function. Possible name formats:
|
httpMethod |
POST |
Sets the type of HTTP method for the invoke event. Possible values:
|
payloadFormatVersion |
1.0 |
Sets the payload format version. Possible values:
|
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. |
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"
}
]
}
MIT - see LICENSE for details.