Git Product home page Git Product logo

apollo-server-cache-dynamodb's People

Contributors

dependabot[bot] avatar hotgazpacho avatar jplock avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

apollo-server-cache-dynamodb's Issues

Use apollo-server-cache-dynamodb with aws-sdk v3

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!

Any example of caching?

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.