GraphQL中继节点接口

时间:2018-08-03 20:19:38

标签: graphql apollo graphql-js relay

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

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

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

其他模式使用自己的自定义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!."
     }
   ]
  }

1 个答案:

答案 0 :(得分:0)

Relay server spec const oneToTen = Array.from(Array(2).keys()); const obj = oneToTen.reduce((acc, cur) => { acc[cur] = { x: Math.floor(Math.random() * 10), y: Math.floor(Math.random() * 10) } return acc; }, {}); console.log(obj);的类型必须完全

Node

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

话虽如此,the ID type可以表示任何字符串或数字值,所以很容易将第二个片段更改为使用纯interface Node { id: ID! } 。在ID接口的定义和Node字段本身中都是如此。