Skip to content

ForestAdmin/graphql-stitcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Stitcher

GraphQL Stitcher Logo

About the project

This project is a human-friendly library used to implement the concept GraphQL Schema Stitching. The goal is to stitch together multiple GraphQL schema in a flash.

A use case could be the need to abstract multiple heterogeneous datasources through a single GraphQL endpoint. This is commonly encountered in large microservices architecture.

Quickstart

Prerequisites

  • NodeJS
  • NPM

Installation

npm install graphql-stitcher

Usage

const stitcher = new GraphQLStitcher();

// "countries" public API
const countrySchema = await stitcher.loadRemoteSchema('https://countries.trevorblades.com');

// Github API
const githubSchema = await stitcher.loadRemoteSchema('https://api.github.com/graphql', {
  headers: { authorization: `Bearer ${process.env.STRIPE_TOKEN}`, },
});

// Local GraphQL schema
const dbSchema = stitche.loadLocalSchema(__dirname + '/graphql');

// Stitch!
const schema = stitcher.stitch();

// … … …

// Example with ApolloServer
const server = new ApolloServer({ schema });

A complete example can be found in the example/ directory.

Adding a remote schema

.loadRemoteSchema(string uri, object opts) -> Promise
  • uri: the remote location of the GraphQL remote schema. Example: 'https://api.github.com/graphql'

  • opts: (optional) a Javascript Object to pass more options to the HTTP request that will fetch the GraphQL schema. Most of the time, it is used to pass authentication details. Example:

     { headers: { authorization: `Bearer ${process.env.STRIPE_TOKEN}` }}

Adding a local schema

.loadLocalSchema(string directory) -> Object

The loadLocalSchema function automatically import all files specified under the parameter passed as an argument directory. Each file should respect the following syntax:

module.exports = function () {
  this.getSchema = function () {
  };

  this.getResolver = function () {
  };
}

A complete example can be found in the example/ directory.

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  • Fork the Project
  • Create your Feature Branch (git checkout -b feature/AmazingFeature)
  • Commit your Changes (git commit -m 'Add some AmazingFeature)
  • Push to the Branch (git push origin feature/AmazingFeature)
  • Open a Pull Request

License

MIT

Contact

This project is currently maintained by Forest Admin. Don't hesitate to join the Slack community if you have any questions: http://community.forestadmin.com

About

Make the implementation of GraphQL Schema Stitching easy.

Resources

License

Stars

Watchers

Forks

Packages

No packages published