Reputation: 572
If yes, which one of the following methods should be overwritten and how? I wasn't able to find an example on how to override each of them.
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)
My intuition would say that visitObject(object: GraphQLObjectType)
since type Query
is a GraphQLObjectType
.
Upvotes: 7
Views: 1983
Reputation: 1057
To visit objects (you are right Query is) use visitObject
and for specific api end (any method in Query) use visitFieldDefinition
I have implemented it in following way,
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;
In graphQL shema
directive @authorization(requires: Role) on OBJECT | FIELD_DEFINITION
In schema-builder or server include
resolvers,
schemaDirectives: {
authorization: authDirective
}
Upvotes: 2