0
0
GraphQLquery~20 mins

Express integration in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express GraphQL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this Express GraphQL server response?

Consider this Express server setup using GraphQL:

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

const schema = buildSchema(`
  type Query {
    hello: String
  }
`);

const root = {
  hello: () => 'Hello world!'
};

const app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: false
}));

app.listen(4000);

If a client sends the query { hello } to /graphql, what is the JSON response?

A{"message":"Hello world!"}
B{"error":"Field 'hello' not found"}
C{"data":{"hello":null}}
D{"data":{"hello":"Hello world!"}}
Attempts:
2 left
💡 Hint

Remember that GraphQL responses wrap data inside a data field.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets up GraphQL middleware in Express?

Which of the following code snippets correctly integrates GraphQL middleware into an Express app?

Aapp.get('/graphql', graphqlHTTP({ schema, rootValue, graphiql: true }));
Bapp.post('/graphql', graphqlHTTP({ schema: schema, root: rootValue }));
Capp.use('/graphql', graphqlHTTP({ schema, rootValue, graphiql: true }));
Dapp.use('/graphql', graphqlHTTP(schema, rootValue, graphiql: true));
Attempts:
2 left
💡 Hint

GraphQL middleware is usually added with app.use and takes an options object.

optimization
advanced
2:00remaining
How to optimize GraphQL schema building in Express for performance?

You notice your Express server rebuilds the GraphQL schema on every request, causing slow responses. Which approach optimizes this?

ABuild the schema once outside the request handler and reuse it for all requests.
BBuild the schema inside the middleware function on every request.
CUse a new schema instance for each query to avoid caching issues.
DDisable schema caching to ensure fresh data every time.
Attempts:
2 left
💡 Hint

Think about expensive operations and how often they should run.

🔧 Debug
advanced
2:00remaining
Why does this Express GraphQL server return a 404 error?

Given this Express setup:

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');

const schema = buildSchema(`
  type Query {
    greet: String
  }
`);

const root = {
  greet: () => 'Hi!'
};

const app = express();

app.get('/graphql', graphqlHTTP({ schema, rootValue: root, graphiql: true }));

app.listen(3000);

When a client sends a POST request to /graphql, the server responds with 404 Not Found. Why?

AThe middleware is only attached to GET requests, so POST requests are not handled.
BThe schema is invalid causing the server to reject POST requests.
CThe root resolver is missing a POST handler.
DThe server is not listening on port 3000.
Attempts:
2 left
💡 Hint

Check which HTTP methods the middleware is attached to.

🧠 Conceptual
expert
2:00remaining
What is the role of the rootValue in Express GraphQL integration?

In Express GraphQL middleware, what does the rootValue option represent?

AIt defines the GraphQL schema structure and types.
BIt provides resolver functions for the fields in the GraphQL schema.
CIt configures the HTTP server settings for Express.
DIt enables the GraphiQL interface for testing queries.
Attempts:
2 left
💡 Hint

Think about what resolves the data for queries.