diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 1e9dc7373ce..d9ab546c817 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -107,6 +107,8 @@ image:doc/screenshots/insideLabelPositions.png[Inside label positions, 70%] - https://github.com/eclipse-sirius/sirius-web/issues/3561[#3561] [diagram] Add support for background and border on diagram labels - https://github.com/eclipse-sirius/sirius-web/issues/3604[#3604] [diagram] Make node overlap resolution faster during "Arrange All" - https://github.com/eclipse-sirius/sirius-web/issues/3634[#3634] [sirius-web] Simplifying the contribution to the GraphQL subscription of the diagram for custom nodes +- https://github.com/eclipse-sirius/sirius-web/issues/3656[#3656] [core] Add the ability to customize the GraphQL type resolver + == v2024.5.0 diff --git a/packages/core/backend/sirius-components-graphql-api/src/main/java/org/eclipse/sirius/components/graphql/api/ITypeResolverDelegate.java b/packages/core/backend/sirius-components-graphql-api/src/main/java/org/eclipse/sirius/components/graphql/api/ITypeResolverDelegate.java new file mode 100644 index 00000000000..ef9fea57dad --- /dev/null +++ b/packages/core/backend/sirius-components-graphql-api/src/main/java/org/eclipse/sirius/components/graphql/api/ITypeResolverDelegate.java @@ -0,0 +1,25 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.components.graphql.api; + +import graphql.TypeResolutionEnvironment; +import graphql.schema.GraphQLObjectType; + +/** + * Used to find GraphQL object type not found by the reflective type resolver. + * + * @author sbegaudeau + */ +public interface ITypeResolverDelegate { + GraphQLObjectType getType(TypeResolutionEnvironment environment); +} diff --git a/packages/core/backend/sirius-components-graphql-api/src/main/java/org/eclipse/sirius/components/graphql/api/ReflectiveTypeResolver.java b/packages/core/backend/sirius-components-graphql-api/src/main/java/org/eclipse/sirius/components/graphql/api/ReflectiveTypeResolver.java index cb0c6857387..b3bc707af36 100644 --- a/packages/core/backend/sirius-components-graphql-api/src/main/java/org/eclipse/sirius/components/graphql/api/ReflectiveTypeResolver.java +++ b/packages/core/backend/sirius-components-graphql-api/src/main/java/org/eclipse/sirius/components/graphql/api/ReflectiveTypeResolver.java @@ -12,6 +12,9 @@ *******************************************************************************/ package org.eclipse.sirius.components.graphql.api; +import java.util.List; +import java.util.Objects; + import graphql.TypeResolutionEnvironment; import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLSchema; @@ -24,11 +27,27 @@ */ public class ReflectiveTypeResolver implements TypeResolver { + private final List typeResolverDelegates; + + public ReflectiveTypeResolver(List typeResolverDelegates) { + this.typeResolverDelegates = Objects.requireNonNull(typeResolverDelegates); + } + @Override public GraphQLObjectType getType(TypeResolutionEnvironment environment) { GraphQLSchema graphQLSchema = environment.getSchema(); Object object = environment.getObject(); - return graphQLSchema.getObjectType(object.getClass().getSimpleName()); + var graphQLObjectType = graphQLSchema.getObjectType(object.getClass().getSimpleName()); + + if (graphQLObjectType == null) { + graphQLObjectType = this.typeResolverDelegates.stream() + .map(typeResolverDelegate -> typeResolverDelegate.getType(environment)) + .filter(Objects::nonNull) + .findFirst() + .orElse(null); + } + + return graphQLObjectType; } } diff --git a/packages/sirius-web/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/configuration/GraphQLWiringFactory.java b/packages/sirius-web/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/configuration/GraphQLWiringFactory.java index 099b3082675..966919077f8 100644 --- a/packages/sirius-web/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/configuration/GraphQLWiringFactory.java +++ b/packages/sirius-web/backend/sirius-web-graphql/src/main/java/org/eclipse/sirius/web/graphql/configuration/GraphQLWiringFactory.java @@ -12,6 +12,10 @@ *******************************************************************************/ package org.eclipse.sirius.web.graphql.configuration; +import java.util.List; +import java.util.Objects; + +import org.eclipse.sirius.components.graphql.api.ITypeResolverDelegate; import org.eclipse.sirius.components.graphql.api.ReflectiveTypeResolver; import org.springframework.stereotype.Service; @@ -28,6 +32,12 @@ @Service public class GraphQLWiringFactory implements WiringFactory { + private final List typeResolverDelegates; + + public GraphQLWiringFactory(List typeResolverDelegates) { + this.typeResolverDelegates = Objects.requireNonNull(typeResolverDelegates); + } + @Override public boolean providesTypeResolver(InterfaceWiringEnvironment environment) { return true; @@ -35,7 +45,7 @@ public boolean providesTypeResolver(InterfaceWiringEnvironment environment) { @Override public TypeResolver getTypeResolver(InterfaceWiringEnvironment environment) { - return new ReflectiveTypeResolver(); + return new ReflectiveTypeResolver(this.typeResolverDelegates); } @Override @@ -45,6 +55,6 @@ public boolean providesTypeResolver(UnionWiringEnvironment environment) { @Override public TypeResolver getTypeResolver(UnionWiringEnvironment environment) { - return new ReflectiveTypeResolver(); + return new ReflectiveTypeResolver(this.typeResolverDelegates); } } diff --git a/packages/sirius-web/backend/sirius-web-infrastructure/src/main/java/org/eclipse/sirius/web/infrastructure/graphql/GraphQLWiringFactory.java b/packages/sirius-web/backend/sirius-web-infrastructure/src/main/java/org/eclipse/sirius/web/infrastructure/graphql/GraphQLWiringFactory.java index edad5f16e69..748965b5be2 100644 --- a/packages/sirius-web/backend/sirius-web-infrastructure/src/main/java/org/eclipse/sirius/web/infrastructure/graphql/GraphQLWiringFactory.java +++ b/packages/sirius-web/backend/sirius-web-infrastructure/src/main/java/org/eclipse/sirius/web/infrastructure/graphql/GraphQLWiringFactory.java @@ -12,6 +12,10 @@ *******************************************************************************/ package org.eclipse.sirius.web.infrastructure.graphql; +import java.util.List; +import java.util.Objects; + +import org.eclipse.sirius.components.graphql.api.ITypeResolverDelegate; import org.eclipse.sirius.components.graphql.api.ReflectiveTypeResolver; import org.springframework.stereotype.Service; @@ -28,6 +32,12 @@ @Service public class GraphQLWiringFactory implements WiringFactory { + private final List typeResolverDelegates; + + public GraphQLWiringFactory(List typeResolverDelegates) { + this.typeResolverDelegates = Objects.requireNonNull(typeResolverDelegates); + } + @Override public boolean providesTypeResolver(InterfaceWiringEnvironment environment) { return true; @@ -35,7 +45,7 @@ public boolean providesTypeResolver(InterfaceWiringEnvironment environment) { @Override public TypeResolver getTypeResolver(InterfaceWiringEnvironment environment) { - return new ReflectiveTypeResolver(); + return new ReflectiveTypeResolver(this.typeResolverDelegates); } @Override @@ -45,6 +55,6 @@ public boolean providesTypeResolver(UnionWiringEnvironment environment) { @Override public TypeResolver getTypeResolver(UnionWiringEnvironment environment) { - return new ReflectiveTypeResolver(); + return new ReflectiveTypeResolver(this.typeResolverDelegates); } } \ No newline at end of file