graphql.language.SelectionSet Java Examples

The following examples show how to use graphql.language.SelectionSet. 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: SelectorToFieldMaskTest.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@Test
public void getFieldMaskForProtoShouldReturnSingleField() {
  assertThat(
          SelectorToFieldMask.getFieldMaskForProto(
                  DataFetchingEnvironmentImpl.newDataFetchingEnvironment()
                      .mergedField(
                          MergedField.newMergedField()
                              .addField(
                                  new Field(
                                      "top_level_field",
                                      new SelectionSet(ImmutableList.of(new Field("username")))))
                              .build())
                      .build(),
                 PersonOuterClass.Person.getDescriptor())
              .build())
      .isEqualTo(FieldMask.newBuilder().addPaths("username").build());
}
 
Example #2
Source File: SelectorToFieldMaskTest.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@Test
public void getFieldMaskForProtoShouldIgnoreSelectorsNotInProto() {
  assertThat(
          SelectorToFieldMask.getFieldMaskForProto(
                  DataFetchingEnvironmentImpl.newDataFetchingEnvironment()
                      .mergedField(
                          MergedField.newMergedField()
                              .addField(
                                  new Field(
                                      "top_level_field",
                                      new SelectionSet(
                                          ImmutableList.of(
                                              new Field("username"),
                                              new Field("notInPersonProto")))))
                              .build())
                      .build(),
                 PersonOuterClass.Person.getDescriptor())
              .build())
      .isEqualTo(FieldMask.newBuilder().addPaths("username").build());
}
 
Example #3
Source File: SelectorToFieldMaskTest.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@Test
public void getFieldMaskForProtoShouldReturnNestedField() {
  assertThat(
          SelectorToFieldMask.getFieldMaskForProto(
                  DataFetchingEnvironmentImpl.newDataFetchingEnvironment()
                      .mergedField(
                          MergedField.newMergedField()
                              .addField(
                                  new Field(
                                      "top_level_field",
                                      new SelectionSet(
                                          ImmutableList.of(
                                              new Field(
                                                  "birthday",
                                                  new SelectionSet(
                                                      ImmutableList.of(new Field("month"))))))))
                              .build())
                      .build(),
                 PersonOuterClass.Person.getDescriptor())
              .build())
      .isEqualTo(FieldMask.newBuilder().addPaths("birthday.month").build());
}
 
Example #4
Source File: SelectorToFieldMaskTest.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@Test
public void getFieldMaskForProtoShouldReturnMultipleNestedField() {
  assertThat(
          SelectorToFieldMask.getFieldMaskForProto(
                  DataFetchingEnvironmentImpl.newDataFetchingEnvironment()
                      .mergedField(
                          MergedField.newMergedField()
                              .addField(
                                  new Field(
                                      "top_level_field",
                                      new SelectionSet(
                                          ImmutableList.of(
                                              new Field(
                                                  "birthday",
                                                  new SelectionSet(
                                                      ImmutableList.of(
                                                          new Field("day"),
                                                          new Field("month"))))))))
                              .build())
                      .build(),
                 PersonOuterClass.Person.getDescriptor())
              .build())
      .isEqualTo(
          FieldMask.newBuilder().addPaths("birthday.day").addPaths("birthday.month").build());
}
 
Example #5
Source File: SelectorToFieldMaskTest.java    From rejoiner with Apache License 2.0 6 votes vote down vote up
@Test
public void getFieldMaskForProtoShouldFallbackToStarPathIfSubSelectorNameDoesntMatchProto() {
  assertThat(
          SelectorToFieldMask.getFieldMaskForProto(
                  DataFetchingEnvironmentImpl.newDataFetchingEnvironment()
                      .mergedField(
                          MergedField.newMergedField()
                              .addField(
                                  new Field(
                                      "top_level_field",
                                      new SelectionSet(
                                          ImmutableList.of(
                                              new Field(
                                                  "birthday",
                                                  new SelectionSet(
                                                      ImmutableList.of(new Field("unknown"))))))))
                              .build())
                      .build(),
                 PersonOuterClass.Person.getDescriptor())
              .build())
      .isEqualTo(FieldMask.newBuilder().addPaths("birthday.*").build());
}
 
Example #6
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 #7
Source File: SelectorToFieldMaskTest.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
@Test
public void getFieldMaskForChildProto() {
  assertThat(
          SelectorToFieldMask.getFieldMaskForProto(
                  DataFetchingEnvironmentImpl.newDataFetchingEnvironment()
                      .mergedField(
                          MergedField.newMergedField()
                              .addField(
                                  new Field(
                                      "top_level_field", // QueryType
                                      new SelectionSet(
                                          ImmutableList.of(
                                              new Field(
                                                  "second_level_field", // RequestType
                                                  new SelectionSet(
                                                      ImmutableList.of(
                                                          new Field(
                                                              "birthday",
                                                              new SelectionSet(
                                                                  ImmutableList.of(
                                                                      new Field("month")))))))))))
                              .build())
                      .build(),
                 PersonOuterClass.Person.getDescriptor(),
                  "second_level_field")
              .build())
      .isEqualTo(FieldMask.newBuilder().addPaths("birthday.month").build());
}
 
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: Generator.java    From smallrye-graphql with Apache License 2.0 4 votes vote down vote up
private Stream<? extends Field> selectedFields(Field field) {
    SelectionSet selectionSet = field.getSelectionSet();
    return (selectionSet == null) ? Stream.of() : selectionSet.getSelectionsOfType(Field.class).stream();
}
 
Example #10
Source File: ExecutionForestFactory.java    From hypergraphql with Apache License 2.0 4 votes vote down vote up
public ExecutionForest getExecutionForest(Document queryDocument , HGQLSchema schema) {

        ExecutionForest forest = new ExecutionForest();

        SelectionSet queryFields = selectionSet(queryDocument);

        final AtomicInteger counter = new AtomicInteger(0);
        queryFields.getSelections().forEach(child -> { // query fields - why no args?

            if (child.getClass().getSimpleName().equals("Field")) {

                String nodeId = "x_" + counter.incrementAndGet();
                forest.getForest().add(new ExecutionTreeNode((Field) child, nodeId , schema));

            }
        });
        return forest;
    }
 
Example #11
Source File: ComplexityAnalyzer.java    From graphql-spqr with Apache License 2.0 4 votes vote down vote up
private List<Selection> getUnconditionalSelections(SelectionSet selectionSet) {
    return selectionSet.getSelections().stream()
            .filter(selection -> !isConditional(selection))
            .collect(Collectors.toList());
}
 
Example #12
Source File: ExecutionTreeNode.java    From hypergraphql with Apache License 2.0 2 votes vote down vote up
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 #13
Source File: ExecutionTreeNode.java    From hypergraphql with Apache License 2.0 2 votes vote down vote up
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;
    }