private GraphQLFieldsContainer getFieldsContainer(ExecutionStepInfo executionStepInfo) { GraphQLOutputType type = executionStepInfo.getParent().getType(); return (GraphQLFieldsContainer) GraphQLTypeUtil.unwrapAll(type); }
private FetchedValueAnalysis analyzeList(ExecutionContext executionContext, FetchedValue fetchedValue, Object toAnalyze, ExecutionStepInfo executionInfo) { if (toAnalyze == null) { return newFetchedValueAnalysis(LIST) .fetchedValue(fetchedValue) .executionStepInfo(executionInfo) .nullValue() .build(); } if (toAnalyze.getClass().isArray() || toAnalyze instanceof Iterable) { Collection<Object> collection = FpKit.toCollection(toAnalyze); return analyzeIterable(executionContext, fetchedValue, collection, executionInfo); } else { TypeMismatchError error = new TypeMismatchError(executionInfo.getPath(), executionInfo.getType()); return newFetchedValueAnalysis(LIST) .fetchedValue(fetchedValue) .executionStepInfo(executionInfo) .nullValue() .error(error) .build(); } }
private GraphQLFieldsContainer getFieldsContainer(ExecutionStepInfo executionStepInfo) { GraphQLOutputType type = executionStepInfo.getParent().getType(); return (GraphQLFieldsContainer) GraphQLTypeUtil.unwrapAll(type); }
private EntityType getEntityTypeForQuery(DataFetchingEnvironment graphEnv) { String entityName = graphEnv.getExecutionStepInfo().getType().getName(); if (entityName == null) { entityName = graphEnv.getExecutionStepInfo().getType().getChildren().get(0).getName(); } EntityType et = env.getDefinitionsSet().getFirstEntityTypeByInterfaceName(namespace + ".model." + entityName); requireNonNull(et, "EntityType must exist"); return et; }
@Override protected FieldValueInfo completeValue(ExecutionContext executionContext, ExecutionStrategyParameters parameters) throws NonNullableFieldWasNullException { graphql.schema.GraphQLType fieldType = parameters.getExecutionStepInfo().getType(); Object result = parameters.getSource(); if (result instanceof Enum && fieldType instanceof GraphQLEnumType) { Object value = ((GraphQLEnumType) fieldType).getCoercing().parseValue(((Enum) result).name()); return super.completeValue(executionContext, withSource(parameters, value)); } if (result instanceof Optional) { Object value = ((Optional<?>) result).orElse(null); return completeValue(executionContext, withSource(parameters, value)); } return super.completeValue(executionContext, parameters); }
@Override public Object get(DataFetchingEnvironment graphEnv) throws Exception { EntityContext ctx = new EntityContext(env, namespace); QueryObject<Object> query = null; if (customQueries != null) { query = (QueryObject<Object>)customQueries.getQuery(graphEnv.getField().getName()); } if (query == null) { EntityType entityType = getEntityTypeForQuery(graphEnv); query = buildQuery(graphEnv, entityType); } else { buildQuery(graphEnv, query); } List<Entity> result = ctx.performQuery(query).getEntityList(); LOG.debug("Processed {} rows", ctx.getStatistics().getNumberOfRowsRead()); if (graphEnv.getExecutionStepInfo().getType() instanceof GraphQLList) { return Entity2Map.toListOfMaps(result); } else if (result.size() == 1) { return Entity2Map.toMap(result.get(0)); } throw new IllegalStateException("too many results"); }
private FetchedValueAnalysis analyzeList(ExecutionContext executionContext, FetchedValue fetchedValue, Object toAnalyze, ExecutionStepInfo executionInfo) { if (toAnalyze == null) { return newFetchedValueAnalysis(LIST) .fetchedValue(fetchedValue) .executionStepInfo(executionInfo) .nullValue() .build(); } if (toAnalyze.getClass().isArray() || toAnalyze instanceof Iterable) { Collection<Object> collection = FpKit.toCollection(toAnalyze); return analyzeIterable(executionContext, fetchedValue, collection, executionInfo); } else { TypeMismatchError error = new TypeMismatchError(executionInfo.getPath(), executionInfo.getType()); return newFetchedValueAnalysis(LIST) .fetchedValue(fetchedValue) .executionStepInfo(executionInfo) .nullValue() .error(error) .build(); } }
public static FragmentDirectiveCollector collect(DataFetchingEnvironment env, ExecutionStepInfo step) { FragmentDirectiveCollector fragmentDirectiveCollector = new FragmentDirectiveCollector(env); // This is safe because top-level fields don't get to here and all deeper fields at least have a parent (source object) and a grand-parent (query root) ExecutionStepInfo rootStep = step.getParent().getParent(); if (rootStep == null) { //Should never be possible, see above return fragmentDirectiveCollector; } GraphQLType rootParentType = GraphQLUtils.unwrapNonNull(rootStep.getType()); while(!(rootParentType instanceof GraphQLObjectType)) { rootStep = rootStep.getParent(); rootParentType = GraphQLUtils.unwrapNonNull(rootStep.getType()); } QueryTraversal traversal = QueryTraversal.newQueryTraversal() .fragmentsByName(env.getExecutionContext().getFragmentsByName()) .schema(env.getGraphQLSchema()) .variables(env.getExecutionContext().getVariables()) .root(env.getExecutionStepInfo().getParent().getField()) .rootParentType((GraphQLObjectType) rootParentType) .build(); traversal.visitPostOrder(fragmentDirectiveCollector); return fragmentDirectiveCollector; }