Coder Social home page Coder Social logo

apollo-datasource-swr's Introduction

Apollo Datasource SWR • latest version release

npm status

An implementation of Apollo Datasource to support SWR caching mechanism.

Features:

  • SWR caching
  • Request deduplication

Installation

npm i -S apollo-datasource-swr

Usage

Make sure to enable decorator support by adding this line in your tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,    // <-- add this
    ...
  },
}
import fetch from "node-fetch";
import { UserInputError } from "apollo-server-express";
import { SWRDataSource } from "apollo-datasource-swr"; // <-- import SWRDataSource

// datasources/movies.ts
class MoviesAPI extends SWRDataSource<Context> {
  private endpoint: string;

  constructor(endpoint: string) {
    super({
      // see https://datatracker.ietf.org/doc/html/rfc5861#section-3
      // for explanation of how MaxAge and SWR behave.
      ttlMaxAge: 10, // MaxAge in seconds.
      ttlSWR: 3600, // SWR TTL in seconds.

      logger: getLogger(), // apollo compatible Logger
    });
    this.endpoint = endpoint;
  }

  @SWRDataSource.useSWR // <-- add this decorator to your fetcher
  async getMovie(movieId: string) {
    const res = await fetch(`${this.endpoint}/movies/${movieId}`);
    const body = await res.json();
    return body as Movie;
  }

  @SWRDataSource.useSWR // <-- add as many methods as you like!
  async getTheatre(theatreId: string) {
    // Apollo request context is available: this.context
    if (!this.context.auth) {
      throw new UserInputError("Not authenticated");
    }

    const res = await fetch(`${this.endpoint}/theatre/${theatreId}`);
    const body = await res.json();
    return body as Theatre;
  }
}

// -------------
// main.ts
// use the data source:
const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => {
    return {
      moviesAPI: new MoviesAPI(), // <-- add it in your Apollo Server
    };
  },
});

apollo-datasource-swr's People

Contributors

andresusanto avatar semantic-release-bot avatar maraisr avatar

Stargazers

Tim Kendall avatar  avatar

Watchers

James Cloos avatar  avatar

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.