サーバレス構成でGraphQLを使うにはどうすればよいか?を調べていたところ、serverless framework にExampleを見つけたので、動かしてみます。
- インストール
serverless frameworkをインストールし、serverless-graphql-apiをインストールします。
npm install serverless -g
git clone https://github.com/boazdejong/serverless-graphql-api
cd serverless-graphql-api
npm install
Resourceのリージョンの指定がデフォルト(us-east-1
)になっています。
必要であれば、serverless.yml
を動作させるリージョンに修正してください。
serverless.yml
6 provider:
7 name: aws
8 runtime: nodejs4.3
9 iamRoleStatements:
10 - Effect: Allow
11 Action:
12 - dynamodb:DescribeTable
13 - dynamodb:Query
14 - dynamodb:Scan
15 - dynamodb:GetItem
16 - dynamodb:PutItem
17 - dynamodb:UpdateItem
18 - dynamodb:DeleteItem
19 Resource: arn:aws:dynamodb:us-east-1:*:*
東京リージョンに変更する場合は、9行目に region: ap-northeast-1
を1行追加し、19行目のus-east-1
をap-northeast-1
に変更してください。
- デプロイ
npm run deploy
deployが終了すると以下のように出力されます。
Serverless: Stack update finished...
Service Information
service: graphql-api
stage: dev
region: us-west-1
api keys:
None
endpoints:
POST - https://xxxxxxx.execute-api.us-west-1.amazonaws.com/dev/graphql
functions:
graphql: graphql-api-dev-graphql
Stack Outputs
GraphqlLambdaFunctionQualifiedArn: arn:aws:lambda:us-west-1:nnnnnnnnnn:function:graphql-api-dev-graphql:2
ServiceEndpoint: https://xxxxxxx.execute-api.us-west-1.amazonaws.com/dev
ServerlessDeploymentBucketName: graphql-api-dev-serverlessdeploymentbucket-xxxxxxxxx
Service Informationの endpointsに GraphiQL.appなどでアクセスし、動作することを確認してください。
今回動かしてみたserverless-graphql-apiですが、Apolloのgraphql-server-lambdaベースで実装されています。