处理GraphQL中的Mongoose填充字段

tec*_*rek 5 mongoose mongodb node.js graphql graphql-js

如何表示可以是简单ObjectId字符串或填充对象实体的字段?

我有一个表示'设备类型'的Mongoose Schema,如下所示

// assetSchema.js

import * as mongoose from 'mongoose'
const Schema = mongoose.Schema;

var Asset = new Schema({  name : String,
                          linked_device: { type: Schema.Types.ObjectId, 
                                           ref: 'Asset'})

export AssetSchema = mongoose.model('Asset', Asset);
Run Code Online (Sandbox Code Playgroud)

我试图将其建模为GraphQLObjectType,但我很难理解如何允许linked_ue字段采用两种类型的值,一种是一种ObjectId,另一种是完整的Asset对象(当它被填充时)

// graphql-asset-type.js

import { GraphQLObjectType, GraphQLString } from 'graphql'

export var GQAssetType = new GraphQLObjectType({
           name: 'Asset',
           fields: () => ({
               name: GraphQLString,
               linked_device: ____________    // stumped by this
});
Run Code Online (Sandbox Code Playgroud)

我已经研究了联盟类型,但问题是联盟类型期望字段被规定为其定义的一部分,而在上述情况下,linked_devicelinked_device对应于简单时,字段下面没有字段ObjectId.

有任何想法吗?

Ahm*_*ous 7

事实上,您可以为字段使用unioninterface类型linked_device.

使用union类型,您可以实现GQAssetType如下:

// graphql-asset-type.js

import { GraphQLObjectType, GraphQLString, GraphQLUnionType } from 'graphql'

var LinkedDeviceType = new GraphQLUnionType({
  name: 'Linked Device',
  types: [ ObjectIdType, GQAssetType ],
  resolveType(value) {
    if (value instanceof ObjectId) {
      return ObjectIdType;
    }
    if (value instanceof Asset) {
      return GQAssetType;
    }
  }
});

export var GQAssetType = new GraphQLObjectType({
  name: 'Asset',
  fields: () => ({
    name: { type: GraphQLString },
    linked_device: { type: LinkedDeviceType },
  })
});
Run Code Online (Sandbox Code Playgroud)

查看关于GraphQL联合和接口的这篇优秀文章.


sar*_*ora 5

当我遇到这篇文章时,我试图解决拉取关系数据的一般问题。需要明确的是,最初的问题似乎是当字段可能包含 ObjectId 或 Object 时如何动态解析数据,但是我认为首先让字段存储 object 或 objectId 并不是一个好的设计。因此,我对解决将字段分开的简化场景很感兴趣——一个用于 Id,另一个用于对象。我也认为使用 Unions 过于复杂,除非您实际上有另一种场景,如上面引用的文档中描述的场景。我想下面的解决方案也可能会让其他人感兴趣......

注意:我使用的是 graphql-tools,所以我的类型是编写的模式语言语法。因此,如果您的用户类型具有如下字段:

type User {
    _id: ID
    firstName: String
    lastName: String
    companyId: ID
    company: Company
}
Run Code Online (Sandbox Code Playgroud)

然后在我的用户解析器函数代码中,我添加了以下内容:

  User: {   // <-- this refers to the User Type in Graphql
    company(u) {   // <-- this refers to the company field
      return User.findOne({ _id: u.companyId }); // <-- mongoose User type
    },
  }
Run Code Online (Sandbox Code Playgroud)

以上与已经存在的用户解析器函数一起工作,并允许您编写 GQL 查询,如下所示:

query getUserById($_id:ID!) 
    { getUserById(_id:$_id) {
    _id
    firstName
    lastName
    company {
        name
    }
    companyId
    }}
Run Code Online (Sandbox Code Playgroud)

问候,

S.阿罗拉