-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (58 loc) · 1.87 KB
/
server.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { ApolloServer } from "@apollo/server"
import { startStandaloneServer } from "@apollo/server/standalone"
import { schemaFromExecutor, wrapSchema } from "@graphql-tools/wrap"
import { buildHTTPExecutor } from "@graphql-tools/executor-http"
import { mergeSchemas } from "@graphql-tools/schema" // this version don't merge the schemas
// import { mergeSchemas } from "graphql-tools" // graphql-tools ^2.9.7 (It merge the schemas)
async function mergedSchema() {
const starwars = "https://swapi-graphql.netlify.app/.netlify/functions/index"
const rickandmorty = "https://rickandmortyapi.com/graphql"
// starwars schema
const remoteExecutor1 = buildHTTPExecutor({
endpoint: starwars,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
const schema1 = await schemaFromExecutor(remoteExecutor1)
const executableSchema1 = wrapSchema({
schema: schema1,
executor: remoteExecutor1,
})
// rickandmorty schema
const remoteExecutor2 = buildHTTPExecutor({
endpoint: rickandmorty,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
})
const schema2 = await schemaFromExecutor(remoteExecutor2)
const executableSchema2 = wrapSchema({
schema: schema2,
executor: remoteExecutor2,
})
// Merge the schemas
const mergedSchema = mergeSchemas({
/* executableSchema1 will be overwritten by executableSchema2
This will happen using @graphql-tools v10 but not with the v4
* */
schemas: [executableSchema1, executableSchema2],
})
return mergedSchema
}
const runServer = async () => {
const server = new ApolloServer({
schema: await mergedSchema(),
})
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
})
console.log(`🚀 Server ready at ${url}`)
}
try {
runServer()
} catch (error) {
console.error(error)
}