Last active
December 12, 2019 18:20
-
-
Save erebus1/fa86a41805862c1545517ca9b5002429 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) { | |
const query = ` | |
query($ids: [Int!]!) { | |
files(ids: $ids){ | |
id | |
name | |
url | |
previewUrl | |
} | |
} | |
`; | |
graphql(fileSchema, query, null, context, {ids: keys}) | |
.then((result) => { | |
const fileMap = {}; | |
_.get(result, 'data.files', []).forEach((file) => { | |
fileMap[file.id] = file | |
}); | |
resolve(keys.map((id) => fileMap[id])) | |
}, (error) => { | |
console.error(error); | |
resolve(keys.map((id) => null)) | |
}); | |
}) | |
} | |
} | |
const createRemoteSchema = async (uri: string) => { | |
const http = new HttpLink({ uri, fetch }); | |
const link = setContext((request, previousContext) => ({ | |
// 5. pass headers in underlying services | |
headers: (previousContext.graphqlContext || {}).headers | |
})).concat(http); | |
try { | |
return makeRemoteExecutableSchema({ | |
schema: await introspectSchema(link), | |
link | |
}); | |
}catch (e) { | |
console.error(e); | |
} | |
}; | |
// 1. define underlying services | |
const monolithSchema = await createRemoteSchema(apollo_url); | |
const fileSchema = await createRemoteSchema(file_manager_url); | |
let schemas:any = []; | |
schemas.push(monolithSchema); | |
if (fileSchema) { | |
schemas.push(fileSchema) | |
} | |
// 2. define how schemas joins | |
const linkSchemaDefs = ` | |
extend type MessageNode { | |
file: [FileNode] | |
} | |
`; | |
const resolvers = {}; | |
if (fileSchema && monolithSchema) { | |
schemas.push(linkSchemaDefs); | |
if (typeof(resolvers['MessageNode']) == "undefined") { | |
resolvers['MessageNode'] = {} | |
} | |
resolvers['MessageNode'].file = { | |
fragment: `fragment MessageNodeF on MessageNode {id, files{id}}`, | |
// 3. define how resolver for join | |
resolve(parent: any, args: any, context: any, info: any) { | |
return context.fileLoader.loadMany( | |
parent.files.map((val) => val.id) | |
); | |
} | |
} | |
} | |
let schema = mergeSchemas({ | |
schemas: schemas, | |
resolvers: mergeInfo => (resolvers) | |
}); | |
const app = express(); | |
const server = new ApolloServer({ | |
schema, | |
// 4. add headers and loader in context | |
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