Relay/GraphQL 将自定义字段添加到连接

Posted

技术标签:

【中文标题】Relay/GraphQL 将自定义字段添加到连接【英文标题】:Relay/GraphQL add custom field to connection 【发布时间】:2016-11-13 10:53:37 【问题描述】:

我正在尝试添加要用于分页的记录总数。现在我可以看到我无法修改它以使我的 totalCount 与它分开的连接。

    Books: 
        type: BooksConnection.connectionType,
        args:  ...connectionArgs, isbn:  type: GraphQLString , publisher: type: GraphQLString,
        resolve: ( obj,  ...args , context,  rootValue: objectManager  ) =>
        
            let user = obj;
            let FormatedArgs = MasterFields.FormatPredicate(args);
            return objectManager.getListBy( 'Book', user, FormatedArgs.queryArgs, objectManager.getViewerUserId( ) ).then( ( arr ) =>
            

                let result = ;
                result.Books = arr;
                result.totalCount = arr.length;
;
                //Originally i would just pass arr instead of result.
                return connectionFromArray( result, FormatedArgs.connArgs);
             )
        
    ,

在这种情况下,当我在 BookConnection 中获取连接对象时。我希望能够将该值分配给一个字段。

export default connectionDefinitions( 
    name: 'Books',
    nodeType: BookType,
    connectionFields: () => (
        totalCount: 
            type: GraphQLInt,
            resolve: (connection) =>  console.log(connection); return connection.totalCount; ,
            description: `A count of the total number of objects in this connection, ignoring pagination.
This allows a client to fetch the first five objects by passing "5" as the
argument to "first", then fetch the total count so it could display "5 of 83",
for example.`
        
)
);

如何使totalCount 成为connection 变量的属性?

我在这里找到了部分答案:How to pass total count to the client in pageInfo

【问题讨论】:

【参考方案1】:

这点在看了一个小时左右后就很明显了。我不知道如何从connectionFromArray() 接收totalCount 值这就是我覆盖它的方式。

    Books: 
        type: BooksConnection.connectionType,
        args:  ...connectionArgs, isbn:  type: GraphQLString , publisher: type: GraphQLString,
        resolve: ( obj,  ...args , context,  rootValue: objectManager  ) =>
        
            let user = obj;
            let FormatedArgs = MasterFields.FormatPredicate(args);
            return objectManager.getListBy( 'Book', user, FormatedArgs.queryArgs, objectManager.getViewerUserId( ) ).then( ( arr ) =>
            

                let result  = connectionFromArray( arr, FormatedArgs.connArgs);
;
                result.pageInfo.totalCount = arr.length;


                return result;
             )
        
    ,

现在这是我在抱怨。

function connectionFromArray(data, args) 
  return connectionFromArraySlice(data, args, 
    sliceStart: 0,
    arrayLength: data.length
  );

function connectionFromArraySlice(arraySlice, args, meta) 
  var after = args.after;
  var before = args.before;
  var first = args.first;
  var last = args.last;
  var sliceStart = meta.sliceStart;
  var arrayLength = meta.arrayLength;

  var sliceEnd = sliceStart + arraySlice.length;
  var beforeOffset = getOffsetWithDefault(before, arrayLength);
  var afterOffset = getOffsetWithDefault(after, -1);

  var startOffset = Math.max(sliceStart - 1, afterOffset, -1) + 1;
  var endOffset = Math.min(sliceEnd, beforeOffset, arrayLength);
  if (typeof first === 'number') 
    endOffset = Math.min(endOffset, startOffset + first);
  
  if (typeof last === 'number') 
    startOffset = Math.max(startOffset, endOffset - last);
  

  // If supplied slice is too large, trim it down before mapping over it.
  var slice = arraySlice.slice(Math.max(startOffset - sliceStart, 0), arraySlice.length - (sliceEnd - endOffset));

  var edges = slice.map(function (value, index) 
    return 
      cursor: offsetToCursor(startOffset + index),
      node: value
    ;
  );

上面是具有此功能的graphql-relay lib。当他们显然有它在那里执行分页时,不传递给我们arrayLength的目的是什么?

【讨论】:

以上是关于Relay/GraphQL 将自定义字段添加到连接的主要内容,如果未能解决你的问题,请参考以下文章

如何将自定义字段添加到 WooCommerce 注册表单

Netsuite 将自定义字段添加到交易列字段

使用 paypal 节点 sdk 将自定义字段添加到计费协议

Drupal,Ubercart - 将自定义字段添加到结帐表单

DRF,将自定义字段添加到 ModelSerializer

php 将自定义字段内容添加到菜单项