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

io.datarouter.graphql.service.GraphQlInstancesContainer Maven / Gradle / Ivy

There is a newer version: 0.0.125
Show newest version
/*
 * Copyright © 2009 HotPads ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.datarouter.graphql.service;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import graphql.GraphQL;
import io.datarouter.graphql.client.util.type.GraphQlRootType;
import io.datarouter.graphql.loader.GraphQlDataLoaderService;
import io.datarouter.graphql.playground.GraphQlPlaygroundSampleService;
import io.datarouter.graphql.web.GraphQlBaseHandler;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class GraphQlInstancesContainer{

	private static final Map,GraphQL> classToGraphQl = new HashMap<>();
	private static final Map,String> graphQlClassToPath = new HashMap<>();
	private static final Map,Map>>
			graphQlClassToSampleQueries = new HashMap<>();
	private static final Map,GraphQlDataLoaderConfig>
			graphQlClassToDataloaderConfig = new HashMap<>();


	@Inject
	private GraphQlSchemaService graphQlService;
	@Inject
	private GraphQlPlaygroundSampleService sampleService;
	@Inject
	private GraphQlRouteSetRegistry registry;
	@Inject
	private GraphQlDataLoaderService dataLoaderService;

	public void initializeGraphQlInstances(){
		registry.getGraphQlHandlerToPath().entrySet().stream()
				.filter(entry -> GraphQlBaseHandler.class.isAssignableFrom(entry.getKey()))
				.peek(entry -> graphQlClassToPath.put(entry.getKey(), entry
						.getValue()))
				.map(Entry::getKey)
				.peek(handlerClass -> classToGraphQl.put(handlerClass, graphQlService.build(handlerClass)))
				.peek(handlerClass -> graphQlClassToDataloaderConfig.put(handlerClass, dataLoaderService
						.buildDataloaderConfig(handlerClass)))
				.forEach(handlerClass -> graphQlClassToSampleQueries.put(handlerClass, sampleService
						.buildSchemaQuerySamples(handlerClass)));
	}

	public Set> getGraphQlHandlers(){
		return classToGraphQl.keySet();
	}

	public GraphQL getForClass(Class graphQlHandler){
		return classToGraphQl.get(graphQlHandler);
	}

	public String getPath(Class graphQlHandler){
		return graphQlClassToPath.get(graphQlHandler);
	}

	public Map> getSchemaSampleQueries(
			Class graphQlHandler){
		return graphQlClassToSampleQueries.get(graphQlHandler);
	}

	public GraphQlDataLoaderConfig getDataloaderConfig(Class graphQlHandler){
		return graphQlClassToDataloaderConfig.get(graphQlHandler);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy