首页 > 解决方案 > 访问自定义指令参数

问题描述

我在访问指令的参数时遇到问题,我尝试实现谁应该接受客户端给出的参数并根据指令中的参数过滤一些数据。但是,即使在我复制了文档中的代码之后,我也无法访问指令的参数。我有一个奇怪的想法是服务器需要添加FIELD位置,而我在文档中找不到任何提及该位置的内容,所以也许我有一个缺失的方法我应该实施以使其工作,但我在 Apollo 文档中找不到它.

我在2.6.4forapollo-server和forapollo-server-koa的版本上。14.3.1graphql-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)
      )
    }
  }
}

标签: apolloapollo-server

解决方案


推荐阅读