in aws-api-appsync/src/main/java/com/amplifyframework/api/aws/SelectionSet.java [360:411]
private Set<SelectionSet> getModelFields(ModelSchema modelSchema, int depth, Operation operation) {
if (depth < 0) {
return new HashSet<>();
}
Set<SelectionSet> result = new HashSet<>();
if (
depth == 0
&& LeafSerializationBehavior.JUST_ID.equals(requestOptions.leafSerializationBehavior())
&& operation != QueryType.SYNC
) {
result.add(new SelectionSet("id"));
return result;
}
SchemaRegistry modelSchemas = SchemaRegistry.instance();
for (Map.Entry<String, ModelField> entry : modelSchema.getFields().entrySet()) {
String fieldName = entry.getKey();
ModelAssociation association = modelSchema.getAssociations().get(fieldName);
if (association != null) {
if (depth >= 1) {
String associatedModelName = association.getAssociatedType();
ModelSchema associateModelSchema =
modelSchemas.getModelSchemaForModelClass(associatedModelName);
Set<SelectionSet> fields;
if (entry.getValue().isArray()) { // If modelField is an Array
fields = wrapPagination(getModelFields(associateModelSchema, depth - 1, operation));
} else {
fields = getModelFields(associateModelSchema, depth - 1, operation);
}
result.add(new SelectionSet(fieldName, fields));
}
} else if (entry.getValue().isCustomType()) {
CustomTypeSchema fieldCustomTypeSchema =
modelSchemas.getCustomTypeSchemaForCustomTypeClass(entry.getValue().getTargetType());
Set<SelectionSet> fields = getCustomTypeFields(fieldCustomTypeSchema);
result.add(new SelectionSet(fieldName, fields));
} else {
result.add(new SelectionSet(fieldName));
}
for (AuthRule authRule : modelSchema.getAuthRules()) {
if (AuthStrategy.OWNER.equals(authRule.getAuthStrategy())) {
result.add(new SelectionSet(authRule.getOwnerFieldOrDefault()));
break;
}
}
}
for (String fieldName : requestOptions.modelMetaFields()) {
result.add(new SelectionSet(fieldName));
}
return result;
}