Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-api-appsync): update getModelFields for flutter support #1611

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public SelectionSet build() throws AmplifyException {
Objects.requireNonNull(this.operation);
SelectionSet node = new SelectionSet(null,
SerializedModel.class == modelClass
? getModelFields(modelSchema, requestOptions.maxDepth())
? getModelFields(modelSchema, requestOptions.maxDepth(), operation)
: getModelFields(modelClass, requestOptions.maxDepth(), operation));
if (QueryType.LIST.equals(operation) || QueryType.SYNC.equals(operation)) {
node = wrapPagination(node);
Expand Down Expand Up @@ -243,7 +243,7 @@ private Set<SelectionSet> wrapPagination(Set<SelectionSet> nodes) {

/**
* Gets a selection set for the given class.
* TODO: this is mostly duplicative of {@link #getModelFields(ModelSchema, int)}.
* TODO: this is mostly duplicative of {@link #getModelFields(ModelSchema, int, Operation)}.
* Long-term, we want to remove this current method and rely only on the ModelSchema-based
* version.
* @param clazz Class from which to build selection set
Expand Down Expand Up @@ -357,12 +357,16 @@ static Class<?> getClassForField(Field field) {

// TODO: this method is tech debt. We added it to support usage of the library from Flutter.
// This version of the method needs to be unified with getModelFields(Class<? extends Model> clazz, int depth).
private Set<SelectionSet> getModelFields(ModelSchema modelSchema, int depth) {
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())) {
if (
depth == 0
&& LeafSerializationBehavior.JUST_ID.equals(requestOptions.leafSerializationBehavior())
&& operation != QueryType.SYNC
) {
result.add(new SelectionSet("id"));
return result;
}
Expand All @@ -379,9 +383,9 @@ private Set<SelectionSet> getModelFields(ModelSchema modelSchema, int depth) {
modelSchemas.getModelSchemaForModelClass(associatedModelName);
Set<SelectionSet> fields;
if (entry.getValue().isArray()) { // If modelField is an Array
fields = wrapPagination(getModelFields(associateModelSchema, depth - 1));
fields = wrapPagination(getModelFields(associateModelSchema, depth - 1, operation));
} else {
fields = getModelFields(associateModelSchema, depth - 1);
fields = getModelFields(associateModelSchema, depth - 1, operation);
}
result.add(new SelectionSet(fieldName, fields));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.api.aws;

import androidx.annotation.NonNull;

import java.util.Collections;
import java.util.List;

/**
* Copy of DefaultGraphQLRequestOptions, with LeafSerializationBehavior set to JUST_ID
* and maxDepth of 1.
*/
public final class JustIDGraphQLRequestOptions implements GraphQLRequestOptions {
private static final String ITEMS_KEY = "items";
private static final String NEXT_TOKEN_KEY = "nextToken";

@NonNull
@Override
public List<String> paginationFields() {
return Collections.singletonList(NEXT_TOKEN_KEY);
}

@NonNull
@Override
public List<String> modelMetaFields() {
return Collections.emptyList();
}

@NonNull
@Override
public String listField() {
return ITEMS_KEY;
}

@Override
public int maxDepth() {
return 1;
}

@NonNull
@Override
public LeafSerializationBehavior leafSerializationBehavior() {
return LeafSerializationBehavior.JUST_ID;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.amplifyframework.core.model.AuthStrategy;
import com.amplifyframework.core.model.CustomTypeField;
import com.amplifyframework.core.model.CustomTypeSchema;
import com.amplifyframework.core.model.ModelAssociation;
import com.amplifyframework.core.model.ModelField;
import com.amplifyframework.core.model.ModelOperation;
import com.amplifyframework.core.model.ModelSchema;
Expand Down Expand Up @@ -267,4 +268,76 @@ public void nestedSerializedModelAndSerializedCustomType() throws AmplifyExcepti
assertEquals(Resources.readAsString("selection-set-nested-serialized-model-serialized-custom-type.txt"),
result + "\n");
}

/**
* Test generating SelectionSet for nested ModelSchema using SerializedModel.
* @throws AmplifyException if a ModelSchema can't be derived from postSchema
*/
@Test
public void nestedSerializedModel() throws AmplifyException {
SchemaRegistry schemaRegistry = SchemaRegistry.instance();
ModelField blogModelId = ModelField.builder()
.isRequired(true)
.targetType("ID")
.build();
ModelField blogName = ModelField.builder()
.isRequired(true)
.targetType("String")
.build();
Map<String, ModelField> blogFields = new HashMap<>();
blogFields.put("id", blogModelId);
blogFields.put("name", blogName);

ModelSchema blogSchema = ModelSchema.builder()
.name("Blog")
.pluralName("Blogs")
.modelClass(SerializedModel.class)
.fields(blogFields)
.build();

ModelField postModelId = ModelField.builder()
.isRequired(true)
.targetType("ID")
.build();
ModelField postTitle = ModelField.builder()
.isRequired(true)
.targetType("String")
.build();
ModelField postBlog = ModelField.builder()
.isRequired(true)
.targetType("Blog")
.isModel(true)
.build();
Map<String, ModelField> postFields = new HashMap<>();
postFields.put("id", postModelId);
postFields.put("title", postTitle);
postFields.put("blog", postBlog);

Map<String, ModelAssociation> associations = new HashMap<>();
associations.put("blog", ModelAssociation.builder()
.name("BelongsTo")
.targetName("blogId")
.associatedType("Blog")
.build());

ModelSchema postSchema = ModelSchema.builder()
.name("Post")
.pluralName("Posts")
.modelClass(SerializedModel.class)
.fields(postFields)
.associations(associations)
.build();

schemaRegistry.register("Blog", blogSchema);
schemaRegistry.register("Post", postSchema);

SelectionSet selectionSet = SelectionSet.builder()
.modelClass(SerializedModel.class)
.modelSchema(postSchema)
.operation(QueryType.SYNC)
.requestOptions(new JustIDGraphQLRequestOptions())
.build();

assertEquals(Resources.readAsString("selection-set-post-nested.txt"), selectionSet.toString() + "\n");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
items {
blog {
id
name
}
id
title
}
nextToken
}