<commit_before># GraphQL DynamoDB Connections
[![Build Status](https://secure.travis-ci.org/dowjones/graphql-dynamodb-connections.png)](http://travis-ci.org/dowjones/graphql-dynamodb-connections) [![NPM version](https://badge.fury.io/js/graphql-dynamodb-connections.svg)](http://badge.fury.io/js/graphql-dynamodb-connections)

This is an adapter library that converts DynamoDB-style pagination to
[GraphQL Connection](https://facebook.github.io/relay/graphql/connections.htm)-style pagination.


## Usage

```js
import {
  paginationToParams,
  dataToConnection
} from 'graphql-dynamodb-connections';

const userConnections = {
  type: userConnection,
  args: connectionArgs,
  resolve: ((_, args) => {
    promisifiedDocumentClient.scan({
      TableName: 'users',
      ...paginationToParams(args)
    })
    .then(data => dataToConnection(data));
  })
};
```

## API

  - `paginationToParams(connectionArgs)` -- adapts connection-args to DynamoDB params
  - `dataToConnection(data)` -- converts the data returned by DynamoDB into a [Connection type](https://facebook.github.io/relay/graphql/connections.htm#sec-Connection-Types)


## Related

[GraphQL REST Connections](https://github.com/dowjones/graphql-rest-connections)<commit_msg>updated readme with a more complete example (closes #1) and fixed existing errors (closes #2)<commit_after># GraphQL DynamoDB Connections
[![Build Status](https://secure.travis-ci.org/dowjones/graphql-dynamodb-connections.png)](http://travis-ci.org/dowjones/graphql-dynamodb-connections) [![NPM version](https://badge.fury.io/js/graphql-dynamodb-connections.svg)](http://badge.fury.io/js/graphql-dynamodb-connections)

This is an adapter library that converts DynamoDB-style pagination to
[GraphQL Connection](https://facebook.github.io/relay/graphql/connections.htm)-style pagination.


## Usage

```js
import {
  paginationToParams,
  dataToConnection
} from 'graphql-dynamodb-connections';

const userConnections = {
  type: userConnection,
  args: connectionArgs,
  resolve: ((_, args) => {
    return promisifiedDocumentClient.scan({
      TableName: 'users',
      ...paginationToParams(args)
    })
    .then(data => {
      return dataToConnection(data)
    });
  })
};
```

### And a more complete example using native ES2015 Node.js features

```js
'use strict';

const AWS = require('aws-sdk')

const GraphQLObjectType = require('graphql').GraphQLObjectType
const GraphQLSchema = require('graphql').GraphQLSchema
const GraphQLString = require('graphql').GraphQLString
const GraphQLNonNull = require('graphql').GraphQLNonNull
const GraphQLID = require('graphql').GraphQLID


const connectionArgs = require('graphql-relay').connectionArgs
const connectionDefinitions = require('graphql-relay').connectionDefinitions

const paginationToParams = require('graphql-dynamodb-connections').paginationToParams
const dataToConnection = require('graphql-dynamodb-connections').dataToConnection

const dynamoConfig = {
  sessionToken:    process.env.AWS_SESSION_TOKEN,
  region:          process.env.AWS_REGION
}

const docClient = new AWS.DynamoDB.DocumentClient(dynamoConfig)

const store = {}

const Store = new GraphQLObjectType({
  name: 'Store',
  fields: () => ({
    linkConnection: {
      type: linkConnection.connectionType,
      args: connectionArgs,
      resolve: (_, args) => {
        return docClient.scan(
          Object.assign(
            {},
            {TableName: 'links'},
            paginationToParams(args)
          )
        ).promise().then(dataToConnection)
      }
    }
  })
})

const Link = new GraphQLObjectType({
  name: 'Link',
  fields: () => ({
    id: {
      type: new GraphQLNonNull(GraphQLID),
      resolve: (obj) => obj.id
    },
    title: { type: GraphQLString },
    url: { type: GraphQLString }
  })
})

const linkConnection = connectionDefinitions({
  name: 'Link',
  nodeType: Link
})

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: () => ({
      store: {
        type: Store,
        resolve: () => store
      }
    })
  })
})

module.exports = schema

```

## API

  - `paginationToParams(connectionArgs)` -- adapts connection-args to DynamoDB params
  - `dataToConnection(data)` -- converts the data returned by DynamoDB into a [Connection type](https://facebook.github.io/relay/graphql/connections.htm#sec-Connection-Types)


## Related

[GraphQL REST Connections](https://github.com/dowjones/graphql-rest-connections)
{'fraction_non_alphanumeric': 0.13610924557868817, 'fraction_numerical': 0.001343183344526528, 'mean_word_length': 4.077272727272727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1609600', 'n_tokens_mistral': 1448, 'n_tokens_neox': 1379, 'n_words': 263}