Skip to content

Stitch your GraphQL schemas in a declarative way by using annotations.

Notifications You must be signed in to change notification settings

almilo/graphql-needle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-needle

Stitch your GraphQL schemas in a declarative way by using annotations (aka directives).

See Apollo schema stitching documentation

Example

Instead of generating a stitched schema like so:

import fetch from 'node-fetch';
import { HttpLink } from 'apollo-link-http';
import { introspectSchema, makeRemoteExecutableSchema, mergeSchemas } from 'graphql-tools';

const authorLink = new HttpLink({ uri: 'https://8xz15vx5q.lp.gql.zone/graphql', fetch });
const authorSchema = await introspectSchema(authorLink);
const authorExecutableSchema = makeRemoteExecutableSchema({ schema: authorSchema, link: authorLink });

const chirpLink = new HttpLink({ uri: 'https://vpzl4vxr3.lp.gql.zone/graphql', fetch });
const chirpSchema = await introspectSchema(chirpLink);
const chirpExecutableSchema = makeRemoteExecutableSchema({ schema: chirpSchema, link: chirpLink });

const linkTypeDefs = `
  extend type User {
    chirps: [Chirp]
  }
  extend type Chirp {
    author: User
  }
`;

const executableSchema = mergeSchemas({
  schemas: [authorExecutableSchema, chirpExecutableSchema, linkTypeDefs],
  resolvers: mergeInfo => ({
    User: {
      chirps: {
        fragment: `fragment UserFragment on User { id }`,
        resolve(parent, args, context, info) {
          const authorId = parent.id;
          return mergeInfo.delegate(
            'query',
            'chirpsByAuthorId',
            {
              authorId,
            },
            context,
            info,
          );
        },
      },
    },
    Chirp: {
      author: {
        fragment: `fragment ChirpFragment on Chirp { authorId }`,
        resolve(parent, args, context, info) {
          const id = parent.authorId;
          return mergeInfo.delegate(
            'query',
            'userById',
            {
              id,
            },
            context,
            info,
          );
        },
      },
    },
  }),
});

You can do it like so:

import { makeAnnotatedExecutableSchema } from 'graphql-needle';

const authorSchemaUri = 'https://8xz15vx5q.lp.gql.zone/graphql'; // Apollo Launchpad https://launchpad.graphql.com/vpzl4vxr3
const chirpSchemaUri = 'https://vpzl4vxr3.lp.gql.zone/graphql'; // Apollo Launchpad https://launchpad.graphql.com/8xz15vx5q
const annotatedSchema = `
    extend type User @schema(uri: "${authorSchemaUri}") {
      chirps: [Chirp] @stitch(keyField: "id", queryField: "chirpsByAuthorId", queryParameter: "authorId")
    }
    extend type Chirp @schema(uri: "${chirpSchemaUri}") {
      author: User @stitch(keyField: "authorId", queryField: "userById", queryParameter: "id")
    }
`;


const executableSchema = await makeAnnotatedExecutableSchema(annotatedSchema);

See running example

About

Stitch your GraphQL schemas in a declarative way by using annotations.

Resources

Stars

Watchers

Forks

Packages

No packages published