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
.
How To Get The Schema — Introspection Queries. Some GraphQL servers don't provide a convenient GraphQL API explorer. Instead, to get the schema we need to send a HTTP request to the GraphQL server endpoint asking for the GraphQL schema. This type of HTTP request is called a GraphQL introspection query.
Configure GraphQL types, fields, and argumentsA directive decorates part of a GraphQL schema or operation with additional configuration. Tools like Apollo Server (and Apollo Client) can read a GraphQL document's directives and perform custom logic as appropriate. oldField: String @deprecated(reason: "Use `newField`.")
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With