All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spring.boxes.dollar.support.graphql.directive.FetcherDirective Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.dollar.support.graphql.directive;

import static com.spring.boxes.dollar.support.graphql.GraphqlArguments.getFetcherDirectiveArgument;
import static com.spring.boxes.dollar.support.graphql.GraphqlFetchers.getDataFetcherWithNewEnvironment;
import static com.spring.boxes.dollar.support.graphql.GraphqlFetchers.sourceAviFetcherNewArgument;
import static graphql.schema.DataFetchingEnvironmentImpl.newDataFetchingEnvironment;

import java.util.Map;
import java.util.Objects;

import com.spring.boxes.dollar.support.graphql.model.FetchDirectiveArgument;
import org.springframework.context.ApplicationContext;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetcherFactories;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLFieldsContainer;
import graphql.schema.idl.SchemaDirectiveWiring;
import graphql.schema.idl.SchemaDirectiveWiringEnvironment;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FetcherDirective implements SchemaDirectiveWiring {

    private final ApplicationContext applicationContext;

    public FetcherDirective(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public static FetcherDirective newAutoFetcherDirective(ApplicationContext applicationContext){
        return new FetcherDirective(applicationContext);
    }

    /**
     * 扩展指令
     *
     * 
     *    # 定义指令
     *    directive @autoFetcher(type: Int!, service: String!, supply: String, extract: String) on FIELD_DEFINITION
     *
     *    # 应用指令
     *    type Datum {
     *       id
     *       title
     *       createTime: String @autoFetcher(type:1, service: 'datumDataFetcher#getDatumById', supply={appId:appId, id:datumId}, extract: "")
     *       updateTime: DateTime
     *    }
     * 
     *
     * @param environment 上下文
     * @return 实体属性定义
     */
    @SuppressWarnings("unchecked")
    @Override
    public GraphQLFieldDefinition onField(SchemaDirectiveWiringEnvironment environment) {
        GraphQLFieldDefinition field = environment.getElement();
        GraphQLFieldsContainer parentType = environment.getFieldsContainer();
        GraphQLDirective autoAviatorDirective = field.getDirective(DefaultDirective.fetcher.getName());
        if (Objects.nonNull(autoAviatorDirective)) {
            FetchDirectiveArgument directiveArgument = getFetcherDirectiveArgument(autoAviatorDirective);
            if(Objects.nonNull(directiveArgument)){
                DataFetcher originalFetcher = environment.getCodeRegistry().getDataFetcher(parentType, field);
                DataFetcher dataFetcher =
                        DataFetcherFactories.wrapDataFetcher(originalFetcher, ((dataFetchingEnvironment, value) -> {
                            Map variables = dataFetchingEnvironment.getVariables();
                            Map arguments = sourceAviFetcherNewArgument(dataFetchingEnvironment, directiveArgument.getSupply(), directiveArgument.isAviator());
                            DataFetchingEnvironment newEnvironment = newDataFetchingEnvironment(dataFetchingEnvironment)
                                    .arguments(arguments)
                                    .variables(variables).build();
                            return getDataFetcherWithNewEnvironment(applicationContext, originalFetcher, newEnvironment, directiveArgument);
                        }));

                // now change the field definition to use the new aviatorDirective data fetcher
                environment.getCodeRegistry().dataFetcher(parentType, field, dataFetcher);
            }
        }
        return field;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy