graphql.language.Selection Java Examples
The following examples show how to use
graphql.language.Selection.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SelectorToFieldMask.java From rejoiner with Apache License 2.0 | 6 votes |
public static Builder getFieldMaskForProto( DataFetchingEnvironment environment, Descriptor descriptor, String startAtFieldName) { Map<String, FragmentDefinition> fragmentsByName = environment.getFragmentsByName(); Builder maskFromSelectionBuilder = FieldMask.newBuilder(); for (Field field : environment.getFields()) { for (Selection<?> selection1 : field.getSelectionSet().getSelections()) { if (selection1 instanceof Field) { Field field2 = (Field) selection1; if (field2.getName().equals(startAtFieldName)) { for (Selection<?> selection : field2.getSelectionSet().getSelections()) { maskFromSelectionBuilder.addAllPaths( getPathsForProto("", selection, descriptor, fragmentsByName)); } } } } } return maskFromSelectionBuilder; }
Example #2
Source File: SelectorToFieldMask.java From rejoiner with Apache License 2.0 | 6 votes |
public static Builder getFieldMaskForProto( DataFetchingEnvironment environment, Descriptor descriptor) { Map<String, FragmentDefinition> fragmentsByName = environment.getFragmentsByName(); Builder maskFromSelectionBuilder = FieldMask.newBuilder(); for (Field field : Optional.ofNullable(environment.getMergedField()) .map(MergedField::getFields) .orElse(ImmutableList.of())) { for (Selection<?> selection : field.getSelectionSet().getSelections()) { maskFromSelectionBuilder.addAllPaths( getPathsForProto("", selection, descriptor, fragmentsByName)); } } return maskFromSelectionBuilder; }
Example #3
Source File: GqlParentType.java From manifold with Apache License 2.0 | 6 votes |
private void addQueryResultType( OperationDefinition operation, TypeDefinition ctx, SrcLinkedClass enclosingType ) { String fqn = enclosingType.getName() + ".Result"; SrcLinkedClass srcClass = new SrcLinkedClass( fqn, enclosingType, Interface ) .addInterface( GqlQueryResult.class.getSimpleName() ) .addAnnotation( new SrcAnnotationExpression( Structural.class.getSimpleName() ) .addArgument( "factoryClass", Class.class, "Result.ProxyFactory.class" ) ) .modifiers( Modifier.PUBLIC ); addProxyFactory( srcClass ); for( Selection member: operation.getSelectionSet().getSelections() ) { addQuerySelection( srcClass, ctx, member ); } enclosingType.addInnerClass( srcClass ); }
Example #4
Source File: GraphQLAPIHandler.java From carbon-apimgt with Apache License 2.0 | 6 votes |
/** * This method support to extracted nested level operations * @param selectionList selection List * @param supportedFields supportedFields * @param operationArray operationArray */ public void getNestedLevelOperations(List<Selection> selectionList, ArrayList<String> supportedFields, ArrayList<String> operationArray) { for (Selection selection : selectionList) { Field levelField = (Field) selection; if (!operationArray.contains(levelField.getName()) && supportedFields.contains(levelField.getName())) { operationArray.add(levelField.getName()); if (log.isDebugEnabled()) { log.debug("Extracted operation: " + levelField.getName()); } } if (levelField.getSelectionSet() != null) { getNestedLevelOperations(levelField.getSelectionSet().getSelections(), supportedFields, operationArray); } } }
Example #5
Source File: ComplexityAnalyzer.java From graphql-spqr with Apache License 2.0 | 5 votes |
private void collectFields(FieldCollectorParameters parameters, Map<String, List<ResolvedField>> fields, List<Selection> selectionSet, List<String> visitedFragments, GraphQLFieldsContainer parent) { for (Selection selection : selectionSet) { if (selection instanceof Field) { collectField(parameters, fields, (Field) selection, parent); } else if (selection instanceof InlineFragment) { collectInlineFragment(parameters, fields, visitedFragments, (InlineFragment) selection, parent); } else if (selection instanceof FragmentSpread) { collectFragmentSpread(parameters, fields, visitedFragments, (FragmentSpread) selection, parent); } } }
Example #6
Source File: ComplexityAnalyzer.java From graphql-spqr with Apache License 2.0 | 5 votes |
private Map<String, List<Selection>> getConditionalSelections(SelectionSet selectionSet) { return selectionSet.getSelections().stream() .filter(this::isConditional) .collect(Collectors.groupingBy(s -> s instanceof FragmentDefinition ? ((FragmentDefinition) s).getTypeCondition().getName() : ((InlineFragment) s).getTypeCondition().getName())); }
Example #7
Source File: ComplexityAnalyzer.java From graphql-spqr with Apache License 2.0 | 4 votes |
private List<Selection> getUnconditionalSelections(SelectionSet selectionSet) { return selectionSet.getSelections().stream() .filter(selection -> !isConditional(selection)) .collect(Collectors.toList()); }
Example #8
Source File: ComplexityAnalyzer.java From graphql-spqr with Apache License 2.0 | 4 votes |
private boolean isConditional(Selection selection) { return (selection instanceof FragmentDefinition && ((FragmentDefinition) selection).getTypeCondition() != null) || (selection instanceof InlineFragment && ((InlineFragment) selection).getTypeCondition() != null); }
Example #9
Source File: ContentTypeBasedDataFetcher.java From engine with GNU General Public License v3.0 | 4 votes |
/** * {@inheritDoc} */ @Override public Object doGet(final DataFetchingEnvironment env) { Field field = env.getMergedField().getSingleField(); String fieldName = field.getName(); // Get arguments for pagination & sorting int offset = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_OFFSET)).orElse(0); int limit = Optional.ofNullable(env.<Integer>getArgument(ARG_NAME_LIMIT)).orElse(defaultLimit); String sortBy = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_BY)).orElse(defaultSortField); String sortOrder = Optional.ofNullable(env.<String>getArgument(ARG_NAME_SORT_ORDER)).orElse(defaultSortOrder); List<String> queryFieldIncludes = new LinkedList<>(); // Add content-type to includes, we might need it for a GraphQL TypeResolver queryFieldIncludes.add(QUERY_FIELD_NAME_CONTENT_TYPE); List<Map<String, Object>> items = new LinkedList<>(); Map<String, Object> result = new HashMap<>(2); result.put(FIELD_NAME_ITEMS, items); // Setup the ES query SearchSourceBuilder source = new SearchSourceBuilder(); BoolQueryBuilder query = boolQuery(); source .query(query) .from(offset) .size(limit) .sort(sortBy, SortOrder.fromString(sortOrder)); StopWatch watch = new StopWatch(field.getName() + " - " + field.getAlias()); watch.start("build filters"); // Filter by the content-type switch (fieldName) { case FIELD_NAME_CONTENT_ITEMS: query.filter(existsQuery(QUERY_FIELD_NAME_CONTENT_TYPE)); break; case FIELD_NAME_PAGES: query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_PAGE)); break; case FIELD_NAME_COMPONENTS: query.filter(regexpQuery(QUERY_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_REGEX_COMPONENT)); break; default: // Get the content-type name from the field name query.filter(termQuery(QUERY_FIELD_NAME_CONTENT_TYPE, getOriginalName(fieldName))); break; } // Check the selected fields to build the ES query Optional<Field> itemsField = field.getSelectionSet().getSelections() .stream() .map(f -> (Field) f) .filter(f -> f.getName().equals(FIELD_NAME_ITEMS)) .findFirst(); if (itemsField.isPresent()) { List<Selection> selections = itemsField.get().getSelectionSet().getSelections(); selections.forEach(selection -> processSelection(StringUtils.EMPTY, selection, query, queryFieldIncludes, env)); } // Only fetch the selected fields for better performance source.fetchSource(queryFieldIncludes.toArray(new String[0]), new String[0]); watch.stop(); logger.debug("Executing query: {}", source); watch.start("searching items"); SearchResponse response = elasticsearch.search(new SearchRequest().source(source)); watch.stop(); watch.start("processing items"); result.put(FIELD_NAME_TOTAL, response.getHits().totalHits); if (response.getHits().totalHits > 0) { for(SearchHit hit : response.getHits().getHits()) { items.add(fixItems(hit.getSourceAsMap())); } } watch.stop(); if (logger.isTraceEnabled()) { logger.trace(watch.prettyPrint()); } return result; }
Example #10
Source File: ContentTypeBasedDataFetcher.java From engine with GNU General Public License v3.0 | 4 votes |
/** * Adds the required filters to the ES query for the given field */ protected void processSelection(String path, Selection currentSelection, BoolQueryBuilder query, List<String> queryFieldIncludes, DataFetchingEnvironment env) { if (currentSelection instanceof Field) { // If the current selection is a field Field currentField = (Field) currentSelection; // Get the original field name String propertyName = getOriginalName(currentField.getName()); // Build the ES-friendly path String fullPath = StringUtils.isEmpty(path)? propertyName : path + "." + propertyName; // If the field has sub selection if (Objects.nonNull(currentField.getSelectionSet())) { // If the field is a flattened component if (fullPath.matches(COMPONENT_INCLUDE_REGEX)) { // Include the 'content-type' field to make sure the type can be resolved during runtime String contentTypeFieldPath = fullPath + "." + QUERY_FIELD_NAME_CONTENT_TYPE; if (!queryFieldIncludes.contains(contentTypeFieldPath)) { queryFieldIncludes.add(contentTypeFieldPath); } } // Process recursively and finish currentField.getSelectionSet().getSelections() .forEach(selection -> processSelection(fullPath, selection, query, queryFieldIncludes, env)); return; } // Add the field to the list logger.debug("Adding selected field '{}' to query", fullPath); queryFieldIncludes.add(fullPath); // Check the filters to build the ES query Optional<Argument> arg = currentField.getArguments().stream().filter(a -> a.getName().equals(FILTER_NAME)).findFirst(); if (arg.isPresent()) { logger.debug("Adding filters for field {}", fullPath); Value<?> argValue = arg.get().getValue(); if (argValue instanceof ObjectValue) { List<ObjectField> filters = ((ObjectValue) argValue).getObjectFields(); filters.forEach((filter) -> addFieldFilterFromObjectField(fullPath, filter, query, env)); } else if (argValue instanceof VariableReference && env.getVariables().containsKey(((VariableReference) argValue).getName())) { Map<String, Object> map = (Map<String, Object>) env.getVariables().get(((VariableReference) argValue).getName()); map.entrySet().forEach(filter -> addFieldFilterFromMapEntry(fullPath, filter, query, env)); } } } else if (currentSelection instanceof InlineFragment) { // If the current selection is an inline fragment, process recursively InlineFragment fragment = (InlineFragment) currentSelection; fragment.getSelectionSet().getSelections() .forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env)); } else if (currentSelection instanceof FragmentSpread) { // If the current selection is a fragment spread, find the fragment and process recursively FragmentSpread fragmentSpread = (FragmentSpread) currentSelection; FragmentDefinition fragmentDefinition = env.getFragmentsByName().get(fragmentSpread.getName()); fragmentDefinition.getSelectionSet().getSelections() .forEach(selection -> processSelection(path, selection, query, queryFieldIncludes, env)); } }
Example #11
Source File: ExecutionTreeNode.java From hypergraphql with Apache License 2.0 | 2 votes |
private Map<Service, Set<Field>> getPartitionedFields(String parentType, SelectionSet selectionSet) { Map<Service, Set<Field>> result = new HashMap<>(); List<Selection> selections = selectionSet.getSelections(); for (Selection child : selections) { if (child.getClass().getSimpleName().equals("Field")) { Field field = (Field) child; if (hgqlSchema.getFields().containsKey(field.getName())) { Service serviceConfig; if(hgqlSchema.getTypes().containsKey(parentType)) { if(hgqlSchema.getTypes().get(parentType).getFields().containsKey(field.getName())) { serviceConfig = hgqlSchema.getTypes().get(parentType).getFields().get(field.getName()).getService(); } else { throw new HGQLConfigurationException("Schema is missing field '" + parentType + "::" + field.getName() + "'"); } } else { throw new HGQLConfigurationException("Schema is missing type '" + parentType + "'"); } if (result.containsKey(serviceConfig)) { result.get(serviceConfig).add(field); } else { Set<Field> newFieldSet = new HashSet<>(); newFieldSet.add(field); result.put(serviceConfig, newFieldSet); } } } } return result; }
Example #12
Source File: ExecutionTreeNode.java From hypergraphql with Apache License 2.0 | 2 votes |
private Map<Service, Set<Field>> getPartitionedFields(String parentType, SelectionSet selectionSet) { Map<Service, Set<Field>> result = new HashMap<>(); List<Selection> selections = selectionSet.getSelections(); for (Selection child : selections) { if (child.getClass().getSimpleName().equals("Field")) { Field field = (Field) child; if (hgqlSchema.getFields().containsKey(field.getName())) { Service serviceConfig; if(hgqlSchema.getTypes().containsKey(parentType)) { if(hgqlSchema.getTypes().get(parentType).getFields().containsKey(field.getName())) { serviceConfig = hgqlSchema.getTypes().get(parentType).getFields().get(field.getName()).getService(); } else { throw new HGQLConfigurationException("Schema is missing field '" + parentType + "::" + field.getName() + "'"); } } else { throw new HGQLConfigurationException("Schema is missing type '" + parentType + "'"); } if (result.containsKey(serviceConfig)) { result.get(serviceConfig).add(field); } else { Set<Field> newFieldSet = new HashSet<>(); newFieldSet.add(field); result.put(serviceConfig, newFieldSet); } } } } return result; }