如何使用阿波罗服务器默认的解析器使用函数?

问题描述 投票:0回答:1

在Graphql工具文档的默认解析器部分,它规定

  1. 返回从obj获得的属性与相关的字段名称,或
  2. 呼吁有关字段名的obj的函数,将查询参数成函数

https://www.apollographql.com/docs/graphql-tools/resolvers.html#Default-resolver

键入DEFS:

type AggregateMessage {
  count: Int!
}

鉴于这种查询解析器:

Query: {
    async messagesConnection(root: any, args: any, context: any, info: any) {
      const messages: IMessageDocument[] = await messageController.messages();

      const edges: MessageEdge[] = [];
      for (const node of messages) {
        edges.push({
          node: node,
          cursor: node.id
        });
      }
      // return messages;
      return {
        pageInfo: {
            hasNextPage: false,
            hasPreviousPage: false
        },
        edges: edges,
        aggregate: {
          count: () => {
            // Resolve count only
            return 123;
          }
        }
      };
   }
}

所以,如果我手动这样定义解析它的工作原理。

AggregateMessage: {
    count(parent: any, args: any, context: any, info: any) {
      return parent.count();
      // Default resolver normally returns parent.count
      // I want it to return parent.count() by default
    }
}

但是,如果我删除的定义,并依靠默认解决功能不起作用。

我希望它调用函数parent.count()为每点#2的文档中,如果我删除手动解析器和依靠默认的解析器行为呼吁属性名称的功能。

  1. 呼吁有关字段名的obj的函数,将查询参数成函数

但是它给出了一个类型的错误,因为“计数”被定义为int类型,但它实际上是一个函数。我怎样才能正确地做到这一点,因此计数函数被调用和解决时返回的值,而不必定义解析器自己?

Int cannot represent non-integer value: [function count]

graphql apollo-server
1个回答
0
投票

这个问题真可谓是由mergeSchemas从graphql工具造成的。

当传递到解析器mergeSchemas(而不是阿波罗服务器或makeExecutableSchema),各功能默认的解析器功能不能正常工作。

https://github.com/apollographql/graphql-tools/issues/1061

我不知道如果这是预期的功能,但移动解析器到makeExecutableSchema呼叫固定的问题。

© www.soinside.com 2019 - 2024. All rights reserved.