LoginSignup
5
4

More than 5 years have passed since last update.

AWS のAPI Gateway + Lambda を使って GraphQL をやってみる

Last updated at Posted at 2017-07-09

サーバレス構成でGraphQLを使うにはどうすればよいか?を調べていたところ、serverless framework にExampleを見つけたので、動かしてみます。

serverless-graphql-api


  • インストール

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-1ap-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ですが、Apollographql-server-lambdaベースで実装されています。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4