org.babyfish.jimmer.spring.cfg.JimmerSpringGraphQLAutoConfiguration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-spring-boot-starter Show documentation
Show all versions of jimmer-spring-boot-starter Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.spring.cfg;
import graphql.GraphQL;
import graphql.language.*;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.TypeRuntimeWiring;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.meta.ImmutableType;
import org.babyfish.jimmer.meta.PropId;
import org.babyfish.jimmer.meta.TargetLevel;
import org.babyfish.jimmer.meta.impl.TypedPropImpl;
import org.babyfish.jimmer.runtime.ImmutableSpi;
import org.babyfish.jimmer.sql.JSqlClient;
import org.babyfish.jimmer.sql.kt.KSqlClient;
import org.babyfish.jimmer.sql.runtime.JSqlClientImplementor;
import org.dataloader.DataLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.graphql.execution.BatchLoaderRegistry;
import org.springframework.graphql.execution.GraphQlSource;
import org.springframework.graphql.execution.RuntimeWiringConfigurer;
import reactor.core.publisher.Mono;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@ConditionalOnClass({GraphQL.class, GraphQlSource.class})
public class JimmerSpringGraphQLAutoConfiguration {
@SuppressWarnings("unchecked")
public JimmerSpringGraphQLAutoConfiguration(
BatchLoaderRegistry registry,
@Autowired(required = false) JSqlClient jSqlClient,
@Autowired(required = false) KSqlClient kSqlClient
) {
JSqlClientImplementor sqlClient = sqlClient(jSqlClient, kSqlClient);
for (ImmutableType type : sqlClient.getEntityManager().getAllTypes(sqlClient.getMicroServiceName())) {
if (type.isEntity()) {
for (ImmutableProp prop : type.getProps().values()) {
if (prop.isReference(TargetLevel.ENTITY)) {
registry.forName(prop.toString()).registerMappedBatchLoader((sources, env) -> {
return Mono.just(
sqlClient
.getLoaders()
.reference(
new TypedPropImpl.Reference<>(prop)
)
.batchLoad(sources)
);
});
} else if (prop.isReferenceList(TargetLevel.ENTITY)) {
registry.forName(prop.toString()).registerMappedBatchLoader((sources, env) -> {
return Mono.just(
(Map