Skip to content

How to make GraphqlFieldVisibility work with WebFlux #284

Open
@huahouye

Description

@huahouye
spring-boot 2.7.0-M1
spring-boot-starter-graphql 2.7.0-M1
@Bean
public RuntimeWiringConfigurer configurer(MyReactiveUserAccessService myReactiveUserAccessService) {
return (builder) -> builder.fieldVisibility(new PerFieldAuthorization(myReactiveUserAccessService));
}
class PerFieldAuthorization implements GraphqlFieldVisibility {

	private final MyReactiveUserAccessService myReactiveUserAccessService;

	public PerFieldAuthorization(MyReactiveUserAccessService myReactiveUserAccessService) {
		this.myReactiveUserAccessService = myReactiveUserAccessService;
	}

	@Override
	public List<GraphQLFieldDefinition> getFieldDefinitions(GraphQLFieldsContainer fieldsContainer) {
		return fieldsContainer.getFieldDefinitions();
	}

	@Override
	public GraphQLFieldDefinition getFieldDefinition(GraphQLFieldsContainer fieldsContainer, String fieldName) {
		myReactiveUserAccessService.getRoles().blockLast(); // Exception
		return fieldsContainer.getFieldDefinition(fieldName);
	}

	@Override
	public List<GraphQLInputObjectField> getFieldDefinitions(GraphQLInputFieldsContainer fieldsContainer) {
		return fieldsContainer.getFieldDefinitions();
	}

	@Override
	public GraphQLInputObjectField getFieldDefinition(GraphQLInputFieldsContainer fieldsContainer, String fieldName) {
		return fieldsContainer.getFieldDefinition(fieldName);
	}

}
@Component
class MyReactiveUserAccessService {
	public Flux<String> getRoles() {
//		Mono.deferContextual((contextView) -> {
//			return Mono.empty();
//		});
		return Flux.just("ROLE_USER", "ROLE_ADMIN");
	}
}

I am trying to use GraphqlFieldVisibility (field-visibility document is here) to perform per-field authorization. Is there a way to make GraphqlFieldVisibility work with WebFlux?

Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues related to config and core supportin: securityIssues related to securitystatus: blockedAn issue that's blocked on an external project change

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions