This package exports a (DynamoDBDataSource) class which is used for fetching data from a DynamoDB Table and exposing it via GraphQL within Apollo Server.
To get started, install the apollo-datasource-dynamodb package
# with npm
npm install --save apollo-datasource-dynamodb
# with yarn
yarn add apollo-datasource-dynamodb
To define a data source, extend the DynamoDBDataSource class and pass in the name of the table, the Key schema, and the ClientConfiguration that allows the lib to connect to the DynamoDB.DocumentClient to interact with data in the table. Creating an instance of this class then allows you to utilize the API methods.
Example
Say you have a DynamoDB table called test_data which has a Schema:
As of v1.1.0+, another optional parameter was added to the DynamoDBDataSource class constructor that accepts an intialized DynamoDB.DocumentClient instance and uses this instance in the class instead of initializing a new one.
Here is an example of how to use this param with a class that extends the DynamoDBDataSource:
This paramater was added to allow for use of the library with already initialized DynamoDB.DocumentClient instances for use with dependency injection, etc.
API
getItem
this.getItem(getItemInput, 180)
Returns a single instance of the item being retrieved from the table by the key value. It checks the cache for the record, if the value is found in the cache, it returns the item, otherwise it uses the DynamoDB.DocumentClient.get method to retrieve the item from the table; if a record is found in the table, it is then added to the cache with the passed in ttl.
Returns all scanned records from the table by the scanInput. A scan is different from a query because in a query a portion of the key schema on the table must be provided. A scan allows you to retrieve all items from the table, it also lets you paginate.
Updates the item in the table found by the given key and then uses the update expressions to update the record in the table. These input values are used to build a DocumentClient.UpdateItemInput instance to tells DynamoDB how to update the record.