Skip to content

Do not ignore a DataFetcher with a DataFetcherFactories wrapper #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kitkars opened this issue Jul 15, 2022 · 1 comment
Closed

Do not ignore a DataFetcher with a DataFetcherFactories wrapper #440

kitkars opened this issue Jul 15, 2022 · 1 comment
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@kitkars
Copy link

kitkars commented Jul 15, 2022

schem.graphqls

directive @UpperCase on FIELD_DEFINITION

type Query{
    hello: String @UpperCase
}

QueryMapping

@QueryMapping
public Mono<String> hello(){
       return Mono.just("hello");
}

SchemaDirectiveWriting

    @Override
    public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) {
        DataFetcher<?> dataFetcher = DataFetcherFactories.wrapDataFetcher(environment.getFieldDataFetcher(), ((dataFetchingEnvironment, value) -> {
            return Mono.just("HELLO");
        }));
        return environment.setFieldDataFetcher(dataFetcher);
    }

I was expecting "HELLO" and but it returns this,

{
  "data": {
    "hello": "MonoJust"
  }
}

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 15, 2022
@rstoyanchev
Copy link
Contributor

DataFetherFactories supports a function to apply to the value of a CompletionStage. Spring for GraphQL applies similar decoration to support Reactor types. However, it ignores any DataFetchers under the graphql package, and that includes DataFetcherFactories.

I can add an exclusion for DataFetcherFactories but in general, if you want to support a directive for Reactor return types you will need to the wrapping yourself anyway. Something like this:

@Override
public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> environment) {
	if (environment.getDirective("UpperCase") == null) {
		return environment.getElement();
	}
	DataFetcher<?> fieldDataFetcher = environment.getFieldDataFetcher();
	return environment.setFieldDataFetcher(dataFetchingEnvironment -> {
		Object value = fieldDataFetcher.get(dataFetchingEnvironment);
		if (value instanceof Mono) {
			return ((Mono<?>) value).map(s -> (s instanceof String ? ((String) s).toUpperCase() : s));
		}
		else if (value instanceof String) {
			return ((String) value).toUpperCase();
		}
		else {
			return value;
		}
	});
}

@rstoyanchev rstoyanchev self-assigned this Aug 4, 2022
@rstoyanchev rstoyanchev added this to the 1.0.2 milestone Aug 4, 2022
@rstoyanchev rstoyanchev added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 4, 2022
@rstoyanchev rstoyanchev changed the title SchemaDirectiveWiring does not support reactive types Do not ignore a DataFetcher with a DataFetcherFactories wrapper Aug 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants