React Relay:将数据添加到边缘

Ger*_*api 3 reactjs graphql graphql-js relayjs

我将首先介绍我的应用程序:简单的投票应用程序,用户可以在其中创建投票并对其进行投票。简单。
当前,我的graphql模式由用户类型,投票类型和投票类型组成,其中用户和投票通过中继连接与其投票具有一对多关系。投票类型包含对其投票者和民意调查的引用,时间戳和实际投票值。

现在,据我所知,在规范graphql列表上使用连接的优点之一是能够在边缘上存储数据(除了分页等)。我该怎么办?

如果确实可行,我的计划是摆脱投票类型,直接通过连接连接用户及其投票的民意调查,并在连接边缘存储投票值及其时间戳。

如果重要的话,选民与其民意测验之间的联系应该是双向的,即每个用户都与他的投票民意测验相连,而每个民意测验都与其选民相连。

小智 5

听起来您真的已经快要拥有想要的东西了。我认为使用Vote类型作为用户和投票之间的中间人是一个很好的解决方案。这样,您就可以发出看起来像这样的查询:

// 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
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,您的投票类型是沿边缘存储信息的实体。没错,“连接而不是列表”的一个优点是可以沿边缘存储信息,但是我想说,更大的好处是可以通过大量对象进行分页。

要在服务器上实现此功能,您将必须为“用户”和“民意调查”上的“连接”字段编写自定义解析方法(即,上面示例中的“投票”字段)。根据您存储数据的方式,这将发生变化,但这是一些构想的伪代码。

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
          }
        })
      }
    }
  })
});
Run Code Online (Sandbox Code Playgroud)

对于相反的方向,您将不得不在“轮询”类型中执行类似的操作。要将对象添加到连接中,您需要做的就是创建一个指向正确的用户和帖子的投票对象。db.getVotesForUser()方法应该足够聪明,以实现这是一对多连接,然后可以拉出正确的对象。

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