Created
January 21, 2018 01:44
-
-
Save eduardojmatos/1137f0b9bf4f0dc33bcf66607298227b to your computer and use it in GitHub Desktop.
Sample of merge Schemas using stitching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default ` | |
extend type Service { | |
category: Category | |
} | |
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import graphqlTools from 'graphql-tools'; | |
import serverOneFetcher from './server-fetcher-one'; | |
import serverTwoFetcher from './server-fetcher-two'; | |
import SchemaBuilder from './schema-builder'; | |
import serviceStitching from './stitching-service'; | |
import linkServiceCategorySchema from './link-service-with-category'; | |
const schemas = []; | |
const fetchers = { | |
serverOne: serverOneFetcher, | |
serverTwo: serverTwoFetcher, | |
}; | |
export default async () => { | |
try { | |
await Promise.all( | |
Object.values(fetchers).map(async (fetcher) => { | |
schemas.push(await SchemaBuilder(fetcher)); | |
}), | |
); | |
schemas.push(linkServiceCategorySchema); | |
return graphqlTools.mergeSchemas({ | |
schemas, | |
resolvers: mergeInfo => serviceStitching(mergeInfo), | |
}); | |
} catch (error) { | |
console.log('error:', error); | |
return error; | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import graphqlTools from 'graphql-tools'; | |
const schemaBuilder = async fetcher => graphqlTools.makeRemoteExecutableSchema({ | |
schema: await graphqlTools.introspectSchema(fetcher), | |
fetcher, | |
}); | |
export default schemaBuilder; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import apolloFetch from 'apollo-fetch'; | |
import config from './config'; | |
const serverFetcher = apolloFetch.createApolloFetch({ | |
uri: config.serverGraphEndpoint, | |
}); | |
export default serverFetcher; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import apolloFetch from 'apollo-fetch'; | |
import config from './config'; | |
const serverFetcher = apolloFetch.createApolloFetch({ | |
uri: config.serverTwoGraphEndpoint, | |
}); | |
serverFetcher.use(({ options }, next) => { | |
if (!options.headers) { | |
Object.assign(options, { | |
headers: { | |
'api-key': config.serverTwoGraphApiKey, | |
}, | |
}); | |
} | |
next(); | |
}); | |
export default serverFetcher; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const serviceStitching = function (mergeInfo) { | |
return { | |
Service: { | |
category: { | |
fragment: 'fragment CategoryTypeFragment on Category { slug }', | |
resolve(parent, args, context, info) { | |
return mergeInfo.delegate( | |
'query', | |
'category', | |
{ slug: parent.categorySlug }, | |
context, | |
info, | |
); | |
}, | |
}, | |
}, | |
}; | |
}; | |
export default serviceStitching; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment