-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Embeddable type to store schema info for custom types (#539)
* Add Embbeded types support * Renaming to Embeddable type
- Loading branch information
Showing
17 changed files
with
436 additions
and
19 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
// MARK: - Embeddable | ||
|
||
/// A `Embeddable` type can be used in a `Model` as an embedded type. All types embedded in a `Model` as an | ||
/// `embedded(type:)` or `embeddedCollection(of:)` must comform to the `Embeddable` protocol except for Swift's Basic | ||
/// types embedded as a collection. A collection of String can be embedded in the `Model` as | ||
/// `embeddedCollection(of: String.self)` without needing to conform to Embeddable. | ||
public protocol Embeddable: Codable { | ||
|
||
/// A reference to the `ModelSchema` associated with this embedded type. | ||
static var schema: ModelSchema { get } | ||
} | ||
|
||
extension Embeddable { | ||
public static func defineSchema(name: String? = nil, | ||
attributes: ModelAttribute..., | ||
define: (inout ModelSchemaDefinition) -> Void) -> ModelSchema { | ||
var definition = ModelSchemaDefinition(name: name ?? "", | ||
attributes: attributes) | ||
define(&definition) | ||
return definition.build() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...fyPlugins/Core/AWSPluginsCoreTests/Model/GraphQLRequest/GraphQLRequestNonModelTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import XCTest | ||
|
||
@testable import Amplify | ||
@testable import AmplifyTestCommon | ||
@testable import AWSPluginsCore | ||
|
||
class GraphQLRequestNonModelTests: XCTestCase { | ||
|
||
override func setUp() { | ||
ModelRegistry.register(modelType: Todo.self) | ||
} | ||
|
||
override func tearDown() { | ||
ModelRegistry.reset() | ||
} | ||
|
||
func testCreateTodoGraphQLRequest() { | ||
let color1 = Color(name: "color1", red: 1, green: 2, blue: 3) | ||
let color2 = Color(name: "color2", red: 12, green: 13, blue: 14) | ||
let category1 = Category(name: "green", color: color1) | ||
let category2 = Category(name: "red", color: color2) | ||
let section = Section(name: "section", number: 1.1) | ||
let todo = Todo(name: "my first todo", | ||
description: "todo description", | ||
categories: [category1, category2], | ||
section: section) | ||
let documentStringValue = """ | ||
mutation CreateTodo($input: CreateTodoInput!) { | ||
createTodo(input: $input) { | ||
id | ||
categories { | ||
color { | ||
blue | ||
green | ||
name | ||
red | ||
__typename | ||
} | ||
name | ||
__typename | ||
} | ||
description | ||
name | ||
section { | ||
name | ||
number | ||
__typename | ||
} | ||
stickies | ||
__typename | ||
} | ||
} | ||
""" | ||
let request = GraphQLRequest<Todo>.create(todo) | ||
XCTAssertEqual(documentStringValue, request.document) | ||
|
||
guard let variables = request.variables else { | ||
XCTFail("The request doesn't contain variables") | ||
return | ||
} | ||
guard let input = variables["input"] as? [String: Any] else { | ||
XCTFail("The document variables property doesn't contain a valid input") | ||
return | ||
} | ||
XCTAssertEqual(input["id"] as? String, todo.id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
// swiftlint:disable all | ||
import Amplify | ||
import Foundation | ||
|
||
public struct Category: Embeddable { | ||
var name: String | ||
var color: Color | ||
} | ||
|
||
extension Category { | ||
|
||
public enum CodingKeys: CodingKey { | ||
case name | ||
case color | ||
} | ||
|
||
public static let keys = CodingKeys.self | ||
|
||
public static let schema = defineSchema { embedded in | ||
let category = Category.keys | ||
embedded.fields(.field(category.name, is: .required, ofType: .string), | ||
.field(category.color, is: .required, ofType: .embedded(type: Color.self))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
// swiftlint:disable all | ||
import Amplify | ||
import Foundation | ||
|
||
public struct Color: Embeddable { | ||
var name: String | ||
var red: Int | ||
var green: Int | ||
var blue: Int | ||
} | ||
|
||
extension Color { | ||
public enum CodingKeys: CodingKey { | ||
case name | ||
case red | ||
case green | ||
case blue | ||
} | ||
|
||
public static let keys = CodingKeys.self | ||
|
||
public static let schema = defineSchema { embedded in | ||
let color = Color.keys | ||
embedded.fields(.field(color.name, is: .required, ofType: .string), | ||
.field(color.red, is: .required, ofType: .int), | ||
.field(color.green, is: .required, ofType: .int), | ||
.field(color.blue, is: .required, ofType: .int)) | ||
} | ||
} |
Oops, something went wrong.