-
Notifications
You must be signed in to change notification settings - Fork 200
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
GraphQLDocument Builder #309
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Foundation | ||
|
||
/// A Tree data type with a `value` of some type `E` and `children` subtrees. | ||
public class Tree<E> { | ||
public var value: E | ||
public var children: [Tree<E>] = [] | ||
public weak var parent: Tree<E>? | ||
|
||
public init(value: E) { | ||
self.value = value | ||
} | ||
|
||
/// Add a child to the tree's children and set a weak reference from the child to the parent (`self`) | ||
public func addChild(settingParentOf child: Tree) { | ||
children.append(child) | ||
child.parent = self | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ class AnyModelIntegrationTests: XCTestCase { | |
|
||
let callbackInvoked = expectation(description: "Callback invoked") | ||
var responseFromOperation: GraphQLResponse<AnyModel>? | ||
_ = Amplify.API.mutate(ofAnyModel: anyPost, type: .create) { response in | ||
_ = Amplify.API.mutate(of: anyPost, type: .create) { response in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we discussed at some point removing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah yes, i still have those captured in the previous PR: https://github.com/aws-amplify/amplify-ios/blob/7db239ddb6e303fb9dad876033286f0c0ca8979a/Amplify/Categories/API/ClientBehavior/AmplifyAPICategory%2BGraphQLBehavior.swift but would opt to do any of these in another PR. The change you see here is due to removing the mutate(ofAnyModel) API from Amplify.API. this set of test is disabled at the moment so i'm not sure if that's even the right call for the test. datastore uses the GraphQLRequest extensions so |
||
defer { | ||
callbackInvoked.fulfill() | ||
} | ||
|
@@ -101,7 +101,7 @@ class AnyModelIntegrationTests: XCTestCase { | |
let originalAnyPost = try originalPost.eraseToAnyModel() | ||
|
||
let createCallbackInvoked = expectation(description: "Create callback invoked") | ||
_ = Amplify.API.mutate(ofAnyModel: originalAnyPost, type: .create) { _ in | ||
_ = Amplify.API.mutate(of: originalAnyPost, type: .create) { _ in | ||
createCallbackInvoked.fulfill() | ||
} | ||
|
||
|
@@ -115,7 +115,7 @@ class AnyModelIntegrationTests: XCTestCase { | |
|
||
let updateCallbackInvoked = expectation(description: "Update callback invoked") | ||
var responseFromOperation: GraphQLResponse<AnyModel>? | ||
_ = Amplify.API.mutate(ofAnyModel: updatedAnyPost, type: .update) { response in | ||
_ = Amplify.API.mutate(of: updatedAnyPost, type: .update) { response in | ||
defer { | ||
updateCallbackInvoked.fulfill() | ||
} | ||
|
@@ -164,7 +164,7 @@ class AnyModelIntegrationTests: XCTestCase { | |
let originalAnyPost = try originalPost.eraseToAnyModel() | ||
|
||
let createCallbackInvoked = expectation(description: "Create callback invoked") | ||
_ = Amplify.API.mutate(ofAnyModel: originalAnyPost, type: .create) { _ in | ||
_ = Amplify.API.mutate(of: originalAnyPost, type: .create) { _ in | ||
createCallbackInvoked.fulfill() | ||
} | ||
|
||
|
@@ -174,7 +174,7 @@ class AnyModelIntegrationTests: XCTestCase { | |
|
||
let deleteCallbackInvoked = expectation(description: "Delete callback invoked") | ||
var responseFromOperation: GraphQLResponse<AnyModel>? | ||
_ = Amplify.API.mutate(ofAnyModel: originalAnyPost, type: .delete) { response in | ||
_ = Amplify.API.mutate(of: originalAnyPost, type: .delete) { response in | ||
defer { | ||
deleteCallbackInvoked.fulfill() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// Copyright 2018-2020 Amazon.com, | ||
// Inc. or its affiliates. All Rights Reserved. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
import Amplify | ||
import Foundation | ||
|
||
/// Adds conflict resolution information onto the document based on the operation type (query or mutation) | ||
/// All selection sets are decorated with conflict resolution fields and inputs are added based on the values that it | ||
/// was instantiated with. If `version` is passed, the input with key "input" will contain "_version" with the `version` | ||
/// value. If `lastSync` is passed, the input will contain new key "lastSync" with the `lastSync` value. | ||
public struct ConflictResolutionDecorator: ModelBasedGraphQLDocumentDecorator { | ||
|
||
private let version: Int? | ||
private let lastSync: Int? | ||
|
||
public init(version: Int? = nil, lastSync: Int? = nil) { | ||
self.version = version | ||
self.lastSync = lastSync | ||
} | ||
|
||
public func decorate(_ document: SingleDirectiveGraphQLDocument, | ||
modelType: Model.Type) -> SingleDirectiveGraphQLDocument { | ||
var inputs = document.inputs | ||
|
||
if let version = version, | ||
case .mutation = document.operationType, | ||
var input = inputs["input"], | ||
case var .object(value) = input.value { | ||
|
||
value["_version"] = version | ||
input.value = .object(value) | ||
inputs["input"] = input | ||
} | ||
|
||
if let lastSync = lastSync, case .query = document.operationType { | ||
inputs["lastSync"] = GraphQLDocumentInput(type: "AWSTimestamp", value: .scalar(lastSync)) | ||
} | ||
|
||
if let selectionSet = document.selectionSet { | ||
addConflictResolution(selectionSet: selectionSet) | ||
return document.copy(inputs: inputs, selectionSet: selectionSet) | ||
} | ||
|
||
return document.copy(inputs: inputs) | ||
} | ||
|
||
/// Append the correct conflict resolution fields for `model` and `pagination` selection sets. | ||
private func addConflictResolution(selectionSet: SelectionSet) { | ||
switch selectionSet.value.fieldType { | ||
case .value: | ||
break | ||
case .model: | ||
selectionSet.addChild(settingParentOf: .init(value: .init(name: "_version", fieldType: .value))) | ||
selectionSet.addChild(settingParentOf: .init(value: .init(name: "_deleted", fieldType: .value))) | ||
selectionSet.addChild(settingParentOf: .init(value: .init(name: "_lastChangedAt", fieldType: .value))) | ||
case .pagination: | ||
selectionSet.addChild(settingParentOf: .init(value: .init(name: "startedAt", fieldType: .value))) | ||
} | ||
|
||
selectionSet.children.forEach { child in | ||
addConflictResolution(selectionSet: child) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for remembering to make this
weak