[英]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 服务器中,我想根据条目对象中的
publishDate
和expiryDate
返回当前有效的用户条目。
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.我面临的问题是原始查询中没有
publishDate
和expiryDate
来支持这一点。 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:实现细节是:
transforms
array.transforms
数组。transformResult
function for filtering results.transformResult
函数来过滤结果。transformRequest
transformRequest
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.