Skip to content

Lamdba for providing non-sparse paging on top of dynamodb for a graphql api

License

Notifications You must be signed in to change notification settings

ONSdigital/aws-graphql-dynamo-pager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS Graphql DynamoDB Pager

This lambda function implements a pager for a dynamodb datasource. It allows graphql standard compliant paging without the sparse results that would otherwise be returned by the default dynamodb paging behaviour.

Permissions

This repo does not contain IAM policies/roles. To deploy you will need to create suitable policies to access your datasources.

As a minium you'll need the following permissions on the table(s) you wish to access:

  • dynamodb:Scan
  • dyanmodb:DescribeTable

This lambda does not alter any data so does not need any write/update/delete permissions.

For writing CloudWatch logs you'll also require on the appropriate resources:

  • logs:CreateLogGroup
  • logs:CreateLogStream
  • logs:PutLogEvents

Build

The build is standard as you would expect for a Go AWS lambda:

# Assume already in the repo
cd functions/pager
GOOS=linux go build main.go
zip function.zip main

You can now deploy the compiled lambda to AWS as usual, selecting runtime type as Go 1.x

Graphql Resolver

The following gives suitable templates to use for resolvers calling this lambda

Request

Need to supply <TABLE_NAME>

{
  "version" : "2017-02-28",
  "operation": "Invoke",
  "payload": {
    "tableName": "<TABLE_NAME>",
    "filter": $util.transform.toDynamoDBFilterExpression($ctx.args.filter),
    #if($context.args.first) "first": $util.toJson($context.args.first), #end
    #if($context.args.after) "after": $util.toJson($context.args.after), #end
  }
}

Response

No need to JSONify the result as it's passed back correctly encoded by the lambda

$context.result

Example Query

Query:

query list($first: Int, $after: String){
  listAnimals(
    first: $first,
    after: $after,
    filter: {
    species:{
      contains: "panda"
    }
  }){
    pageInfo{
      hasNextPage
      endCursor
    }
    edges{
      cursor
      node {
        id
        fluffy
        species
      }
    }
  }
}

Response:

{
  "data": {
    "listAnimals": {
      "pageInfo": {
        "hasNextPage": true,
        "endCursor": "eyJpZCI6eyJCIjpudWxsLCJCT09MIjpudWxsLCJCUyI6bnVsbCwiTCI6bnVsbCwiTSI6bnVsbCwiTiI6bnVsbCwiTlMiOm51bGwsIk5VTEwiOm51bGwsIlMiOiIxMTExMSIsIlNTIjpudWxsfX0="
      },
      "edges": [
        {
          "cursor": "eyJpZCI6eyJCIjpudWxsLCJCT09MIjpudWxsLCJCUyI6bnVsbCwiTCI6bnVsbCwiTSI6bnVsbCwiTiI6bnVsbCwiTlMiOm51bGwsIk5VTEwiOm51bGwsIlMiOiIxMTExMSIsIlNTIjpudWxsfX0=",
          "node": {
            "id": "11111",
            "fluffy": true,
            "species": "panda"
          }
        }
      ]
    }
  }
}

LICENSE

Copyright (c) 2020 Crown Copyright (Office for National Statistics)

Released under MIT license, see LICENSE for details.

About

Lamdba for providing non-sparse paging on top of dynamodb for a graphql api

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages