【问题标题】:relayjs and graphql error: Error: "Node" expects field "id"relayjs 和 graphql 错误:错误:“节点”需要字段“id”
【发布时间】:2017-05-01 16:57:06
【问题描述】:

我认为问题在于我的 schema.js 文件中的字段被称为“id”以外的其他名称,因为该字段在数据库中被称为其他名称。

var classroomType = new GraphQLObjectType({
  name: 'Classroom',
  description: 'A classroom in a school',
  fields: () => ({
    clid: globalIdField('Classroom'),
    course: {
      type: GraphQLString,
      description: 'The course assigned to this classroom.'
    },
    enrollments: {
      type: enrollmentConnection,
      description: 'The students in a classroom',
      args: connectionArgs,
      resolve: (classroom, args) => connectionFromArray(classroom.enrollments.map(getEnrollment), args)
    }
  }),
  interfaces: [nodeInterface],
});

是否必须将 gloablIdField 称为 id?那会显得很奇怪。如果是这样,我如何将其映射到数据库中的 id 字段而不是 id

【问题讨论】:

    标签: javascript reactjs ecmascript-6 graphql relayjs


    【解决方案1】:

    如果您需要将全局 id 字段设置为“clid”,您可以在 Node 接口中将 id 的名称更改为 clid,或者您可以保留它的 id 并将别名应用于解析器方法中命中的字段你的数据库。

    选项 1 如下所示:

    interface MyNode {
      clid: ID!
    }
    
    type Classroom implements MyNode {
        clid: ID!
        ...
    }
    

    选项 2

    graphql-relay 公开的 'globalIdField' 函数接受 idFetcher 作为第二个参数。您可以将其更改为:

    id: globalIdField('Classroom', (obj, context, info) => obj.clid)
    

    然后,您还需要为 id 字段加上别名,以在您的突变解析器中添加。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2019-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多