|
| 1 | +/** |
| 2 | + * Copyright 2016 Yurii Rashkovskii |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + */ |
| 15 | +package graphql.annotations; |
| 16 | + |
| 17 | +import graphql.schema.DataFetcher; |
| 18 | +import graphql.schema.DataFetchingEnvironment; |
| 19 | +import graphql.schema.DataFetchingEnvironmentImpl; |
| 20 | + |
| 21 | +import java.lang.reflect.Constructor; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Optional; |
| 25 | + |
| 26 | +import static graphql.annotations.ReflectionKit.constructNewInstance; |
| 27 | + |
| 28 | + class ConnectionDataFetcher implements DataFetcher { |
| 29 | + private final Class<? extends Connection> connection; |
| 30 | + private final DataFetcher actualDataFetcher; |
| 31 | + private final Constructor<Connection> constructor; |
| 32 | + |
| 33 | + public ConnectionDataFetcher(Class<? extends Connection> connection, DataFetcher actualDataFetcher) { |
| 34 | + this.connection = connection; |
| 35 | + Optional<Constructor<Connection>> constructor = |
| 36 | + Arrays.asList(connection.getConstructors()).stream(). |
| 37 | + filter(c -> c.getParameterCount() == 1). |
| 38 | + map(c -> (Constructor<Connection>) c). |
| 39 | + findFirst(); |
| 40 | + if (constructor.isPresent()) { |
| 41 | + this.constructor = constructor.get(); |
| 42 | + } else { |
| 43 | + throw new IllegalArgumentException(connection + " doesn't have a single argument constructor"); |
| 44 | + } |
| 45 | + this.actualDataFetcher = actualDataFetcher; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public Object get(DataFetchingEnvironment environment) { |
| 50 | + // Create a list of arguments with connection specific arguments excluded |
| 51 | + HashMap<String, Object> arguments = new HashMap<>(environment.getArguments()); |
| 52 | + arguments.keySet().removeAll(Arrays.asList("first", "last", "before", "after")); |
| 53 | + DataFetchingEnvironment env = new DataFetchingEnvironmentImpl(environment.getSource(), arguments, environment.getContext(), |
| 54 | + environment.getRoot(), environment.getFieldDefinition(), environment.getFields(), environment.getFieldType(), environment.getParentType(), environment.getGraphQLSchema(), |
| 55 | + environment.getFragmentsByName(), environment.getExecutionId(), environment.getSelectionSet(), environment.getFieldTypeInfo()); |
| 56 | + Object data = actualDataFetcher.get(env); |
| 57 | + if (data != null) { |
| 58 | + Connection conn = constructNewInstance(constructor, data); |
| 59 | + return conn.get(environment); |
| 60 | + } |
| 61 | + return null; |
| 62 | + } |
| 63 | +} |
0 commit comments