Search in sources :

Example 6 with ModelAssociation

use of com.amplifyframework.core.model.ModelAssociation in project amplify-android by aws-amplify.

the class SelectionSetTest method nestedSerializedModel.

/**
 * 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");
}
Also used : ModelSchema(com.amplifyframework.core.model.ModelSchema) ModelAssociation(com.amplifyframework.core.model.ModelAssociation) ModelField(com.amplifyframework.core.model.ModelField) HashMap(java.util.HashMap) SerializedModel(com.amplifyframework.core.model.SerializedModel) SchemaRegistry(com.amplifyframework.core.model.SchemaRegistry) Test(org.junit.Test)

Aggregations

ModelAssociation (com.amplifyframework.core.model.ModelAssociation)6 ModelField (com.amplifyframework.core.model.ModelField)5 HashMap (java.util.HashMap)4 ModelSchema (com.amplifyframework.core.model.ModelSchema)3 SerializedModel (com.amplifyframework.core.model.SerializedModel)3 Cursor (android.database.Cursor)1 SQLiteException (android.database.sqlite.SQLiteException)1 NonNull (androidx.annotation.NonNull)1 AmplifyException (com.amplifyframework.AmplifyException)1 AuthRule (com.amplifyframework.core.model.AuthRule)1 CustomTypeSchema (com.amplifyframework.core.model.CustomTypeSchema)1 Model (com.amplifyframework.core.model.Model)1 SchemaRegistry (com.amplifyframework.core.model.SchemaRegistry)1 SerializedCustomType (com.amplifyframework.core.model.SerializedCustomType)1 SQLiteTable (com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1