-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriptions.js
97 lines (88 loc) · 2.45 KB
/
subscriptions.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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 Subscriptions {
id: ID!
userList: [User]
price: String!
frequency: String!
orderDate: String
shippingDate: String
billingAddress: String
}
extend type User @key(fields: "id") {
id: ID! @external
subscriptions: [Subscriptions]
}
extend type Query {
subscription(id: ID!): Subscriptions
subscriptions: [Subscriptions]
}
`;
// extend type Mutation {
// updateSubscription(id: ID!, subPayload: SubPayload): Subscriptions
// }
// input SubPayload {
// id: ID
// price: String!
// frequency: String!
// orderDate: String!
// shippingDate: String!
// billingAddress: String!
// }
const resolvers = {
User: {
async subscriptions(user) {
const res = await fetch(`${apiUrl}/subscriptions`);
const subscriptions = await res.json();
return subscriptions.filter(({ userList }) =>
userList.includes(parseInt(user.id))
);
},
},
Subscriptions: {
userList(subscription) {
return subscription.userList.map((id) => ({ __typename: "User", id }));
},
},
Query: {
subscription(_, { id }) {
return fetch(`${apiUrl}/subscriptions/${id}`).then((res) => res.json());
},
subscriptions() {
return fetch(`${apiUrl}/subscriptions`).then((res) => res.json());
},
},
// Mutation: {
// updateSubscription: async (_, { id, subPayload }) => {
// console.log(id, subPayload);
// try {
// const currentSubs = await fetch(`${apiUrl}/subscriptions`).then((res) =>
// res.json()
// );
// if (id === currentSubs.id) {
// console.log("QQQQQQQ id already exists");
// }
// } catch (error) {
// return await fetch(`${apiUrl}/subscriptions`, {
// method: "POST",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify(subPayload),
// })
// .then((res) => res.json())
// .then((res) => console.log("&&&&&&", res));
// }
// },
// },
};
const server = new ApolloServer({
schema: buildSubgraphSchema([{ typeDefs, resolvers }]),
});
server.listen({ port }).then(({ url }) => {
console.log(`subscriptions service ready at ${url}`);
});