|
57 | 57 | import graphql.schema.GraphQLUnionType; |
58 | 58 | import graphql.schema.GraphqlTypeComparatorRegistry; |
59 | 59 | import graphql.schema.PropertyDataFetcher; |
| 60 | +import graphql.schema.SingletonPropertyDataFetcher; |
60 | 61 | import graphql.schema.TypeResolver; |
61 | 62 | import graphql.schema.TypeResolverProxy; |
62 | 63 | import graphql.schema.idl.errors.NotAnInputTypeError; |
@@ -801,23 +802,27 @@ GraphQLFieldDefinition buildField(BuildContext buildCtx, TypeDefinition<?> paren |
801 | 802 | // if they have already wired in a fetcher - then leave it alone |
802 | 803 | FieldCoordinates coordinates = FieldCoordinates.coordinates(parentType.getName(), fieldDefinition.getName()); |
803 | 804 | if (!buildCtx.getCodeRegistry().hasDataFetcher(coordinates)) { |
804 | | - DataFetcherFactory<?> dataFetcherFactory = buildDataFetcherFactory(buildCtx, |
| 805 | + Optional<DataFetcherFactory<?>> dataFetcherFactory = buildDataFetcherFactory(buildCtx, |
805 | 806 | parentType, |
806 | 807 | fieldDef, |
807 | 808 | fieldType, |
808 | 809 | appliedDirectives.first, |
809 | 810 | appliedDirectives.second); |
810 | | - buildCtx.getCodeRegistry().dataFetcher(coordinates, dataFetcherFactory); |
| 811 | + |
| 812 | + // if the dataFetcherFactory is empty, then it must have been the code registry default one |
| 813 | + // and hence we don't need to make a "map entry" in the code registry since it will be defaulted |
| 814 | + // anyway |
| 815 | + dataFetcherFactory.ifPresent(fetcherFactory -> buildCtx.getCodeRegistry().dataFetcher(coordinates, fetcherFactory)); |
811 | 816 | } |
812 | 817 | return directivesObserve(buildCtx, fieldDefinition); |
813 | 818 | } |
814 | 819 |
|
815 | | - private DataFetcherFactory<?> buildDataFetcherFactory(BuildContext buildCtx, |
816 | | - TypeDefinition<?> parentType, |
817 | | - FieldDefinition fieldDef, |
818 | | - GraphQLOutputType fieldType, |
819 | | - List<GraphQLDirective> directives, |
820 | | - List<GraphQLAppliedDirective> appliedDirectives) { |
| 820 | + private Optional<DataFetcherFactory<?>> buildDataFetcherFactory(BuildContext buildCtx, |
| 821 | + TypeDefinition<?> parentType, |
| 822 | + FieldDefinition fieldDef, |
| 823 | + GraphQLOutputType fieldType, |
| 824 | + List<GraphQLDirective> directives, |
| 825 | + List<GraphQLAppliedDirective> appliedDirectives) { |
821 | 826 | String fieldName = fieldDef.getName(); |
822 | 827 | String parentTypeName = parentType.getName(); |
823 | 828 | TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry(); |
@@ -847,16 +852,18 @@ private DataFetcherFactory<?> buildDataFetcherFactory(BuildContext buildCtx, |
847 | 852 | if (dataFetcher == null) { |
848 | 853 | DataFetcherFactory<?> codeRegistryDFF = codeRegistry.getDefaultDataFetcherFactory(); |
849 | 854 | if (codeRegistryDFF != null) { |
850 | | - return codeRegistryDFF; |
| 855 | + // this will use the default of the code registry when its |
| 856 | + // asked for at runtime |
| 857 | + return Optional.empty(); |
851 | 858 | } |
852 | | - dataFetcher = dataFetcherOfLastResort(wiringEnvironment); |
| 859 | + dataFetcher = dataFetcherOfLastResort(); |
853 | 860 | } |
854 | 861 | } |
855 | 862 | } |
856 | 863 | } |
857 | 864 | dataFetcherFactory = DataFetcherFactories.useDataFetcher(dataFetcher); |
858 | 865 | } |
859 | | - return dataFetcherFactory; |
| 866 | + return Optional.of(dataFetcherFactory); |
860 | 867 | } |
861 | 868 |
|
862 | 869 | GraphQLArgument buildArgument(BuildContext buildCtx, InputValueDefinition valueDefinition) { |
@@ -1087,9 +1094,8 @@ private Optional<OperationTypeDefinition> getOperationNamed(String name, Map<Str |
1087 | 1094 | return Optional.ofNullable(operationTypeDefs.get(name)); |
1088 | 1095 | } |
1089 | 1096 |
|
1090 | | - private DataFetcher<?> dataFetcherOfLastResort(FieldWiringEnvironment environment) { |
1091 | | - String fieldName = environment.getFieldDefinition().getName(); |
1092 | | - return new PropertyDataFetcher(fieldName); |
| 1097 | + private DataFetcher<?> dataFetcherOfLastResort() { |
| 1098 | + return SingletonPropertyDataFetcher.singleton(); |
1093 | 1099 | } |
1094 | 1100 |
|
1095 | 1101 | private List<Directive> directivesOf(List<? extends TypeDefinition<?>> typeDefinitions) { |
|
0 commit comments