This will look for a file called graphql.js with two exports: graphqlHandler and graphiqlHandler. It creates two API enpoints:
- /graphql (GET and POST)
- /graphiql (GET)
template.yaml:
```yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
GraphQL:
Type: AWS::Serverless::Function
Properties:
Handler: graphql.graphqlHandler
Runtime: nodejs4.3
Events:
GetRequest:
Type: Api
Properties:
Path: /graphql
Method: get
PostRequest:
Type: Api
Properties:
Path: /graphql
Method: post
GraphQLInspector:
Type: AWS::Serverless::Function
Properties:
Handler: graphql.graphiqlHandler
Runtime: nodejs4.3
Events:
GetRequest:
Type: Api
Properties:
Path: /graphiql
Method: get
```
#### Pacakge source code and dependencies
This 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.
```
aws cloudformation package \
--template-file template.yaml \
--output-template-file serverless-output.yaml \
--s3-bucket <bucket-name>
```
#### Deploy the API
The 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.
To read information about the current request (HTTP headers, HTTP method, body, path, ...) or the current Lambda Context (Function Name, Function Version, awsRequestId, time remaning, ...) use the options function. This way they can be passed to your schema resolvers using the context option.
To 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.