-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
52 lines (48 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const { ApolloServer } = require('apollo-server')
const { mergeSchemas } = require('graphql-tools')
const schemaLoader = require('./helpers/schema-loader')
/**
* @param {GraqhQLSchema} localSchema
* @param {object} resolvers
* @param {Array<{name: string, url: string, headers: object, onLoaded: function (swaggerJson, service) {}}>} endpointsList
* @param {object} apolloServer
* @param logger,
* @param {object} contextConfig
* @returns {ApolloServer}
*/
module.exports = async ({
localSchema,
resolvers,
endpointsList,
apolloServerConfig,
contextConfig,
logger
}) => {
logger = logger || console
if (!endpointsList) {
throw new Error('The parameter endpointsList is required')
}
// Building list of Schemas by retrieving the remote JSON swagger and adding
// the local Schema (which in theory should contain relations and resolvers to retrieve this relations)
const { resolvedSchemas, services } = await schemaLoader({ localSchema, remoteRestServices: endpointsList })
// Create one Schema from the list of available schemas
const mergedSchemas = mergeSchemas({
schemas: resolvedSchemas,
resolvers
})
// Build Apollo server
return new ApolloServer({
...apolloServerConfig,
schema: mergedSchemas,
context: (integrationContext) => ({
...integrationContext,
...contextConfig,
req: integrationContext.req,
connection: integrationContext.connection,
resolveSchema: (schameName) => {
const remoteSchema = services.find(service => service.name === schameName)
return remoteSchema.schema
}
})
})
}