-
Notifications
You must be signed in to change notification settings - Fork 0
/
gateway.js
178 lines (158 loc) · 4.6 KB
/
gateway.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const { ApolloServer, gql } = require('apollo-server');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const fetch = require('node-fetch'); // Import 'node-fetch' correctly
const typeDefs = gql`
type Mutation {
updateBookAndAuthor(bookId: ID!, bookTitle: String!, authorId: ID!, authorName: String!): UpdateResult
}
type UpdateResult {
book: Book
author: Author
}
`;
// Define your schemas and resolvers for the books service
const booksTypeDefs = gql`
type Book {
id: ID!
title: String!
genre: String!
publisher: String!
}
type Query {
books: [Book]
}
type Mutation {
createBook(title: String!, genre: String!, publisher: String!): Book
}
type Mutation {
updateBookAndAuthor(bookId: ID!, authorId: ID!, publisher: String!): UpdateResult
}
type UpdateResult {
book: Book
author: Author
}
type Author {
id: ID!
name: String!
nationality: String!
publisher: String!
}
type Query {
authors: [Author]
}
type Mutation {
createAuthor(name: String!, nationality: String!, publisher: String!): Author
}
`;
const booksResolvers = {
Query: {
books: async () => {
// Fetch data from the JSON API endpoint for books
const response = await fetch('https://6516385909e3260018c989ba.mockapi.io/books');
const data = await response.json();
return data;
},
},
Mutation: {
createBook: async (_, { title, genre, publisher }) => {
// Create a new book using a POST request to the JSON API endpoint
const response = await fetch('https://6516385909e3260018c989ba.mockapi.io/books', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ title, genre, publisher }),
});
const newBook = await response.json();
return newBook;
},
},
Mutation: {
updateBookAndAuthor: async (_, { bookId, authorId, publisher }) => {
// Update the book
const bookResponse = await fetch(`https://6516385909e3260018c989ba.mockapi.io/books/${bookId}`, {
method: 'PUT', // Use PUT to update the existing resource
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ publisher: publisher }),
});
const updatedBook = await bookResponse.json();
// Update the author
const authorResponse = await fetch(`https://6516385909e3260018c989ba.mockapi.io/authors/${authorId}`, {
method: 'PUT', // Use PUT to update the existing resource
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ publisher: publisher }),
});
const updatedAuthor = await authorResponse.json();
return {
book: updatedBook,
author: updatedAuthor,
};
},
},
};
// Define your schemas and resolvers for the authors service
const authorsTypeDefs = gql`
type Author {
id: ID!
name: String!
nationality: String!
publisher: String!
}
type Query {
authors: [Author]
}
type Mutation {
createAuthor(name: String!, nationality: String!, publisher: String!): Author
}
`;
const authorsResolvers = {
Query: {
authors: async () => {
// Fetch data from the JSON API endpoint for authors
const response = await fetch('https://6516385909e3260018c989ba.mockapi.io/authors');
const data = await response.json();
return data;
},
},
Mutation: {
createAuthor: async (_, { name, nationality, publisher }) => {
// Create a new author using a POST request to the JSON API endpoint
const response = await fetch('https://6516385909e3260018c989ba.mockapi.io/authors', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name, nationality, publisher }),
});
const newAuthor = await response.json();
return newAuthor;
},
},
};
// Create executable schemas for each service
const booksSchema = makeExecutableSchema({
typeDefs: booksTypeDefs,
resolvers: booksResolvers,
});
const authorsSchema = makeExecutableSchema({
typeDefs: authorsTypeDefs,
resolvers: authorsResolvers,
});
// Merge the schemas into a single schema
const schema = makeExecutableSchema({
typeDefs: [
booksTypeDefs,
authorsTypeDefs,
// Include other service schemas here if needed
],
resolvers: [booksResolvers, authorsResolvers],
});
// Create an Apollo Server instance with the merged schema
const server = new ApolloServer({ schema });
server.listen().then(({ url }) => {
console.log(`Gateway running at ${url}`);
});