首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >:向边缘添加数据

:向边缘添加数据
EN

Stack Overflow用户
提问于 2016-06-23 05:33:01
回答 2查看 989关注 0票数 3

首先,我将介绍我的应用程序:简单的投票应用程序,用户可以在那里创建投票并在投票中投票。很简单。

当前,我的graphql模式由用户类型、投票类型和投票类型组成,其中用户和投票与其投票有一对多的关系,使用中继连接。投票类型包含,连同它的投票者和投票,它的时间戳和实际的投票值。

现在,据我了解,在regulations列表上使用连接的优点之一是能够在边缘存储数据(除了分页和更多的.)。我怎么能这么做?

如果可能的话,我的计划是去掉投票类型,通过连接将用户和他的投票结果直接连接起来,并将选票值和它的时间戳存储在连接边缘。

如果它是重要的,选民和他的投票之间的联系应该是双向的,即每个用户都连接到他的投票,而每一个投票是连接到它的选民。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-27 16:23:19

但是,为了解决“如何在边缘上定义附加字段”这一更为普遍的问题,最好使用一个单独的Vote实体(如迈克尔·帕里斯的回答中所描述的)来建模,请注意,您可以将edgeFields传递到connectionDefinitions函数在graphql-继电器模块中

有一个例子在测试套件中

代码语言:javascript
复制
var {connectionType: friendConnection} = connectionDefinitions({
  name: 'Friend',
  nodeType: userType,
  resolveNode: edge => allUsers[edge.node],
  edgeFields: () => ({
    friendshipTime: {
      type: GraphQLString,
      resolve: () => 'Yesterday'
    }
  }),
  connectionFields: () => ({
    totalCount: {
      type: GraphQLInt,
      resolve: () => allUsers.length - 1
    }
  }),
});
票数 3
EN

Stack Overflow用户

发布于 2016-06-24 00:12:51

听起来你真的很接近你想要的东西了。我认为使用投票类型作为用户和民意测验之间的中间人是一个很好的解决方案。这样做将允许您发出如下所示的查询:

代码语言:javascript
复制
// Direction 1: User -> Vote -> Poll
query GetUser($id: "abc") {
  getUser(id: $id) {
    username
    votes(first: 10) {
      edges {
        node {
          value
          poll {
            name
          }
        }
        cursor
      }
    }
  }
}

// Direction 2: Poll -> Vote -> User
query GetPoll($id: "xyz") {
  getPoll(id: $id) {
    name
    votes(first: 10) {
      edges {
        node {
          value
          user {
            username
          }
        }
        cursor
      }
    }
  }
}

在本例中,您的投票类型是沿边缘存储信息的实体。与列表相比,连接的一个优点是可以沿边缘存储信息,这是正确的,但我要说的是,更大的好处是能够通过大量的对象进行分页。

要在服务器上实现这一点,您必须为用户和Poll上的连接字段(即上面示例中的“选票”字段)编写自定义的解决方法。取决于您存储数据的方式,这将发生变化,但这里有一些伪代码的想法。

代码语言:javascript
复制
type Vote {
  value: String,
  poll: Poll, // Both poll & user would have resolve functions to grab their respective object.
  user: User
}

type VoteEdge {
  node: Vote,
  cursor: String // an opaque cursor used in the 'before' & 'after' pagination args
}

type PageInfo {
  hasNextPage: Boolean,
  hasPreviousPage: Boolean
}

type VotesConnectionPayload {
  edges: [VoteEdge],
  pageInfo: PageInfo
}

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: () => ({
    id: {
      type: new GraphQLNonNull(GraphQLID),
      description: "A unique identifier."
    },
    username: {
      type: new GraphQLNonNull(GraphQLString),
      description: "A username",
    },
    votes: {
      type: VotesConnectionPayload,
      description: "A paginated set of the user's votes",
      args: { // pagination args
        first: {
          type: GraphQLInt
        },
        after: {
          type: GraphQLString
        },
        last: {
          type: GraphQLInt
        },
        before: {
          type: GraphQLString
        }
      }
      resolve: (parent, paginationArgs, ctxt) => {

        // You can pass a reference to your data source in the ctxt.
        const db = ctxt.db;

        // Go get the full set of votes for my user. Preferably this returns a cursor
        // to the set so you don't pull everything over the network
        return db.getVotesForUser(parent.id).then(votes => {

          // Assume we have a pagination function that applies the pagination args
          // See https://facebook.github.io/relay/graphql/connections.htm for more details
          return paginate(votes, paginationArgs);

        }).then((paginatedVotes, pageInfo) => {

          // Format the votes as a connection payload.
          const edges = paginatedVotes.map(vote => {

            // There are many ways to handle cursors but lets assume
            // we have a magic function that gets one.
            return {
              cursor: getCursor(vote),
              node: vote
            }

          });

          return {
            edges: edges,
            pageInfo: pageInfo
          }
        })
      }
    }
  })
});

对于相反的方向,您必须在Poll类型中做一些类似的事情。要将对象添加到连接中,只需创建一个指向正确用户和Post的投票对象。db.getVotesForUser()方法应该足够聪明,能够意识到这是一对多的连接,然后可以提取正确的对象。

创建处理连接的标准方法可能是一项艰巨的任务,但幸运的是,有一些服务可以帮助您开始使用GraphQL,而不必自己实现所有后端逻辑。我为一个这样的服务https://scaphold.io工作,如果你有兴趣的话,我很乐意和你进一步讨论这个解决方案!

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37983078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档