Coder Social home page Coder Social logo

incremental-schema-apollo-link's Introduction

Apollo Link for Incremental Schema concept

This Link is super helpful only if you execute GraphQL operations on the client-side. It enables you to lazy-load chunks of schema incrementally to reduce the bundle size.

Setup

Install:

npm install incremental-schema-apollo-link
const map = {
  modules: [
    () => import("./modules/calendar"),
    () => import("./modules/chats"),
    () => import("./modules/blog"),
  ],
  //
  // Create dependencies between modules
  //    dependencies: {
  //      0: [1],
  //      1: [0, 2],
  //    },
  //
  sharedModule: () => import("./modules/shared"),
  types: {
    Query: {
      events: 0,
      chats: 1,
      posts: 2,
    },
    Mutation: {
      addEvent: 0,
    },
    Subscription: {},
  },
  //
  // Allow force pre-loading certain modules on first query
  //   preloadModules: [0, 1]
};

const link = createIncrementalSchemaLink({
  map,
  //
  // Accepts initial resolvers, independent from modules
  //    resolvers: {}, - see src/index.ts
  //
  // Suppports context building
  //    contextBuilder() {}, - see src/index.ts
  //
  // Allows to define root types
  //    schemaDefinition: {} - see src/index.ts
  //
  // Comes with built-in schema builder but you can define your own
  //    schemaBuilder({ typeDefs, resolvers }) {
  //      return makeExecutableSchema({ typeDefs, resolvers });
  //    },
  //
});

Connect it with Apollo Client and you're good to go!

Usage

Take a look at the example below. We call a GraphQL operation to query events.

function Component() {
  useQuery(gql`
    {
      events {
        id
        name
      }
    }
  `);

  // ...
}

Initially, before the component gets mounted, no schema is loaded and the schema modules are not included in the initial bundle. You save up kilobytes.

After component is mounted, the Incremental Schema Link receives an operation and sees that you asked for Query.events which belongs to calendar module (index 0 in modules list). It collects dependencies and dynamically imports everything, builds a schema (memoizes it) and executes the operation. Now your component receives the data.

Everything happens on demand.

Tooling

We don't write the map by hand, it's auto-generated based on our schema. If you're interested in tooling, please reach out to us!

incremental-schema-apollo-link's People

Contributors

dependabot[bot] avatar freiksenet avatar kamilkisiela avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

freiksenet

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.