UNPKG

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