Relay中的nodeInterface、nodeField、nodeDefinitions是什么?
What is nodeInterface, nodeField and nodeDefinitions in Relay?
我目前正在学习 facebook relayjs 教程,我需要帮助来理解教程的这一部分,它指出
Next, let's define a node interface and type. We need only provide a
way for Relay to map from an object to the GraphQL type associated
with that object, and from a global ID to the object it points to
const {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
const {type, id} = fromGlobalId(globalId);
if (type === 'Game') {
return getGame(id);
} else if (type === 'HidingSpot') {
return getHidingSpot(id);
} else {
return null;
}
},
(obj) => {
if (obj instanceof Game) {
return gameType;
} else if (obj instanceof HidingSpot) {
return hidingSpotType;
} else {
return null;
}
}
);
关于 nodeDefinition 的第一个参数,它从哪里获得它的 globalId? Game 和 HidingSpot 是 GraphQLSchema 上的名称吗?这个 'const {type, id} = fromGlobalId(globalId);' 是做什么的?还有第二个论点是什么?我需要帮助理解 nodeDefinitions,不知何故我在官方文档中找不到 nodeDefinitions。谢谢。
中继使用全局对象标识,这意味着,在我的理解中,如果您的应用程序曾经尝试搜索对象。在您的示例中,尝试寻找游戏,或尝试寻找 hidingSpot。中继将尝试在标准节点接口中获取对象。即通过游戏的 {id: 123} 查找,或通过 hidingSpot 的 {id:abc} 查找。如果你的schema(Game, HidingSpot)没有设置节点接口,Relay将无法获取对象。
因此,如果您的应用程序需要在 "Game" 中进行搜索,则需要在模式中定义节点接口。
通过使用 graphql-relay
帮助程序,在您的应用程序中仅使用一次 nodeDefinitions
函数,基本上将全局定义的 ID 映射到实际数据对象及其 GraphQL 类型。
第一个参数接收globalId,我们将globalId映射到对应的数据对象中。并且 globalId 实际上可以用于使用 fromGlobalId
函数读取对象的类型。
第二个函数接收结果对象,Relay 使用该对象将对象映射到其 GraphQL 数据类型。所以如果对象是Game的实例,它将return gameType等
希望对您的理解有所帮助。我也在学习中
如果你正在编写一个没有 Relay 的 GraphQL 服务器,你会在 Query 类型上定义一些入口点,例如:
type Query {
picture(id: Int!): Picture
user(id: Int!): User
...etc
}
因此,当您想要获取用户时,您可以轻松获取它,因为 user
可用作图表的入口点。当您为 page/screen 构建查询时,它通常会深入几个级别,您可能会去 user -> followers -> pictures
.
有时您希望能够仅重新获取部分查询,也许您正在通过连接分页,或者您有 运行 变更。 Relay 的 Node 接口所做的是为您提供一种标准方法来获取 any 类型,该类型通过全局唯一 ID 实现它。 Relay 能够在其查询中识别此类节点,并在可能的情况下使用它们来提高重新获取和分页的效率。我们在根Query类型中添加节点类型:
type Query {
picture(id: Int!): Picture
user(id: Int!): User
...etc
node(id: ID!): Node
}
现在 nodeDefinitions
。这个函数本质上让我们定义了两件事:
- 如何 return 给定对象的 globalId。
- 如何 return 给定对象的类型。
第一个用于获取节点字段的 ID 参数并使用它来解析对象。第二个允许您的 GraphQL 服务器计算出哪种类型的对象被 returned - 这是必要的,以便我们能够在查询节点时定义特定类型的片段,以便我们可以实际获取我们的数据想。否则,我们将无法成功执行如下查询:
query Test {
node(id: 'something') {
...fragment on Picture {
url
}
...fragment on User {
username
}
}
}
我目前正在学习 facebook relayjs 教程,我需要帮助来理解教程的这一部分,它指出
Next, let's define a node interface and type. We need only provide a way for Relay to map from an object to the GraphQL type associated with that object, and from a global ID to the object it points to
const {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
const {type, id} = fromGlobalId(globalId);
if (type === 'Game') {
return getGame(id);
} else if (type === 'HidingSpot') {
return getHidingSpot(id);
} else {
return null;
}
},
(obj) => {
if (obj instanceof Game) {
return gameType;
} else if (obj instanceof HidingSpot) {
return hidingSpotType;
} else {
return null;
}
}
);
关于 nodeDefinition 的第一个参数,它从哪里获得它的 globalId? Game 和 HidingSpot 是 GraphQLSchema 上的名称吗?这个 'const {type, id} = fromGlobalId(globalId);' 是做什么的?还有第二个论点是什么?我需要帮助理解 nodeDefinitions,不知何故我在官方文档中找不到 nodeDefinitions。谢谢。
中继使用全局对象标识,这意味着,在我的理解中,如果您的应用程序曾经尝试搜索对象。在您的示例中,尝试寻找游戏,或尝试寻找 hidingSpot。中继将尝试在标准节点接口中获取对象。即通过游戏的 {id: 123} 查找,或通过 hidingSpot 的 {id:abc} 查找。如果你的schema(Game, HidingSpot)没有设置节点接口,Relay将无法获取对象。
因此,如果您的应用程序需要在 "Game" 中进行搜索,则需要在模式中定义节点接口。
通过使用 graphql-relay
帮助程序,在您的应用程序中仅使用一次 nodeDefinitions
函数,基本上将全局定义的 ID 映射到实际数据对象及其 GraphQL 类型。
第一个参数接收globalId,我们将globalId映射到对应的数据对象中。并且 globalId 实际上可以用于使用 fromGlobalId
函数读取对象的类型。
第二个函数接收结果对象,Relay 使用该对象将对象映射到其 GraphQL 数据类型。所以如果对象是Game的实例,它将return gameType等
希望对您的理解有所帮助。我也在学习中
如果你正在编写一个没有 Relay 的 GraphQL 服务器,你会在 Query 类型上定义一些入口点,例如:
type Query {
picture(id: Int!): Picture
user(id: Int!): User
...etc
}
因此,当您想要获取用户时,您可以轻松获取它,因为 user
可用作图表的入口点。当您为 page/screen 构建查询时,它通常会深入几个级别,您可能会去 user -> followers -> pictures
.
有时您希望能够仅重新获取部分查询,也许您正在通过连接分页,或者您有 运行 变更。 Relay 的 Node 接口所做的是为您提供一种标准方法来获取 any 类型,该类型通过全局唯一 ID 实现它。 Relay 能够在其查询中识别此类节点,并在可能的情况下使用它们来提高重新获取和分页的效率。我们在根Query类型中添加节点类型:
type Query {
picture(id: Int!): Picture
user(id: Int!): User
...etc
node(id: ID!): Node
}
现在 nodeDefinitions
。这个函数本质上让我们定义了两件事:
- 如何 return 给定对象的 globalId。
- 如何 return 给定对象的类型。
第一个用于获取节点字段的 ID 参数并使用它来解析对象。第二个允许您的 GraphQL 服务器计算出哪种类型的对象被 returned - 这是必要的,以便我们能够在查询节点时定义特定类型的片段,以便我们可以实际获取我们的数据想。否则,我们将无法成功执行如下查询:
query Test {
node(id: 'something') {
...fragment on Picture {
url
}
...fragment on User {
username
}
}
}