graphql.language.FragmentDefinition Java Examples

The following examples show how to use graphql.language.FragmentDefinition. 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 vote down vote up
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 vote down vote up
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: ExecutionForestFactory.java    From hypergraphql with Apache License 2.0 5 votes vote down vote up
private SelectionSet selectionSet(final Document queryDocument) {

        final Definition definition = queryDocument.getDefinitions().get(0);

        if(definition.getClass().isAssignableFrom(FragmentDefinition.class)) {

            return getFragmentSelectionSet(queryDocument);

        } else if(definition.getClass().isAssignableFrom(OperationDefinition.class)) {
            final OperationDefinition operationDefinition = (OperationDefinition)definition;
            return operationDefinition.getSelectionSet();
        }
        throw new IllegalArgumentException(queryDocument.getClass().getName() + " is not supported");
    }
 
Example #4
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
GqlParentType( String fqn, SchemaDefinition schemaDefinition, TypeDefinitionRegistry registry,
               Map<String, OperationDefinition> operations, Map<String, FragmentDefinition> fragments,
               IFile file, GqlManifold gqlManifold )
{
  _fqn = fqn;
  _schemaDefinition = schemaDefinition;
  _registry = registry;
  _operations = operations;
  _fragments = fragments;
  _file = file;
  _gqlManifold = gqlManifold;
  _typeToUnions = new HashMap<>();
}
 
Example #5
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private String getFragments( SrcLinkedClass srcClass )
{
  //noinspection unchecked
  Map<String, FragmentDefinition> fragments = (Map)srcClass.getUserData( "fragments" );
  if( fragments == null )
  {
    return "";
  }

  StringBuilder sb = new StringBuilder();
  fragments.values().forEach( fragment ->
    sb.append( ManEscapeUtil.escapeForJavaStringLiteral( AstPrinter.printAstCompact( fragment ) ) ).append( " " ) );
  return sb.toString();
}
 
Example #6
Source File: GqlParentType.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void mapFragmentUsageToQuery( SrcLinkedClass srcClass, String name, FragmentDefinition fragment )
{
  SrcLinkedClass operation = findOperation( srcClass );
  //noinspection unchecked
  Map<String, FragmentDefinition> fragments = (Map)operation.computeOrGetUserData( "fragments",
    key -> new LinkedHashMap<String, FragmentDefinition>() );
  fragments.put( name, fragment );
}
 
Example #7
Source File: GqlModel.java    From manifold with Apache License 2.0 5 votes vote down vote up
private void buildRegistry( Document document )
{
  TypeDefinitionRegistry typeRegistry = new TypeDefinitionRegistry();
  List<Definition> definitions = document.getDefinitions();
  Map<String, OperationDefinition> operations = new LinkedHashMap<>();
  Map<String, FragmentDefinition> fragments = new LinkedHashMap<>();
  List<GraphQLError> errors = new ArrayList<>();
  for( Definition definition: definitions )
  {
    if( definition instanceof SchemaDefinition )
    {
      _schemaDefinition = (SchemaDefinition)definition;
    }
    else if( definition instanceof SDLDefinition )
    {
      // types, interfaces, unions, inputs, scalars, extensions
      typeRegistry.add( (SDLDefinition)definition ).ifPresent( errors::add );
      if( definition instanceof ScalarTypeDefinition )
      {
        // register scalar type
        typeRegistry.scalars().put( ((ScalarTypeDefinition)definition).getName(), (ScalarTypeDefinition)definition );
      }
    }
    else if( definition instanceof OperationDefinition )
    {
      // queries, mutations, subscriptions
      operations.put( ((OperationDefinition)definition).getName(), (OperationDefinition)definition );
    }
    else if( definition instanceof FragmentDefinition )
    {
      // fragments
      fragments.put( ((FragmentDefinition)definition).getName(), (FragmentDefinition)definition );
    }
  }
  _issues = new GqlIssueContainer( errors, getFile() );
  _typeRegistry = typeRegistry;
  _operations = operations;
  _fragments = fragments;
}
 
Example #8
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
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 #9
Source File: RelayDataFetchingEnvironmentDecorator.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, FragmentDefinition> getFragmentsByName() {
    return delegate.getFragmentsByName();
}
 
Example #10
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
private boolean isConditional(Selection selection) {
    return (selection instanceof FragmentDefinition && ((FragmentDefinition) selection).getTypeCondition() != null)
            || (selection instanceof InlineFragment && ((InlineFragment) selection).getTypeCondition() != null);
}
 
Example #11
Source File: ContentTypeBasedDataFetcher.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 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 #12
Source File: LookUpSubjectByUriFetcherWrapperTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Map<String, FragmentDefinition> getFragmentsByName() {
  throw new IllegalStateException("Not implemented yet");
}