动手做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.
练习Exercises
筛出 148 个练习 · 第 8 / 13 页。Showing 148 · page 8 / 13.__resolveReference 返回 { id: user.id }。 那么 User.orders(user, ...) 里的 user上有哪些属性?
__resolveReference returns { id: user.id }. So which properties does user have inside User.orders(user, ...)?
三个空。第一个是 directive,第二个是标记「这不是我的字段」, 第三个是引用解析要返回什么。
Three blanks. The first is a directive, the second marks a field as not belonging to this service, and the third is what the reference resolver returns.
下面哪一条最准确?
Which of these is most accurate?
batch 函数收到 ids = ['a', 'b', 'c'], 其中 b 在数据库里不存在。下面哪种返回是错的?
The batch function receives ids = ['a', 'b', 'c'], and b does not exist in the database. Which of these return values is wrong?
两个空。第一个要你填对数据源上真实存在的方法名, 第二个要你用 loader 而不是数据源。
Two blanks. The first wants the name of a method that really exists on the data source; the second wants you to go through the loader rather than the data source.
跑 npm test,其中一个 DataLoader 相关的测试挂了。 报错指向 loader 内部。
You run npm test and one of the DataLoader tests fails. The error points inside the loader.
四个 TODO 全都只写了 return [] 或return null,为什么还有 4 个测试通过?
All four TODOs contain nothing but return [] or return null. So why do 4 tests still pass?
createOrderLoader 调了不存在的orderDataSource.getOrderById(id)。 正确的修法是?
createOrderLoader calls orderDataSource.getOrderById(id), which does not exist. What is the right fix?
拿到 node-subgraph,最合理的动作顺序?
You have just opened node-subgraph. What is the most sensible order to work in?
四个空。第 2 个是数据源上的真实方法名, 第 4 个是那行最容易漏的防御。
Four blanks. The second is the method name that really exists on the data source; the fourth is the guard people most often forget.
按 TODO 的三条要求写完整实现。检查器会核对方法名、兜底、 错误处理和 correlation id。
Write the full implementation against the three requirements in the TODO. The checker looks at the method name, the fallback, the error handling and the correlation id.
- 用 user.id 去取该用户的订单Use user.id to fetch that user's orders
- 调用数据源上真实存在的方法Call a method that really exists on the data source
- 绝不返回 null 或 undefined(schema 是 [Order!]!)Never return null or undefined (the schema says [Order!]!)
- 用 try/catch 包住,失败时抛 GraphQLErrorWrap it in try/catch and throw a GraphQLError on failure
- 错误的 extensions 里带 code 和 correlationIdPut code and correlationId in the error's extensions
- 已经是 GraphQLError 的错误要原样往上抛,不要重新包装Rethrow an error that is already a GraphQLError untouched, without rewrapping it
- 日志里带上 correlationIdInclude correlationId in the log line
看答案之前,先确认你已经自己动手写过一遍。看懂别人的答案和自己写出来,是两种能力。Before you open this, make sure you have written it yourself once. Following someone else's answer and producing your own are two different skills.
三个空。第 1 个决定你答不答得到 N+1 考点, 第 3 个决定第二条测试过不过。
Three blanks. The first decides whether you answer the N+1 question at all; the third decides whether the second test passes.