Created
December 12, 2019 17:56
-
-
Save erebus1/1febae94120303f4a60bedfbef9aefa3 to your computer and use it in GitHub Desktop.
This file contains 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 monolith_url: string = process.env.APOLLO_URL | |
const file_manager_url: string = process.env.FILE_MANAGER_URL | |
async function run() { | |
// logic on how extract files from file microservice | |
function getBatchFiles(context) { | |
return function batchFiles(keys: string[]): Promise<Array<{} | null>> { | |
return new Promise(function (resolve, reject) { | |
// query file microservice here | |
}) | |
} | |
} | |
const createRemoteSchema = async (uri: string) => { | |
const http = new HttpLink({ uri, fetch }); | |
const link = setContext((request, previousContext) => ({ | |
headers: (previousContext.graphqlContext || {}).headers | |
})).concat(http); | |
try { | |
return makeRemoteExecutableSchema({ | |
schema: await introspectSchema(link), | |
link | |
}); | |
}catch (e) { | |
console.error(e); | |
} | |
}; | |
const monolithSchema = await createRemoteSchema(apollo_url); | |
const fileSchema = await createRemoteSchema(file_manager_url); | |
let schemas:any = []; | |
schemas.push(monolithSchema); | |
schemas.push(fileSchema) | |
const linkSchemaDefs = ` | |
extend type MessageNode { | |
file: [FileNode] | |
} | |
`; | |
// define how 2 schemas of monolith and filemanager will be combined | |
let schema = mergeSchemas({ | |
schemas: schemas, | |
resolvers: mergeInfo => (resolvers) | |
}); | |
const app = express(); | |
const server = new ApolloServer({ | |
schema, | |
context: function({req, res}) { | |
let context = { | |
headers: req.headers, | |
}; | |
let batchFiles = getBatchFiles(context); | |
context["fileLoader"] = new DataLoader(batchFiles); | |
return context | |
}, | |
playground: true, | |
}); | |
server.applyMiddleware({app}); | |
app.listen(3000); | |
console.log('Server running. Open http://localhost:3000/graphql to run queries.'); | |
} | |
run().catch(e => { | |
console.error(e, e.message, e.stack); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment