Java源码示例:graphql.language.SelectionSet
示例1
@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());
}
示例2
@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());
}
示例3
@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());
}
示例4
@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());
}
示例5
@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());
}
示例6
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");
}
示例7
@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());
}
示例8
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()));
}
示例9
private Stream<? extends Field> selectedFields(Field field) {
SelectionSet selectionSet = field.getSelectionSet();
return (selectionSet == null) ? Stream.of() : selectionSet.getSelectionsOfType(Field.class).stream();
}
示例10
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;
}
示例11
private List<Selection> getUnconditionalSelections(SelectionSet selectionSet) {
return selectionSet.getSelections().stream()
.filter(selection -> !isConditional(selection))
.collect(Collectors.toList());
}
示例12
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;
}
示例13
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;
}