-
Notifications
You must be signed in to change notification settings - Fork 227
fix(Model): retrieve correct associated target name #965
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
Changes from all commits
31cf35f
3b6957b
303fb32
cc1b492
4a96639
373dfe0
a6c457c
f2eee7d
889b1b4
ebc4b3a
2832e1b
7f18c53
d555946
1ed640e
e578d4b
4adc9fa
cd0d263
e5ab45e
169fb4c
a462883
e1b402e
1795c2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,12 +11,35 @@ import Amplify | |
| public typealias GraphQLFilter = [String: Any] | ||
|
|
||
| protocol GraphQLFilterConvertible { | ||
| var graphQLFilter: GraphQLFilter { get } | ||
| func graphQLFilter(for modelSchema: ModelSchema?) -> GraphQLFilter | ||
| } | ||
|
|
||
| // Convert QueryPredicate to GraphQLFilter JSON, and GraphQLFilter JSON to GraphQLFilter | ||
| public struct GraphQLFilterConverter { | ||
|
|
||
| /// Serialize the translated GraphQL query variable object to JSON string. | ||
| /// - Warning: Although this has `public` access, it is intended for internal use and should not be used directly | ||
| /// by host applications. The behavior of this may change without warning. | ||
| public static func toJSON(_ queryPredicate: QueryPredicate, | ||
| modelSchema: ModelSchema, | ||
| options: JSONSerialization.WritingOptions = []) throws -> String { | ||
| let graphQLFilterData = | ||
| try JSONSerialization.data(withJSONObject: queryPredicate.graphQLFilter(for: modelSchema), | ||
| options: options) | ||
|
|
||
| guard let serializedString = String(data: graphQLFilterData, encoding: .utf8) else { | ||
| preconditionFailure(""" | ||
| Could not initialize String from the GraphQL representation of QueryPredicate: | ||
| \(String(describing: graphQLFilterData)) | ||
| """) | ||
| } | ||
|
|
||
| return serializedString | ||
| } | ||
|
|
||
| @available(*, deprecated, message: """ | ||
| Use `toJSON(_:modelSchema:options)` instead. See https://github.com/aws-amplify/amplify-ios/pull/965 for more details. | ||
| """) | ||
| /// Serialize the translated GraphQL query variable object to JSON string. | ||
| public static func toJSON(_ queryPredicate: QueryPredicate, | ||
| options: JSONSerialization.WritingOptions = []) throws -> String { | ||
|
|
@@ -47,11 +70,27 @@ public struct GraphQLFilterConverter { | |
| /// Extension to translate a `QueryPredicate` into a GraphQL query variables object | ||
| extension QueryPredicate { | ||
|
|
||
| /// - Warning: Although this has `public` access, it is intended for internal use and should not be used directly | ||
| /// by host applications. The behavior of this may change without warning. | ||
| public func graphQLFilter(for modelSchema: ModelSchema?) -> GraphQLFilter { | ||
|
Contributor
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. add doc comment for the public API, i see up above perhaps this information can be on top of the method as well. just a suggestion since i recall the discussion regarding adding doc comment and warning, etc. something like |
||
| if let operation = self as? QueryPredicateOperation { | ||
| return operation.graphQLFilter(for: modelSchema) | ||
| } else if let group = self as? QueryPredicateGroup { | ||
| return group.graphQLFilter(for: modelSchema) | ||
| } | ||
|
|
||
| preconditionFailure( | ||
| "Could not find QueryPredicateOperation or QueryPredicateGroup for \(String(describing: self))") | ||
|
Comment on lines
+82
to
+83
Contributor
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. no action needed just a thought exercise.. If developers use
Secondly, precondionFailure affects live apps as well. In one of my latest PR's, I've started using |
||
| } | ||
|
|
||
| @available(*, deprecated, message: """ | ||
| Use `graphQLFilter(for:)` instead. See https://github.com/aws-amplify/amplify-ios/pull/965 for more details. | ||
| """) | ||
| public var graphQLFilter: GraphQLFilter { | ||
lawmicha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if let operation = self as? QueryPredicateOperation { | ||
| return operation.graphQLFilter | ||
| return operation.graphQLFilter(for: nil) | ||
| } else if let group = self as? QueryPredicateGroup { | ||
| return group.graphQLFilter | ||
| return group.graphQLFilter(for: nil) | ||
| } | ||
|
|
||
| preconditionFailure( | ||
|
|
@@ -60,30 +99,52 @@ extension QueryPredicate { | |
| } | ||
|
|
||
| extension QueryPredicateOperation: GraphQLFilterConvertible { | ||
| var graphQLFilter: GraphQLFilter { | ||
| return [field: [self.operator.graphQLOperator: self.operator.value]] | ||
|
|
||
| func graphQLFilter(for modelSchema: ModelSchema?) -> GraphQLFilter { | ||
| let filterValue = [self.operator.graphQLOperator: self.operator.value] | ||
| guard let modelSchema = modelSchema else { | ||
| return [field: filterValue] | ||
| } | ||
| return [columnName(modelSchema): filterValue] | ||
| } | ||
|
|
||
| func columnName(_ modelSchema: ModelSchema) -> String { | ||
| guard let modelField = modelSchema.field(withName: field) else { | ||
| return field | ||
618coffee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| let defaultFieldName = modelSchema.name.camelCased() + field.pascalCased() + "Id" | ||
| switch modelField.association { | ||
| case .belongsTo(_, let targetName): | ||
| return targetName ?? defaultFieldName | ||
| case .hasOne(_, let targetName): | ||
| return targetName ?? defaultFieldName | ||
| default: | ||
| return field | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension QueryPredicateGroup: GraphQLFilterConvertible { | ||
| var graphQLFilter: GraphQLFilter { | ||
|
|
||
| func graphQLFilter(for modelSchema: ModelSchema?) -> GraphQLFilter { | ||
| let logicalOperator = type.rawValue | ||
| switch type { | ||
| case .and, .or: | ||
| var graphQLPredicateOperation = [logicalOperator: [Any]()] | ||
| predicates.forEach { predicate in | ||
| graphQLPredicateOperation[logicalOperator]?.append(predicate.graphQLFilter) | ||
| graphQLPredicateOperation[logicalOperator]?.append(predicate.graphQLFilter(for: modelSchema)) | ||
| } | ||
| return graphQLPredicateOperation | ||
| case .not: | ||
| if let predicate = predicates.first { | ||
| return [logicalOperator: predicate.graphQLFilter] | ||
| return [logicalOperator: predicate.graphQLFilter(for: modelSchema)] | ||
| } else { | ||
| preconditionFailure("Missing predicate for \(String(describing: self)) with type: \(type)") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension QueryOperator { | ||
| var graphQLOperator: String { | ||
| switch self { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.