Javascript GraphQL(Apollo):您可以访问resolve函数中的指令吗?

Javascript GraphQL(Apollo):您可以访问resolve函数中的指令吗?,javascript,graphql,apollo,graphql-js,apollo-server,Javascript,Graphql,Apollo,Graphql Js,Apollo Server,我希望每个字段都是私有的,除非另有指示。是否可以在resolve函数中获取此信息 const typeDefs = gql` directive @public on FIELD_DEFINITION type Query { viewer: User @public secret: String } type User { id: ID! } ` const schema = makeExecutableSchema({ typeDefs,

我希望每个字段都是私有的,除非另有指示。是否可以在resolve函数中获取此信息

const typeDefs = gql`
  directive @public on FIELD_DEFINITION

  type Query {
    viewer: User @public
    secret: String
  }

  type User {
    id: ID!
  }
`

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

addSchemaLevelResolveFunction(schema, (parent, args, params, info) => {
  // Not possible
  if (info.fieldName.directive === 'public') {
    return parent;
  }

  throw new Error('Authentication required...');
});

const server = new ApolloServer({ schema });

虽然
fieldNodes
数组中的
fieldNodes
对象上有一个
directives
属性,但据我所知,它没有填充适用于该特定字段的指令

指令并不是真正用来作为可以在解析器(模式级或其他)中引用的东西的标志。您可以考虑在指令的<代码> VisualFieldDeals函数:

中移动逻辑。
const { defaultFieldResolver } = require('graphql')
const { SchemaDirectiveVisitor } = require('graphql-tools')

class PublicDirective extends SchemaDirectiveVisitor {
  visitFieldDefinition(field) {
    const { resolve = defaultFieldResolver } = field
    field.resolve = async function (source, args, context, info) {
      if (someCondition) {
        throw new SomeError()
      }
      return resolve.apply(this, [source, args, context, info])
    }
  }
}

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
  schemaResolvers: {
    public: PublicDirective,
  },
})

是的,这适用于类似于
@private
指令(默认情况下为public)的东西,但希望找到一种相反的方法。有没有一种简单的方法可以在编译时自动将指令添加到所有字段(即@private)?啊,很抱歉,没有完全理解您试图执行的操作。您可以让指令将一些任意属性注入到上下文中,然后在
addSchemaLevelResolveFunction