DrillLab
练习Practice

动手做Get your hands on it

练习跟着课文走 —— 每节课尾都有本课的练习。这一页是全部练习的总库,想集中刷题的时候来。 每个练习都写清了它来自哪一节,卡住了就回去看那一节。Practice follows the lessons — every lesson ends with the exercises for that lesson. This page is the whole library, for when you want to drill in one sitting. Each exercise names the lesson it came from, so you can go back when you stall.

0 / 148个做对过you got right

练习Exercises

筛出 148 个练习 · 第 7 / 13 页。Showing 148 · page 7 / 13.
来自From GraphQL 是什么:一份 schema 加一堆 resolverWhat GraphQL is: one schema plus a set of resolvers · Federation 考试Federation exam
L2填空Fill the blanks补全 schema 的关键声明Fill in the key declarations of the schema

照真实 schema.graphql 补全。 三个空分别关系到「入口类型」「枚举」「输入类型」。

Fill this in from the real schema.graphql. The three blanks are the entry type, the enum and the input type.

GRAPHQLsrc/schema.graphql3 个空3 blanks
1 OrderStatus {
2 PENDING
3 PROCESSING
4 SHIPPED
5 DELIVERED
6 CANCELLED
7}
8
9type {
10 order(id: ID!): Order
11 orders(userId: ID!): [Order!]!
12}
13
14 OrderItemInput {
15 productId: ID!
16 quantity: Int!
17}
把 3 个空都填上才能检查(还差 3 个)Fill all 3 blanks to check (3 to go)
来自From resolver 的四个参数The four arguments of a resolver · Federation 考试Federation exam
L1认出来Spot it从 context 里取订单数据源,正确写法是The right way to read the order data source out of context

index.js 里 context 的真实结构, 哪个写法能拿到订单数据源?

Going by the real shape of context in index.js, which of these reads the order data source?

先选一个选项Pick an option first
来自From resolver 的四个参数The four arguments of a resolver · Federation 考试Federation exam
L1认出来Spot it这个 resolver 该用哪个参数Which argument should this resolver use

Order.shippingInfo 需要知道「是哪个 order 的物流」。 这个 order 的 id 从哪个参数拿?

Order.shippingInfo has to know which order the shipping belongs to. Which argument holds that order's id?

先选一个选项Pick an option first
来自From resolver 的四个参数The four arguments of a resolver · Federation 考试Federation exam
L1排顺序Order it把 resolver 的调用顺序排对Put the resolver calls in the right order

客户端发 { orders(userId:"123") { id shippingInfo { status } } }, 数据源里 user 123 有两个订单。把服务端的动作排序。

The client sends { orders(userId:"123") { id shippingInfo { status } } }, and in the data source user 123 has two orders. Put the server's steps in order.

1对每个 order 分别调 Order.shippingInfo(共 2 次)Call Order.shippingInfo once per order (2 calls in total)
2用 schema 校验查询:字段存不存在、userId 类型对不对Validate the query against the schema: do the fields exist, is the type of userId right
3DataLoader 把 2 次 load 合并成 1 次批量请求DataLoader merges the 2 load calls into 1 batched request
4调 Query.orders,拿到 [order-456, order-457]Call Query.orders and get back [order-456, order-457]
5按查询形状组装 JSON 返回Assemble the JSON response in the shape of the query
来自From 非空、列表,和那个没有 price 的 inputNon-null, lists, and the input that has no price · Federation 考试Federation exam
L1认出来Spot it这个 resolver 找不到数据时该返回什么What should this resolver return when it finds nothing

schema 写的是 orders(userId: ID!): [Order!]!。 user 999 没有任何订单。resolver 该返回什么?

The schema says orders(userId: ID!): [Order!]!. User 999 has no orders at all. What should the resolver return?

先选一个选项Pick an option first
来自From 非空、列表,和那个没有 price 的 inputNon-null, lists, and the input that has no price · Federation 考试Federation exam
L1认出来Spot itcreateOrder 为什么必须查价格Why createOrder has to look up the price

客户端调 createOrder(userId: "789", items: [{ productId: "prod-789", quantity: 2 }])。 如果 resolver 把 items 原样交给orderDataSource.createOrder,会怎样?

The client calls createOrder(userId: "789", items: [{ productId: "prod-789", quantity: 2 }]). What happens if the resolver hands items straight to orderDataSource.createOrder?

先选一个选项Pick an option first
来自From 非空、列表,和那个没有 price 的 inputNon-null, lists, and the input that has no price · Federation 考试Federation exam
L2填空Fill the blanks给四个 TODO 各自选对兜底策略Pick the right fallback for each of the four TODOs

照 schema 的非空标记,给每个 resolver 填上正确的返回表达式。 想清楚「这个字段能不能是 null」。

Go by the non-null markers in the schema and write the right return expression for each resolver. Decide first whether the field is allowed to be null.

JSsrc/resolvers/orderResolvers.js3 个空3 blanks
1// schema: orders: [Order!]!
2async orders(user, _, { dataSources }) {
3 const orders = await dataSources.orderDataSource.getOrdersByUserId(user.id);
4 return orders [];
5}
6
7// schema: shippingInfo: ShippingInfo (可空)
8async shippingInfo(parent, _, { loaders }) {
9 const info = await loaders.shippingInfoLoader.load(parent.id);
10 return info ?? ;
11}
12
13// schema: order(id: ID!): Order (可空,但题目要求找不到时抛结构化错误)
14async order(_, { id }, { loaders, correlationId }) {
15 const order = await loaders.orderLoader.load(id);
16 if () {
17 throw new GraphQLError(`Order not found: ${id}`, {
18 extensions: { code: ErrorCodes.ORDER_NOT_FOUND, correlationId }
19 });
20 }
21 return order;
22}
把 3 个空都填上才能检查(还差 3 个)Fill all 3 blanks to check (3 to go)
来自From 为什么会有 FederationWhy Federation exists · Federation 考试Federation exam
L1认出来Spot itFederation 主要解决的是什么问题What problem does Federation mainly solve

下面哪一条最准确地描述了 Federation 要解决的核心问题?

Which of these describes most accurately the core problem Federation sets out to solve?

先选一个选项Pick an option first
来自From 为什么会有 FederationWhy Federation exists · Federation 考试Federation exam
L1认出来Spot it哪些东西不在这个仓库里What is not in this repository

下面哪些是这个 assessment 仓库里没有的?(多选)

Which of these are not in this exam repository? (Select all that apply.)

这题是多选。More than one answer is correct.

先选一个选项Pick an option first
来自From subgraph 是怎么跑起来的How a subgraph starts up · Federation 考试Federation exam
L1认出来Spot it_entities 这个字段是谁加的Who adds the _entities field

schema.graphql 里从头到尾没有出现_entities,但 Router 能查它。它从哪来?

_entities appears nowhere in schema.graphql, yet the Router can query it. Where does it come from?

先选一个选项Pick an option first
来自From subgraph 是怎么跑起来的How a subgraph starts up · Federation 考试Federation exam
L1认出来Spot it本地怎么验证 federation 部分How to check the Federation part locally

仓库里没有 Router。你想确认自己的 User.orders在 federation 链路里能被正确调用。最直接的办法?

There is no Router in the repository. You want to confirm your User.orders is called correctly along the federation path. What is the most direct way?

先选一个选项Pick an option first
来自From entity、@key 与 __resolveReferenceentity, @key and __resolveReference · Federation 考试Federation exam
L1认出来Spot it@key 在声明什么What @key declares

type User @key(fields: "id") 最准确的含义是?

What does type User @key(fields: "id") mean, most precisely?

先选一个选项Pick an option first