UNPKG

6.7 kBMarkdownView Raw
1[![npm version](https://badge.fury.io/js/apollo-server-lambda.svg)](https://badge.fury.io/js/apollo-server-lambda) [![Build Status](https://circleci.com/gh/apollographql/apollo-server/tree/main.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-server) [![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/apollo)
2[![Read CHANGELOG](https://img.shields.io/badge/read-changelog-blue)](https://github.com/apollographql/apollo-server/blob/HEAD/CHANGELOG.md)
3
4
5This is the AWS Lambda integration of GraphQL Server. Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks. [Read the docs](https://www.apollographql.com/docs/apollo-server/v2). [Read the CHANGELOG](https://github.com/apollographql/apollo-server/blob/main/CHANGELOG.md).
6
7```shell
8npm install apollo-server-lambda graphql
9```
10
11## Deploying with AWS Serverless Application Model (SAM)
12
13To deploy the AWS Lambda function we must create a Cloudformation Template and a S3 bucket to store the artifact (zip of source code) and template. We will use the [AWS Command Line Interface](https://aws.amazon.com/cli/).
14
15#### 1. Write the API handlers
16
17In a file named `graphql.js`, place the following code:
18
19```js
20const { ApolloServer, gql } = require('apollo-server-lambda');
21
22// Construct a schema, using GraphQL schema language
23const typeDefs = gql`
24 type Query {
25 hello: String
26 }
27`;
28
29// Provide resolver functions for your schema fields
30const resolvers = {
31 Query: {
32 hello: () => 'Hello world!',
33 },
34};
35
36const server = new ApolloServer({
37 typeDefs,
38 resolvers,
39
40 // By default, the GraphQL Playground interface and GraphQL introspection
41 // is disabled in "production" (i.e. when `process.env.NODE_ENV` is `production`).
42 //
43 // If you'd like to have GraphQL Playground and introspection enabled in production,
44 // the `playground` and `introspection` options must be set explicitly to `true`.
45 playground: true,
46 introspection: true,
47});
48
49exports.handler = server.createHandler();
50```
51
52#### 2. Create an S3 bucket
53
54The bucket name must be universally unique.
55
56```bash
57aws s3 mb s3://<bucket name>
58```
59
60#### 3. Create the Template
61
62This will look for a file called graphql.js with the export `graphqlHandler`. It creates one API endpoints:
63
64* `/graphql` (GET and POST)
65
66In a file called `template.yaml`:
67
68```yaml
69AWSTemplateFormatVersion: '2010-09-09'
70Transform: AWS::Serverless-2016-10-31
71Resources:
72 GraphQL:
73 Type: AWS::Serverless::Function
74 Properties:
75 Handler: graphql.handler
76 Runtime: nodejs12.x
77 Events:
78 AnyRequest:
79 Type: Api
80 Properties:
81 Path: /graphql
82 Method: ANY
83```
84
85#### 4. Package source code and dependencies
86
87This will read and transform the template, created in previous step. Package and upload the artifact to the S3 bucket and generate another template for the deployment.
88
89```shell
90aws cloudformation package \
91 --template-file template.yaml \
92 --output-template-file serverless-output.yaml \
93 --s3-bucket <bucket-name>
94```
95
96#### 5. Deploy the API
97
98This will create the Lambda Function and API Gateway for GraphQL. We use the stack-name `prod` to mean production but any stack name can be used.
99
100```
101aws cloudformation deploy \
102 --template-file serverless-output.yaml \
103 --stack-name prod \
104 --capabilities CAPABILITY_IAM
105```
106
107## Getting request info
108
109To read information about the current request from the API Gateway event (HTTP headers, HTTP method, body, path, ...) or the current Lambda Context (Function Name, Function Version, awsRequestId, time remaining, ...) use the options function. This way they can be passed to your schema resolvers using the context option.
110
111```js
112const { ApolloServer, gql } = require('apollo-server-lambda');
113
114// Construct a schema, using GraphQL schema language
115const typeDefs = gql`
116 type Query {
117 hello: String
118 }
119`;
120
121// Provide resolver functions for your schema fields
122const resolvers = {
123 Query: {
124 hello: () => 'Hello world!',
125 },
126};
127
128const server = new ApolloServer({
129 typeDefs,
130 resolvers,
131 context: ({ event, context }) => ({
132 headers: event.headers,
133 functionName: context.functionName,
134 event,
135 context,
136 }),
137});
138
139exports.handler = server.createHandler();
140```
141
142## Modifying the Lambda Response (Enable CORS)
143
144To enable CORS the response HTTP headers need to be modified. To accomplish this use the `cors` option.
145
146```js
147const { ApolloServer, gql } = require('apollo-server-lambda');
148
149// Construct a schema, using GraphQL schema language
150const typeDefs = gql`
151 type Query {
152 hello: String
153 }
154`;
155
156// Provide resolver functions for your schema fields
157const resolvers = {
158 Query: {
159 hello: () => 'Hello world!',
160 },
161};
162
163const server = new ApolloServer({
164 typeDefs,
165 resolvers,
166});
167
168exports.handler = server.createHandler({
169 cors: {
170 origin: '*',
171 credentials: true,
172 },
173});
174```
175
176To enable CORS response for requests with credentials (cookies, http authentication) the allow origin header must equal the request origin and the allow credential header must be set to true.
177
178```js
179const { ApolloServer, gql } = require('apollo-server-lambda');
180
181// Construct a schema, using GraphQL schema language
182const typeDefs = gql`
183 type Query {
184 hello: String
185 }
186`;
187
188// Provide resolver functions for your schema fields
189const resolvers = {
190 Query: {
191 hello: () => 'Hello world!',
192 },
193};
194
195const server = new ApolloServer({
196 typeDefs,
197 resolvers,
198});
199
200exports.handler = server.createHandler({
201 cors: {
202 origin: true,
203 credentials: true,
204 },
205});
206```
207
208### Cors Options
209
210The options correspond to the [express cors configuration](https://github.com/expressjs/cors#configuration-options) with the following fields(all are optional):
211
212* `origin`: boolean | string | string[]
213* `methods`: string | string[]
214* `allowedHeaders`: string | string[]
215* `exposedHeaders`: string | string[]
216* `credentials`: boolean
217* `maxAge`: number
218
219## Principles
220
221GraphQL Server is built with the following principles in mind:
222
223* **By the community, for the community**: GraphQL Server's development is driven by the needs of developers
224* **Simplicity**: by keeping things simple, GraphQL Server is easier to use, easier to contribute to, and more secure
225* **Performance**: GraphQL Server is well-tested and production-ready - no modifications needed
226
227Anyone is welcome to contribute to GraphQL Server, just read [CONTRIBUTING.md](https://github.com/apollographql/apollo-server/blob/main/CONTRIBUTING.md), take a look at the [roadmap](https://github.com/apollographql/apollo-server/blob/main/ROADMAP.md) and make your first PR!