0
0
GraphQLquery~20 mins

Apollo Server setup in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Apollo Server Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Query result from a simple Apollo Server schema

Given this Apollo Server schema and resolver, what will be the output of the query { hello }?

GraphQL
const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    hello: String
  }
`;

const resolvers = {
  Query: {
    hello: () => 'Hello, world!'
  }
};

const server = new ApolloServer({ typeDefs, resolvers });
A{ "data": { "hello": "Hello, world!" } }
B{ "error": "Field 'hello' not found" }
C{ "data": { "hello": "Hi there!" } }
D{ "data": { "greeting": "Hello, world!" } }
Attempts:
2 left
💡 Hint

Check the resolver function for the hello field.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in Apollo Server setup

Which option contains a syntax error in the Apollo Server setup code?

GraphQL
const { ApolloServer, gql } = require('apollo-server');

const typeDefs = gql`
  type Query {
    greet: String
  }
`;

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

const server = new ApolloServer({ typeDefs, resolvers });
Aconst { ApolloServer, gql } = require('apollo-server');
Bconst server = new ApolloServer({ typeDefs, resolvers });
Cconst typeDefs = gql` type Query { greet: String }`
Dconst resolvers = { Query: { greet() => 'Hi!' } };
Attempts:
2 left
💡 Hint

Look at the arrow function syntax inside the resolvers object.

optimization
advanced
2:00remaining
Optimize Apollo Server startup for large schemas

You have a large schema with many types and resolvers. Which option best improves Apollo Server startup performance?

ADisable introspection to speed up server initialization.
BLoad all resolvers and type definitions eagerly at server start.
CUse code splitting and lazy loading for resolvers and typeDefs.
DUse schema stitching to combine multiple small schemas into one.
Attempts:
2 left
💡 Hint

Think about loading only what is needed when it is needed.

🧠 Conceptual
advanced
2:00remaining
Understanding Apollo Server context function

What is the main purpose of the context function in Apollo Server setup?

ATo specify the server port and hostname.
BTo provide a shared object accessible by all resolvers during a request.
CTo define the GraphQL schema types and fields.
DTo handle errors thrown by resolvers.
Attempts:
2 left
💡 Hint

Think about what data resolvers might need access to for each request.

🔧 Debug
expert
3:00remaining
Debugging Apollo Server resolver error

Given this resolver code, what error will Apollo Server return when querying { user(id: "1") { name } } if the user is not found?

GraphQL
const resolvers = {
  Query: {
    user: (_, { id }) => {
      const user = database.findUserById(id);
      if (!user) {
        throw new Error('User not found');
      }
      return user;
    }
  }
};
A{ "data": { "user": null }, "errors": [{ "message": "User not found" }] }
B{ "error": "User not found" }
C{ "data": { "user": { "name": null } } }
D{ "data": null, "errors": [{ "message": "User not found" }] }
Attempts:
2 left
💡 Hint

Recall how Apollo Server handles errors thrown in resolvers.