hotgazpacho / apollo-server-cache-dynamodb Goto Github PK
View Code? Open in Web Editor NEWDynamoDB implementation of KeyValueCache for apollo-server-caching
License: MIT License
DynamoDB implementation of KeyValueCache for apollo-server-caching
License: MIT License
It appears like this library will shallow all errors thrown by DynamoDB which makes troubleshooting difficult.
https://github.com/hotgazpacho/apollo-server-cache-dynamodb/blob/master/src/index.ts#L56
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
I'm implementing a project using aws-sdk v3. Is it possible to use with v3?
As I can see, there are some references to v2 like import DynamoDB = require('aws-sdk/clients/dynamodb')
.
Thanks!
I'm trying to implement this package into my API, however I don't understand how to actually cache the query results on dynamodb.
I've setup my dynamoDB client and cache options like this
const DynamoDB = require("aws-sdk/clients/dynamodb");
const { DynamoDBCache } = require("apollo-server-cache-dynamodb");
const dynamoDBClient = new DynamoDB.DocumentClient();
const cacheOptions = {
tableName: "Test-Apollo-Cache",
partitionKeyName: "Query",
valueAttribute: "Response",
ttlAttribute: "TTL",
defaultTTL: 300
};
And in the apollo lambda server definition I got this
const app = new ApolloServer({
schema,
playground: true,
context: async ({ context }) => {
context.callbackWaitsForEmptyEventLoop = false;
var dataloaderFactory = new MongooseDataloaderFactory();
return { ...context, dataloaderFactory };
},
cache: new DynamoDBCache(dynamoDBClient, cacheOptions),
dataSources: () => ({
productPages: new data.ProductPagesAPI(),
products: new data.ProductAPI()
})
});
The APIs are all subclasses derived from this base class, which looks like this
const { Types } = require("mongoose");
class BaseAPI {
constructor(model) {
this.model = model;
}
getOneById(id, ctx) {
if (!id || !Types.ObjectId.isValid(_id)) {
return null;
}
return this.getOneByField("_id", id, ctx);
}
getManyById(ids, ctx) {
if (!ids) {
return null;
}
return this.getManyByField(
"_id",
ids.filter(_id => Types.ObjectId.isValid(_id)),
ctx
);
}
getOneByField(field, value, ctx) {
return ctx.dataloaderFactory
.mongooseLoader(this.model)
.dataloader(field)
.load(value);
}
getManyByField(field, values, ctx) {
return ctx.dataloaderFactory
.mongooseLoader(this.model)
.dataloader(field)
.loadMany(values);
}
async get(args, ctx, isArray) {
let filter = args.filter;
if (filter && Object.keys(filter).length === 1) {
let indexes = await this.model.collection.getIndexes({ full: true });
indexes = indexes
.map(_index => _index.key)
.map(_index => Object.keys(_index)[0]);
let key = Object.keys(filter)[0];
let value = Object.values(filter)[0];
if (indexes.includes(key)) {
return isArray
? this.getManyByField(key, [value], ctx)
: this.getOneByField(key, value, ctx);
}
}
return isArray
? this.model
.find(filter)
.limit(args.limit)
.skip(args.skip)
.sort(args.sort)
.lean()
: this.model.findOne(filter).lean();
}
}
module.exports = BaseAPI;
What should I wrote (if any) here to cache the response on dynamodb?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.