Skip to content

Commit d1550d2

Browse files
committed
refactor: remove unused GraphQL server files and update schema handling
1 parent 67a1f29 commit d1550d2

File tree

11 files changed

+7903
-127
lines changed

11 files changed

+7903
-127
lines changed

06-rest-api/03-graphql/02-crud/01-graphql-backend/.babelrc

Lines changed: 0 additions & 8 deletions
This file was deleted.

06-rest-api/03-graphql/02-crud/01-graphql-backend/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ test-report.*
66
junit.xml
77
*.log
88
*.orig
9-
package-lock.json
109
yarn.lock
1110
.awcache
11+
.DS_Store

06-rest-api/03-graphql/02-crud/01-graphql-backend/README.md

Lines changed: 32 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ npm install graphql-http graphql --save
3838
3939
# Add Playground
4040

41-
_./src/core/graphql/playground.html_
41+
_./server/src/graphql/playground.html_
4242

4343
```html
4444
<!DOCTYPE html>
@@ -101,6 +101,10 @@ _./src/core/graphql/playground.html_
101101
</html>
102102
```
103103

104+
The fetcher is set to point to the /graphql endpoint, which is where GraphiQL will send requests to
105+
interact with the GraphQL server. To work correctly, the backend must expose a GraphQL API at /graphql,
106+
matching this path.
107+
104108
# Config
105109

106110
- graphql-http comes with types definitions and we don't need an extra package for TypeScript. We have to define a new `graphql-http` instance to create a new `GraphQL Server` and let's create a dummy endpoint to test it:
@@ -132,13 +136,11 @@ app.use('/', express.static(path.resolve(import.meta.dirname, '../public')));
132136
+ },
133137
+ };
134138
+ app.use('/graphql', createHandler({ schema, rootValue: resolvers }));
135-
+ if (!ENV.IS_PRODUCTION) {
136-
+ app.use('/playground', async (req, res) => {
137-
+ res.sendFile(
138-
+ path.join(import.meta.dirname, './core/graphql/playground.html')
139-
+ );
140-
+ });
141-
+ }
139+
+ app.use('/playground', async (req, res) => {
140+
+ res.sendFile(
141+
+ path.join(import.meta.dirname, './core/graphql/playground.html')
142+
+ );
143+
+ });
142144

143145
app.use('/api/hotels', hotelApi);
144146
app.use('/api/cities', cityApi);
@@ -174,89 +176,14 @@ query {
174176
}
175177
```
176178

177-
Let's move GraphQL Server config to `core`:
178-
179-
_./src/core/servers/graphql.server.ts_
180-
181-
```javascript
182-
import path from 'node:path';
183-
import { Express } from 'express';
184-
import { createHandler, HandlerOptions } from 'graphql-http/lib/use/express';
185-
import { ENV } from '#core/constants/index.js';
186-
187-
export const createGraphqlServer = (
188-
expressApp: Express,
189-
options: HandlerOptions
190-
) => {
191-
expressApp.use('/graphql', createHandler(options));
192-
if (!ENV.IS_PRODUCTION) {
193-
expressApp.use('/playground', async (req, res) => {
194-
res.sendFile(
195-
path.join(import.meta.dirname, '../graphql/playground.html')
196-
);
197-
});
198-
}
199-
};
200-
```
201-
202-
Create barrel:
203-
204-
_./src/core/servers/index.ts_
205-
206-
```diff
207-
+ export * from './graphql.server.js';
208-
```
209-
210-
Update app:
211-
212-
_./src/index.ts_
213-
214-
```diff
215-
import express from 'express';
216-
import path from 'node:path';
217-
import { hotelApi, cityApi } from './api/index.js';
218-
- import { createHandler } from 'graphql-http/lib/use/express';
219-
- import { buildSchema } from 'graphql';
220-
- import playground from 'graphql-playground-middleware-express';
221-
- const graphqlPlayground = playground.default;
222-
+ import {
223-
+ createGraphqlServer,
224-
+ } from '#core/servers/index.js';
225-
226-
const PORT = 3000;
227-
const app = express();
228-
app.use(express.json());
229-
230-
app.use('/', express.static(path.resolve(import.meta.dirname, '../public')));
231-
232-
- const schema = buildSchema(`
233-
- type Query {
234-
- hello: String
235-
- }
236-
- `);
237-
- const resolvers = {
238-
- hello: () => {
239-
- return 'Working endpoint!';
240-
- },
241-
- };
242-
- restApiServer.use('/graphql', createHandler({ schema, rootValue: resolvers }));
243-
- if (!envConstants.isProduction) {
244-
- restApiServer.use('/playground', graphqlPlayground({ endpoint: '/graphql' }));
245-
- }
246-
+ createGraphqlServer(app, { schema: null, rootValue: null });
247-
248-
...
249-
250-
```
251-
252179
- Now, we can start implementing `hotel` GraphQL Schema:
253180

254-
### ./server/src/graphql/type-defs.ts
181+
### ./server/src/graphql/schema.ts
255182

256183
```javascript
257184
import { buildSchema as graphql } from 'graphql';
258185

259-
export const typeDefs = graphql(`
186+
export const schema = graphql(`
260187
type Hotel {
261188
id: ID!
262189
type: String!
@@ -292,16 +219,7 @@ export const resolvers = {
292219
};
293220
```
294221

295-
- Let's create `barrel` file:
296-
297-
### ./server/src/graphql/index.ts
298-
299-
```javascript
300-
export * from './type-defs.js';
301-
export * from './resolvers.js';
302-
```
303-
304-
- And use it:
222+
- And use them:
305223

306224
### ./server/src/index.ts
307225

@@ -310,16 +228,32 @@ import express from 'express';
310228
import path from 'node:path';
311229
import { hotelApi, cityApi } from './api/index.js';
312230
import { createGraphqlServer } from '#core/servers/index.js';
313-
+ import { typeDefs, resolvers } from '#graphql/index.js';
231+
- import { buildSchema } from 'graphql';
232+
+ import { schema } from './graphql/schema.js';
233+
+ import { resolvers } from './graphql/resolvers.js';
314234

315235
const PORT = 3000;
316236
const app = express();
317237
app.use(express.json());
318238

319239
app.use('/', express.static(path.resolve(import.meta.dirname, '../public')));
320240

321-
- createGraphqlServer(app, { schema: null, rootValue: null });
322-
+ createGraphqlServer(app, { schema: typeDefs, rootValue: resolvers });
241+
- const schema = buildSchema(`
242+
- type Query {
243+
- hello: String
244+
- }
245+
- `);
246+
- const resolvers = {
247+
- hello: () => {
248+
- return 'Working endpoint!';
249+
- },
250+
- };
251+
app.use('/graphql', createHandler({ schema, rootValue: resolvers }));
252+
app.use('/playground', async (req, res) => {
253+
res.sendFile(
254+
path.join(import.meta.dirname, './core/graphql/playground.html')
255+
);
256+
});
323257

324258
...
325259
```

0 commit comments

Comments
 (0)