-
Notifications
You must be signed in to change notification settings - Fork 24
/
missions.js
60 lines (52 loc) · 1.3 KB
/
missions.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
const { ApolloServer, gql } = require("apollo-server");
const { buildSubgraphSchema } = require("@apollo/subgraph");
const fetch = require("node-fetch");
const port = 4002;
const apiUrl = "http://localhost:3000";
const typeDefs = gql`
type Mission {
id: ID!
crew: [Astronaut]
designation: String!
startDate: String
endDate: String
}
extend type Astronaut @key(fields: "id") {
id: ID! @external
missions: [Mission]
}
extend type Query {
mission(id: ID!): Mission
missions: [Mission]
}
`;
const resolvers = {
Astronaut: {
async missions(astronaut) {
const res = await fetch(`${apiUrl}/missions`);
const missions = await res.json();
return missions.filter(({ crew }) =>
crew.includes(parseInt(astronaut.id))
);
}
},
Mission: {
crew(mission) {
return mission.crew.map(id => ({ __typename: "Astronaut", id }));
}
},
Query: {
mission(_, { id }) {
return fetch(`${apiUrl}/missions/${id}`).then(res => res.json());
},
missions() {
return fetch(`${apiUrl}/missions`).then(res => res.json());
}
}
};
const server = new ApolloServer({
schema: buildSubgraphSchema([{ typeDefs, resolvers }])
});
server.listen({ port }).then(({ url }) => {
console.log(`Missions service ready at ${url}`);
});