apollo-stitcher

Apollo datasource approach to schema stitching. * Straightforward abstraction for extracting, adding and wrapping fields prior to delegation. * Per-request caching using Apollo Client.

Usage no npm install needed!

<script type="module">
  import apolloStitcher from 'https://cdn.skypack.dev/apollo-stitcher';
</script>

README

Features

  • Apollo datasource approach to schema stitching.
  • Straightforward abstraction for extracting, adding and wrapping fields prior to delegation.
  • Per-request caching using Apollo Client.

Installation

npm install graphql apollo-stitcher

Resources

Repository: yaacovCR/apollo-stitcher

API: yaacovcr.github.io/apollo-stitcher

Demo: yaacovCR/nextjs-graphql-starter

Quick Start

Extend the Stitcher class and define methods specific to your data model...

const { Stitcher, stitch } = require('apollo-stitcher');

const wrapInsert = {
  selectionSet: stitch`{
    affected_rows
    returning {
      ...PreStitch
    }
  }`,
  result: result =>
    result && result.affected_rows ? result.returning[0] : null
};

class DbStitcher extends Stitcher {
  delegateToInsertUser(args) {
    return this.transform(wrapInsert).delegateTo({
      operation: 'mutation',
      fieldName: 'insert_user',
      args: {
        objects: [args]
      }
    });
  }
}

...and just add the datasource to your server.

const dataSources = () => {
  return {
    db: new DbStitcher({ dbSchema })
  };
};

Now you can just do this in your resolver:

const user = await context.dataSources.db.delegateToInsertUser({
    email: lowerCaseEmail,
    password: hashedPassword
  });