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';
const chirpSchemaUri = 'https://vpzl4vxr3.lp.gql.zone/graphql';
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