Skip to content

GraphQLCodeRegistry.Builder is null in custom SchemaDirectiveWirings #268

Closed
@stieglma

Description

@stieglma

Hi,

I recently tried to upgrade to the newest version of this library, (graphql-spring-boot-starter is now 5.8.1 and graphql-java-tools is now 5.6.0 both of them seem to be graphql-v12 so I think they should be compatible) and didn't find a solution for the new style of creating directives.

Consider this bean in my spring-boot configuration class:

    @Bean
    public SchemaDirective isAuthorizedDirective() {
        return new SchemaDirective("isAuthorized", new IsAuthorizedDirectiveResolver());
    }

and the respective class:

public class IsAuthorizedDirectiveResolver implements SchemaDirectiveWiring {

    @Override
    public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition> env) {
        GraphQLFieldDefinition field = env.getElement();
        GraphQLFieldsContainer parent = env.getFieldsContainer();

        DataFetcher<?> originalDataFetcher = env.getCodeRegistry().getDataFetcher(parent, field);
        DataFetcher<?> authDataFetcher = dataFetchingEnvironment -> {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth instanceof JWTAuthentication) {
                return originalDataFetcher.get(dataFetchingEnvironment);
            }

            throw new UnauthorizedError();
        };

        // adding the authorized fetcher to the code registry builder
        env.getCodeRegistry().dataFetcher(parent, field, authDataFetcher);

        return field;
    }
}

On application startup I now run into nullpointer exceptions in the line DataFetcher<?> originalDataFetcher = env.getCodeRegistry().getDataFetcher(parent, field); because getCodeRegistry returns null.

I debugged that now quite a while and the problem seems to be that in DirectiveBehavior.toParameters the conversion to SchemaGeneratorDirectiveHelper.Parameters only stores the runtime wiring, which is ignored while creating the SchemaDirectiveWiringEnvironmentImpl.

A solution could be to unpack the GraphQLCodeRegistry.Builder from the runtime-wiring, either at the first conversion from directive behavior parameters to schema generator parameters, or afterwards.

Maybe I do also only have to change how the directive is added?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions