如果是,应重写以下哪种方法,以及如何重写?我找不到如何覆盖每个示例的示例。
visitSchema(schema: GraphQLSchema)
visitScalar(scalar: GraphQLScalarType)
visitObject(object: GraphQLObjectType)
visitFieldDefinition(field: GraphQLField<any, any>)
visitArgumentDefinition(argument: GraphQLArgument)
visitInterface(iface: GraphQLInterfaceType)
visitUnion(union: GraphQLUnionType)
visitEnum(type: GraphQLEnumType)
visitEnumValue(value: GraphQLEnumValue)
visitInputObject(object: GraphQLInputObjectType)
visitInputFieldDefinition(field: GraphQLInputField)
根据我的直觉,visitObject(object: GraphQLObjectType)
是type Query
,因为GraphQLObjectType
。
答案 0 :(得分:1)
要访问对象(对,查询是对的),请使用visitObject
,对于特定的api结束(查询中的任何方法),请使用visitFieldDefinition
我已经通过以下方式实现了
class authDirective extends SchemaDirectiveVisitor {
visitObject(type) {
this.ensureFieldsWrapped(type);
type._requiredAuthRole = this.args.requires;
}
visitFieldDefinition(field, details) {
this.ensureFieldsWrapped(details.objectType);
field._requiredAuthRole = this.args.requires;
}
ensureFieldsWrapped(objectType){
const fields = objectType.getFields();
//your logic to resolve directive
}
}
module.exports = authDirective;
在graphQL shema中
directive @authorization(requires: Role) on OBJECT | FIELD_DEFINITION
在schema-builder或服务器中,包括
resolvers,
schemaDirectives: {
authorization: authDirective
}