apollo - 访问自定义指令参数
问题描述
我在访问指令的参数时遇到问题,我尝试实现谁应该接受客户端给出的参数并根据指令中的参数过滤一些数据。但是,即使在我复制了文档中的代码之后,我也无法访问指令的参数。我有一个奇怪的想法是服务器需要添加FIELD
位置,而我在文档中找不到任何提及该位置的内容,所以也许我有一个缺失的方法我应该实施以使其工作,但我在 Apollo 文档中找不到它.
我在2.6.4
forapollo-server
和forapollo-server-koa
的版本上。14.3.1
graphql-js
我举了一个我所拥有的例子。
谢谢你的帮助
const { SchemaDirectiveVisitor, gql } = require("apollo-server")
const { defaultFieldResolver, GraphQLString, GraphQLList } = require("graphql")
const schema = gql`
directive @myDirective(
fields: [String]
) on FIELD_DEFINITION | FIELD
type mainType {
anotherType: [AnotherType]
}
type AnotherType {
nestedType: [Object] @myDirective
}
`
class MyDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { resolve = defaultFieldResolver } = field
field.args.push({
name: "fields",
type: GraphQLList(GraphQLString)
})
field.resolve = async (source, { fields, otherArgs }, context, info) => {
const results = resolve.call(this, source, otherArgs, context, info)
return results.filter(result =>
fields.every(field => result.value !== field)
)
}
}
}
解决方案
推荐阅读
- django - Django 加密 URL(PK 和 ID)
- vba - ADODB 连接到 Access
- java - 为什么 Windows 控制台代码页会影响带有 ANSI 字符的 Java 程序的编译和运行?
- sql - 如果没有找到数据,Postgresql 将返回具有默认值的行
- python - 在 __init__.py 中循环 __all__
- r - R:二元运算符的非数字参数
- javascript - ReactNative FlatList scrollToIndex 问题
- python - 为什么在 tensorflow 中不允许迭代 tf.Tensor?
- javascript - 如何在不丢失任何数据的情况下重新加载当前页面并显示总平均百分比
- javascript - 如何使用jQuery oninput函数将多个输入字段的值显示到单个输入字段