-
Notifications
You must be signed in to change notification settings - Fork 24
/
astronauts.js
42 lines (36 loc) · 972 Bytes
/
astronauts.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
const { ApolloServer, gql } = require("apollo-server");
const { buildSubgraphSchema } = require("@apollo/subgraph");
const fetch = require("node-fetch");
const port = 4001;
const apiUrl = "http://localhost:3000";
const typeDefs = gql`
type Astronaut @key(fields: "id") {
id: ID!
name: String
}
extend type Query {
astronaut(id: ID!): Astronaut
astronauts: [Astronaut]
}
`;
const resolvers = {
Astronaut: {
__resolveReference(ref) {
return fetch(`${apiUrl}/astronauts/${ref.id}`).then(res => res.json());
}
},
Query: {
astronaut(_, { id }) {
return fetch(`${apiUrl}/astronauts/${id}`).then(res => res.json());
},
astronauts() {
return fetch(`${apiUrl}/astronauts`).then(res => res.json());
}
}
};
const server = new ApolloServer({
schema: buildSubgraphSchema([{ typeDefs, resolvers }])
});
server.listen({ port }).then(({ url }) => {
console.log(`Astronauts service ready at ${url}`);
});