Replies: 1 comment
-
Something like is what you want class AuthorisationDirective implements SchemaDirectiveWiring {
@Override
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) {
String targetAuthRole = (String) environment.getDirective().getArgument("role").getArgumentValue().getValue();
//
// build a data fetcher that first checks authorisation roles before then calling the original data fetcher
//
DataFetcher originalDataFetcher = environment.getFieldDataFetcher();
DataFetcher authDataFetcher = new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment dataFetchingEnvironment) throws Exception {
Map<String, Object> contextMap = dataFetchingEnvironment.getContext();
AuthorisationCtx authContext = (AuthorisationCtx) contextMap.get("authContext");
if (authContext.hasRole(targetAuthRole)) {
return originalDataFetcher.get(dataFetchingEnvironment);
} else {
return null;
}
}
};
//
// now change the field definition to have the new authorising data fetcher
return environment.setFieldDataFetcher(authDataFetcher);
} See https://www.graphql-java.com/documentation/sdl-directives |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We have an authorization directive where we are using SchemaDirectiveWiring to authorize the User at runtime. Also, the fields to which the directive is applied and authorization is successful should be visible in the response, else all other fields which were queried by the user but if the directive is not applied to them should not be returned in the response.
Basically, I need to restrict the fields to be returned to which no directive is applied to them.
Example-
GraphQL Schema
GraphQL Request Body
Expected Response
Since, authorization is going to happen for verifyOfflineAccess that should return the result but verifyTicket should be null and throw UnAuthroized Errors
Beta Was this translation helpful? Give feedback.
All reactions