首页 > 解决方案 > GraphQL 中继节点接口

问题描述

我正在尝试合并 2 个实现自己的 nodeDefinitions 的模式。一种模式使用 globalId,如下所示:

const UserType = new GraphQLObjectType({
  name: 'User',
  interfaces: () => [nodeInterface],

  fields: () => ({
    id: globalIdField('User'),
...

而另一个schema实现了这种节点接口,有自己的自定义id

const Node = new GraphQLInterfaceType({
    name: 'Node',
    description: 'Generic Node to fetch single parts of the graph.', 
    fields: {
        id: { type: new GraphQLNonNull(entries.types.ariseId) }
    },

错误

{
 "errors": [
{
  "message": "Interface field Node.id expects type ID! but Property.id 
   is type ariseId!.\n\nInterface field Node.id expects type ID! but 
   SpaceType.id is type ariseId!.\n\nInterface field Node.id expects 
   type ID! but Availability.id is type ariseId!.\n\nInterface field 
   Node.id expects type ID! but RatePlan.id is type 
   ariseId!.\n\nInterface field Node.id expects type ID! but 
   Traveler.id is type ariseId!.\n\nInterface field Node.id expects 
   type ID! but Partner.id is type ariseId!.\n\nInterface field 
   Node.id expects type ID! but Reservation.id is type ariseId!."
     }
   ]
  }

标签: graphqlapollographql-jsrelay

解决方案


中继服务器规范说的类型必须Node

interface Node {
  id: ID!
}

所以可能你的第二个片段是错误的(或者至少不符合规范)。

话虽如此,ID 类型可以表示任何字符串或数值,因此将第二个片段更改为使用 plain 可能很简单IDNode在接口的定义和id字段本身中都是如此。


推荐阅读