@Override public Object get(DataFetchingEnvironment environment) { Object targetMethodResult = null; try { beforeInvocation(environment); Object[] bindByClassValues = collectBindByClassValues(environment); Object[] inputArguments = getMethodParametersBinder().bindParameters(environment.getArguments(), bindByClassValues); if (isAllNulls(inputArguments) && canApplySourceObject(environment)) { inputArguments = new Object[]{environment.getSource()}; } targetMethodResult = getTargetMethod().invoke(getTargetObject(), inputArguments); targetMethodResult = afterInvocation(environment, targetMethodResult); } catch (Exception e) { String msg = "Exception while calling data fetcher [" + getTargetMethod().getName() + "]"; if (LOGGER.isErrorEnabled()) LOGGER.error(msg, e); throw new DataFetcherRuntimeException(msg, e); } return targetMethodResult; }
protected boolean canApplySourceObject(DataFetchingEnvironment environment) { return environment.getSource() != null && getGraphQLMethodParameters().getNumberOfParameters() > 0? getGraphQLMethodParameters().isCompatibleWith(0, environment.getSource().getClass()) : false; }
public GraphQLFieldDefinition getMethodDefinition(DfsContext dfsContext, Class<?> objectClass, Method method) { GraphQLFieldDefinition graphQLFieldDefinition = null; ResolvableTypeAccessor resolvableTypeAccessor = ResolvableTypeAccessor.forMethodReturnType(method, objectClass); if (resolvableTypeAccessor.isGraphQLIdOrGraphQLField()) { GraphQLOutputType graphQLOutputType = (GraphQLOutputType) createGraphQLFieldType(dfsContext, resolvableTypeAccessor, true); List<GraphQLArgument> graphQLArguments = buildGraphQLArgumentsFromMethodParams(dfsContext, method, objectClass); GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition .newFieldDefinition() .name(resolvableTypeAccessor.getName()) .deprecate(resolvableTypeAccessor.getGraphQLDeprecationReason()) .argument(graphQLArguments) .type(graphQLOutputType) .description(resolvableTypeAccessor.getDescription()); if (!objectClass.isInterface()) { String beanName = objectClass.getName() + resolvableTypeAccessor.getName(); Object object = getGraphQLSchemaBeanFactory().getBeanByType(objectClass); builder.dataFetcher(new ReflectionGraphQLDataFetcher(getGraphQLSchemaConfig(), object, method)); } graphQLFieldDefinition = builder.build(); addToFieldDefinitionResolverMap(dfsContext, graphQLFieldDefinition, resolvableTypeAccessor.getGraphQLComplexitySpelExpression()); } return graphQLFieldDefinition; }