Skip to content
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
8 changes: 2 additions & 6 deletions Amplify/Categories/DataStore/Query/QueryPredicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,15 @@ public class QueryPredicateGroup: QueryPredicate {
predicates.append(predicate)
return self
}
let group = QueryPredicateGroup(type: .and, predicates: [predicate])
predicates.append(group)
return self
return QueryPredicateGroup(type: .and, predicates: [self, predicate])
}

public func or(_ predicate: QueryPredicate) -> QueryPredicateGroup {
if case .or = type {
predicates.append(predicate)
return self
}
let group = QueryPredicateGroup(type: .or, predicates: [predicate])
predicates.append(group)
return self
return QueryPredicateGroup(type: .or, predicates: [self, predicate])
}

public static func && (lhs: QueryPredicateGroup, rhs: QueryPredicate) -> QueryPredicateGroup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,70 @@ class QueryPredicateGraphQLTests: XCTestCase {
XCTAssertEqual(result, expected)
}

func testPredicateWithNestedAndOperator() throws {
let post = Post.keys
let predicate = (post.title.beginsWith("Title") && post.content.contains("content")) || post.id.eq("id")
let expected = """
{
"or" : [
{
"and" : [
{
"title" : {
"beginsWith" : "Title"
}
},
{
"content" : {
"contains" : "content"
}
}
]
},
{
"id" : {
"eq" : "id"
}
}
]
}
"""
let result = try GraphQLFilterConverter.toJSON(predicate, options: [.prettyPrinted])
XCTAssertEqual(result, expected)
}

func testPredicateWithNestedOrOperator() throws {
let post = Post.keys
let predicate = (post.title.beginsWith("Title") || post.content.contains("content")) && post.id.eq("id")
let expected = """
{
"and" : [
{
"or" : [
{
"title" : {
"beginsWith" : "Title"
}
},
{
"content" : {
"contains" : "content"
}
}
]
},
{
"id" : {
"eq" : "id"
}
}
]
}
"""
let result = try GraphQLFilterConverter.toJSON(predicate, options: [.prettyPrinted])
XCTAssertEqual(result, expected)
}

func testJSONSerializationAndDeserialization() throws {
let post = Post.keys
let predicate = post.id.eq("id") && post.title.beginsWith("Title")
Expand Down