diff --git a/graphql-dgs-example-shared/src/main/java/com/netflix/graphql/dgs/example/shared/datafetcher/ActionMovieDataFetcher.java b/graphql-dgs-example-shared/src/main/java/com/netflix/graphql/dgs/example/shared/datafetcher/ActionMovieDataFetcher.java new file mode 100644 index 000000000..aefacab0d --- /dev/null +++ b/graphql-dgs-example-shared/src/main/java/com/netflix/graphql/dgs/example/shared/datafetcher/ActionMovieDataFetcher.java @@ -0,0 +1,30 @@ +/* + * Copyright 2021 Netflix, Inc. + * + * 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 com.netflix.graphql.dgs.example.shared.datafetcher; + +import com.netflix.graphql.dgs.DgsComponent; +import com.netflix.graphql.dgs.DgsData; + +@DgsComponent +public class ActionMovieDataFetcher { + @SuppressWarnings("SameReturnValue") + @DgsData(parentType = "ActionMovie", field = "director") + public String director() { + + return "Action movie director"; + } +} diff --git a/graphql-dgs-example-shared/src/main/java/com/netflix/graphql/dgs/example/shared/datafetcher/ScaryMovieDataFetcher.java b/graphql-dgs-example-shared/src/main/java/com/netflix/graphql/dgs/example/shared/datafetcher/ScaryMovieDataFetcher.java new file mode 100644 index 000000000..93be5a978 --- /dev/null +++ b/graphql-dgs-example-shared/src/main/java/com/netflix/graphql/dgs/example/shared/datafetcher/ScaryMovieDataFetcher.java @@ -0,0 +1,37 @@ +/* + * Copyright 2021 Netflix, Inc. + * + * 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 com.netflix.graphql.dgs.example.shared.datafetcher; + +import com.netflix.graphql.dgs.DgsComponent; +import com.netflix.graphql.dgs.DgsData; +import com.netflix.graphql.dgs.example.shared.types.ActionMovie; +import com.netflix.graphql.dgs.example.shared.types.Movie; +import com.netflix.graphql.dgs.example.shared.types.ScaryMovie; +import graphql.schema.DataFetchingEnvironment; + +import java.util.ArrayList; +import java.util.List; + +@DgsComponent +public class ScaryMovieDataFetcher { + @SuppressWarnings("SameReturnValue") + @DgsData(parentType = "ScaryMovie", field = "director") + public String director() { + + return "Scary movie director"; + } +} diff --git a/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DgsSchemaProvider.kt b/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DgsSchemaProvider.kt index 43a58a6c4..4ec20b828 100644 --- a/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DgsSchemaProvider.kt +++ b/graphql-dgs/src/main/kotlin/com/netflix/graphql/dgs/internal/DgsSchemaProvider.kt @@ -368,14 +368,20 @@ class DgsSchemaProvider( } val implementationsOf = typeDefinitionRegistry.getImplementationsOf(type) implementationsOf.forEach { implType -> - val dataFetcher = - createBasicDataFetcher(method, dgsComponent, parentType == "Subscription") - codeRegistryBuilder.dataFetcher( - FieldCoordinates.coordinates(implType.name, field), - dataFetcher - ) - dataFetcherTracingInstrumentationEnabled["${implType.name}.$field"] = enableTracingInstrumentation - dataFetcherMetricsInstrumentationEnabled["${implType.name}.$field"] = enableMetricsInstrumentation + // if we have a datafetcher explicitly defined for a parentType/field, use that and do not + // register the base implementation for interfaces + if (!codeRegistryBuilder.hasDataFetcher(FieldCoordinates.coordinates(implType.name, field))) { + val dataFetcher = + createBasicDataFetcher(method, dgsComponent, parentType == "Subscription") + codeRegistryBuilder.dataFetcher( + FieldCoordinates.coordinates(implType.name, field), + dataFetcher + ) + dataFetcherTracingInstrumentationEnabled["${implType.name}.$field"] = + enableTracingInstrumentation + dataFetcherMetricsInstrumentationEnabled["${implType.name}.$field"] = + enableMetricsInstrumentation + } } } is UnionTypeDefinition -> {