Skip to content

Instantly share code, notes, and snippets.

@yusinto
Last active April 28, 2018 10:56
Show Gist options
  • Save yusinto/9ab425fc0a100f81c183e824f1405b57 to your computer and use it in GitHub Desktop.
Save yusinto/9ab425fc0a100f81c183e824f1405b57 to your computer and use it in GitHub Desktop.
Example merged relay and prisma graphql schema.
import {mergeSchemas, makeRemoteExecutableSchema, makeExecutableSchema} from 'graphql-tools';
import {importSchema} from 'graphql-import';
import {HttpLink} from 'apollo-link-http';
import fetch from 'node-fetch';
import localSchema from './localSchema';
const uri = 'https://eu1.prisma.sh/public-nickelwarrior-830/wendarie-prisma/dev';
const link = new HttpLink({uri, fetch});
// remote.schema.graphql is downloaded in the previous step
const remoteTypeDefs = importSchema('./remote.schema.graphql');
const remoteSchema = makeRemoteExecutableSchema({
schema: makeExecutableSchema({typeDefs: remoteTypeDefs}),
link, // GOTCHA: you gotta use apollo link, not fetcher!
});
// Our local schema contains one user with her favourite places.
// Prisma contains the business details of those places.
// We connect our places with prisma's businesses using this link.
const linkTypeDefs = `
extend type Place {
business: Business
}
`;
const result = mergeSchemas({
schemas: [localSchema, remoteSchema, linkTypeDefs],
resolvers: {
Place: {
business: {
fragment: `fragment PlaceFragment on Place { id }`,
resolve: (place, args, context, info) =>
info.mergeInfo.delegateToSchema({
schema: remoteSchema,
operation: 'query',
fieldName: 'business',
args: {
where: {
publicId: place.id, // local Place.id maps to remote Business.publicId
},
},
context,
info,
}),
}
}
}
});
export default result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment