relayjs and graphql error: Error: "Node" expects field "id"

relayjs and graphql error: Error: "Node" expects field "id"

我认为问题在于我的 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 字段?

如果你需要你的全局 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 字段添加别名以在您的突变解析器中进行 clid。