@@ -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
257184import { 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';
310228import path from 'node:path';
311229import { hotelApi, cityApi } from './api/index.js';
312230import { 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
315235const PORT = 3000;
316236const app = express();
317237app.use(express.json());
318238
319239app.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