📖 Scenario: You are building a simple web server that uses GraphQL to handle requests. This server will run on Express, a popular web framework for Node.js. You want to set up a GraphQL endpoint where clients can send queries and get data about books.
🎯 Goal: Create a basic Express server with a GraphQL endpoint at /graphql. Define a simple GraphQL schema with a Book type and a query to get a list of books. Connect the schema to the Express server using the express-graphql middleware.
📋 What You'll Learn
Create an Express server instance
Define a GraphQL schema with a Book type having id, title, and author fields
Create a root query type with a books field returning a list of books
Use express-graphql middleware to connect the schema to the Express server at /graphql
Provide a sample list of books as data
💡 Why This Matters
🌍 Real World
Many modern web applications use GraphQL with Express to create flexible APIs that clients can query for exactly the data they need.
💼 Career
Understanding how to integrate GraphQL with Express is a valuable skill for backend developers building APIs for web and mobile apps.
Progress0 / 4 steps
1
Set up Express server and sample data
Create an Express server by importing express and calling express(). Then create a constant called books that is an array of objects with these exact entries: { id: '1', title: '1984', author: 'George Orwell' } and { id: '2', title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' }.
GraphQL
Hint
Use require('express') to import Express and call express() to create the app. Define books as an array with two objects exactly as shown.
2
Define GraphQL schema with Book type and Query
Import buildSchema from graphql. Create a constant called schema using buildSchema with this exact schema string: a Book type with id, title, and author fields all of type String!, and a Query type with a books field that returns a list of Book objects.
GraphQL
Hint
Use buildSchema to define the GraphQL schema string exactly as described. Include the Book type and the Query type with the books field.
3
Create root resolver with books query
Create a constant called root that is an object with a books function. This function should return the books array defined earlier.
GraphQL
Hint
Define root as an object with a books function that returns the books array.
4
Connect GraphQL schema to Express with express-graphql
Import graphqlHTTP from express-graphql. Use app.use to add middleware at '/graphql' with graphqlHTTP passing an object with schema, rootValue set to root, and graphiql set to true. Finally, call app.listen on port 4000.
GraphQL
Hint
Import graphqlHTTP from express-graphql. Use app.use to add the GraphQL middleware at /graphql. Pass the schema, rootValue, and enable graphiql. Finally, start the server on port 4000.
Practice
(1/5)
1. What is the main purpose of using express-graphql middleware in an Express app?
easy
A. To create a new database connection
B. To connect a GraphQL schema to the Express server for handling HTTP requests
C. To serve static files like images and CSS
D. To replace Express with a GraphQL server
Solution
Step 1: Understand the role of middleware in Express
Middleware functions in Express handle requests and responses, adding features to the server.
Step 2: Identify what express-graphql does
This middleware connects a GraphQL schema and resolvers to Express, enabling GraphQL queries over HTTP.
Final Answer:
To connect a GraphQL schema to the Express server for handling HTTP requests -> Option B
Quick Check:
express-graphql middleware = Connect GraphQL to Express [OK]
Hint: Middleware connects GraphQL schema to Express HTTP server [OK]
Common Mistakes:
Thinking it creates a database connection
Confusing it with static file serving
Assuming it replaces Express entirely
2. Which of the following is the correct way to use express-graphql middleware in an Express app?
easy
A. app.get('/graphql', graphqlHTTP(mySchema));
B. app.use('/graphql', graphqlHTTP(schema, rootResolver));
C. app.post('/graphql', graphqlHTTP({ rootValue: rootResolver }));
D. app.use('/graphql', graphqlHTTP({ schema: mySchema, rootValue: rootResolver, graphiql: true }));
Solution
Step 1: Recall the correct syntax for express-graphql middleware
The middleware is used with app.use and takes an options object with keys like schema, rootValue, and graphiql.
Step 2: Check each option for correct usage
app.use('/graphql', graphqlHTTP({ schema: mySchema, rootValue: rootResolver, graphiql: true })); correctly uses app.use with an options object including schema, rootValue, and graphiql. Others either misuse method or pass wrong arguments.
5. You want to add a new Query field user(id: ID!): User to your GraphQL schema in Express. The User type has fields id and name. Which of the following shows the correct way to extend the schema and resolver for this, assuming you have a getUserById function?
hard
A. Schema: type Query { user(id: String): User } and resolver: { user: (id) => getUserById(id) }
B. Schema: type Query { user: User } and resolver: { user: () => getUserById() }
C. Schema: type Query { user(id: ID!): User } and resolver: { user: ({id}) => getUserById(id) }
D. Schema: type Query { user(id: ID!): User } and resolver: { user: (args) => getUserById(args) }
Solution
Step 1: Define schema with required argument and User type
The schema must declare user(id: ID!): User to require an ID argument and return a User object.