Skip to content

Commit 73485f3

Browse files
committed
Add support for fluent OptionalChild property wrappers
1 parent c0cc82e commit 73485f3

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed

Package.resolved

Lines changed: 40 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ let package = Package(
1515
dependencies: [
1616
.package(url: "https://github.com/GraphQLSwift/Graphiti.git", from: "0.24.0"),
1717
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
18-
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),
18+
.package(url: "https://github.com/vapor/fluent.git", from: "4.2.0"),
1919
],
2020
targets: [
2121
.target(name: "GraphQLKit",

Sources/GraphQLKit/Graphiti+Fluent.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,22 @@ extension Graphiti.Field where Arguments == NoArguments, Context == Request, Obj
7777
}, as: TypeReference<ParentType>?.self)
7878
}
7979
}
80+
81+
// OptionalChild Relationship
82+
extension Graphiti.Field where Arguments == NoArguments, Context == Request, ObjectType: Model {
83+
84+
/// Creates a GraphQL field for an optional one-to-many/one-to-one relationship for Fluent
85+
/// - Parameters:
86+
/// - name: Field name
87+
/// - keyPath: KeyPath to the @OptionalParent property
88+
public convenience init<ParentType: Model>(
89+
_ name: FieldKey,
90+
with keyPath: KeyPath<ObjectType, OptionalChildProperty<ObjectType, ParentType>>
91+
) where FieldType == TypeReference<ParentType>? {
92+
self.init(name.description, at: { (type) -> (Request, NoArguments, EventLoopGroup) throws -> EventLoopFuture<Optional<ParentType>> in
93+
return { (context: Request, arguments: NoArguments, eventLoop: EventLoopGroup) throws -> EventLoopFuture<Optional<ParentType>> in
94+
return type[keyPath: keyPath].get(on: context.db) // Get the desired property and make the Fluent database query on it.
95+
}
96+
}, as: TypeReference<ParentType>?.self)
97+
}
98+
}

0 commit comments

Comments
 (0)