【问题标题】:Graph db design to GraphQL schemaGraph db 设计到 GraphQL 模式
【发布时间】:2016-07-18 02:02:21
【问题描述】:

我正在尝试从我拥有的图形数据库架构创建一个 graphql 架构。但我看不到如何在 graphql 架构中为我拥有的边添加属性。

在一些代码中:

示例数据库架构:

node: {
  label: 'Person',
  properties: [
   id: { type: id }
   name: { type: string }
  ]
}

edge: {
  label: 'friends'
  startNode: 'Person',
  endNode: 'Person'
  properties: {
    since: { type: date }
  }
}

在 graphql 模式中应该看起来很简单:

var personType = new graphql.GraphQLObjectType({
  name: 'personType',
  fields: function() { return {
    id: { type: graphql.GraphQLString },
    name: { type: graphql.GraphQLString },
    friends: { type: graphql.GraphQLList(personType) }
  }})
});

但我看不到将属性“since”添加到朋友字段的方法。而且我在文档或互联网上什么也没找到。

规范中是否有某些内容,或者我需要根据添加“since”等附加属性并使用它们的节点为所有边创建新类型。 还是其他我想不通的东西?

【问题讨论】:

    标签: javascript graph graphql graphql-js


    【解决方案1】:

    示例中继应用程序的模式,star-wars 项目在这种特殊情况下,非常有帮助。 FactionShip 在您的情况下扮演 PersonFriend 的角色。

    你是对的。为了包含since属性,可以为朋友引入一个新类型如下(使用graphqlnpm包):

    var friendType = new GraphQLObjectType({
      name: 'Friend',
      fields: {
        id: globalIdField('Friend'),
        name: {
          type: GraphQLString,
          resolve: (friend) => friend.name,
        },
        since: {
          type: GraphQLString,
          resolve: (friend) => friend.since.toString(),
        },
      },
      interfaces: [nodeInterface],
    });
    

    friendType 中,since 是实际日期的字符串表示形式。如果您想要自定义 GraphQL 类型的日期,可以查看 graphql-custom-datetype。我还没有使用它。 在您已经定义的personType中,对于friends字段,列表元素类型personType需要替换为新的friendType

    friends: { type: graphql.GraphQLList(friendType) }
    

    如果朋友的数量很大,建议使用连接或边缘,正如 ykad4 已经建议的那样。一旦我们有了 Friend 的定义,我们就可以为它定义连接如下:

    const {
      connectionType: friendConnection,
      edgeType: friendEdge,
    } = connectionDefinitions({
      name: 'Friend',
      nodeType: friendType,
    });
    

    personType 中的字段friends 将更新如下(使用来自graphql-relay npm 包的辅助函数):

    friends: {
      type: friendConnection,
      args: connectionArgs,
      resolve: (person) => connectionFromArray(person.friends, args),
    },
    

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 2020-02-04
      • 2017-01-23
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多