Relay 对 GraphQL 服务器作出三个核心假设,就是它必须提供::
这个例子说明了这三个假设。
T这个范例不是那么的全面,不过它被设计用来快速地介绍这些核心假设,以在深入更细节的库规范 之前提供一些 context。
这个范例的前提是,我们想要使用 GraphQL 来在原始的 Star Wars 三部曲中 query ship 和 faction 的信息。
假设读者已经熟悉GraphQL; 如果没有, GraphQL.js 的README 是一个很好的开始。
我们也假设读者已经熟悉 Star Wars;如果没有的话,Star Wars 的 1977 版本是一个好的起点,虽然 1997 的特别版将成为这份文档的目的。
在下面描述的 schema 將會被用來示範一個被 Relay 使用的 GraphQL 伺服器應該要實作的功能。兩個核心的 type 是在 Star Wars 宇宙中的 faction 和
ship,而一個 faction 有許多的 ship 關聯到它。下面的這個 schema 是 GraphQL.js schemaPrinter 的 output。
schemaPrinter
.
interface Node { id: ID! } type Faction : Node { id: ID! name: String ships: ShipConnection } type Ship : Node { id: ID! name: String } type ShipConnection { edges: [ShipEdge] pageInfo: PageInfo! } type ShipEdge { cursor: String! node: Ship } type PageInfo { hasNextPage: Boolean! hasPreviousPage: Boolean! startCursor: String endCursor: String } type Query { rebels: Faction empire: Faction node(id: ID!): Node } input IntroduceShipInput { factionId: String! shipNamed: String! clientMutationId: String! } type IntroduceShipPayload { faction: Faction ship: Ship clientMutationId: String! } type Mutation { introduceShip(input: IntroduceShipInput!): IntroduceShipPayload }