简体   繁体   English

如何在api服务器中包含字段并在将结果返回给Graphql中的客户端之前将其删除

[英]How to include fields in api server and remove it before returning to results to client in Graphql

I have a Node.js GraphQL server.我有一个 Node.js GraphQL 服务器。 From the client, I am trying get all the user entries using a query like this:从客户端,我正在尝试使用这样的查询获取所有用户条目:

{
    user {
        name
        entries {
            title
            body
        }
    }
}

In the Node.js GraphQL server, however I want to return user entries that are currently valid based on publishDate and expiryDate in the entries object.但是,在 Node.js GraphQL 服务器中,我想根据条目对象中的publishDateexpiryDate返回当前有效的用户条目。

For example:例如:

{
    "user": "john",
    "entries": [
        { 
          "title": "entry1",
          "body": "body1",
          "publishDate": "2019-02-12",
          "expiryDate": "2019-02-13"
        },
        { 
          "title": "entry2",
          "body": "body2",
          "publishDate": "2019-02-13",
          "expiryDate": "2019-03-01"
        },
        { 
          "title": "entry3",
          "body": "body3",
          "publishDate": "2020-01-01",
          "expiryDate": "2020-01-31"
        }
    ]
}

should return this应该返回这个

{
    "user": "john",
    "entries": [
        { 
          "title": "entry2",
          "body": "body2",
          "publishDate": "2019-02-13",
          "expiryDate": "2019-03-01"
        }
    ]
}

The entries is fetched via a delegateToSchema call ( https://www.apollographql.com/docs/graphql-tools/schema-delegation.html#delegateToSchema ) and I don't have an option to pass publishDate and expiryDate as query parameters.这些entries是通过delegateToSchema调用( https://www.apollographql.com/docs/graphql-tools/schema-delegation.html#delegateToSchema )获取的,我没有选择将 publishDate 和 expiryDate 作为查询参数传递。 Essentially, I need to get the results and then filter them in memory.本质上,我需要得到结果,然后在内存中过滤它们。

The issue I face is that the original query doesn't have publishDate and expiryDate in it to support this.我面临的问题是原始查询中没有publishDateexpiryDate来支持这一点。 Is there a way to add these fields to delegateToSchema call and then remove them while sending them back to the client?有没有办法将这些字段添加到 delegateToSchema 调用,然后在将它们发送回客户端时删除它们?

You are looking for transformResult您正在寻找transformResult

Implementation details are:实现细节是:

  1. At delegateToSchema you need to define transforms array.delegateToSchema 中,您需要定义transforms数组。
  2. At Transform you need to define transformResult function for filtering results.Transform 中,您需要定义transformResult函数来过滤结果。
  3. If you have ability to send arguments to remote GraphQL server, then you should use transformRequest如果您有能力将参数发送到远程 GraphQL 服务器,那么您应该使用transformRequest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在解析字段之前为GraphQL项目充气? - How to inflate GraphQL items before resolving fields? Lambda + DynamoDB + API 网关 - 在返回之前循环并执行结果 - Lambda + DynamoDB + API gateway - Loop and execute results before returning Apollo GraphQL 服务器中的别名字段 - Aliasing fields in Apollo GraphQL Server GraphQL查询在Node-georedis API回调完成之前返回数据(异步问题?) - GraphQL query returning data before node-georedis API callback completes (async issue?) 如何在 Heroku 上部署 GraphQL API 及其 React 客户端? - How can I deploy a GraphQL API and its React Client on Heroku? 在使用Mongoose和MongoDB的API端点中,如何在返回响应给客户端之前等待所有数据库查询完成 - In an API endpoint using Mongoose and MongoDB, how do I wait for all database queries to finish before returning a response to the client 发送到客户端之前如何更改API响应? - How to change the API response before send to the client ? 如何使服务器中的快速路由等到它收到来自另一个nodejs进程的套接字io消息,然后再向客户端返回响应? - how to make express route in server wait until it receives a socket io message from another nodejs process before returning response to client? 如何从Node.js / Express服务器调用GraphQL API? - How to call GraphQL API from Node.js/Express server? 如何在GraphQL中获取对等字段? - How to obtain peer fields in GraphQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM