-
Notifications
You must be signed in to change notification settings - Fork 18
/
Graphiti+Fluent.swift
98 lines (88 loc) · 4.92 KB
/
Graphiti+Fluent.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import Graphiti
import Vapor
import Fluent
// Child Relationship
extension Graphiti.Field where Arguments == NoArguments, Context == Request, ObjectType: Model {
/// Creates a GraphQL field for a one-to-many relationship for Fluent
/// - Parameters:
/// - name: Filed name
/// - keyPath: KeyPath to the @Children property
public convenience init<ChildType: Model>(
_ name: FieldKey,
with keyPath: KeyPath<ObjectType, ChildrenProperty<ObjectType, ChildType>>
) where FieldType == [TypeReference<ChildType>] {
self.init(name.description, at: { (type) -> (Request, NoArguments, EventLoopGroup) throws -> EventLoopFuture<[ChildType]> in
return { (context: Request, arguments: NoArguments, eventLoop: EventLoopGroup) in
return type[keyPath: keyPath].query(on: context.db).all() // Get the desired property and make the Fluent database query on it.
}
}, as: [TypeReference<ChildType>].self)
}
}
// Parent Relationship
extension Graphiti.Field where Arguments == NoArguments, Context == Request, ObjectType: Model {
/// Creates a GraphQL field for a one-to-many/one-to-one relationship for Fluent
/// - Parameters:
/// - name: Field name
/// - keyPath: KeyPath to the @Parent property
public convenience init<ParentType: Model>(
_ name: FieldKey,
with keyPath: KeyPath<ObjectType, ParentProperty<ObjectType, ParentType>>
) where FieldType == TypeReference<ParentType> {
self.init(name.description, at: { (type) -> (Request, NoArguments, EventLoopGroup) throws -> EventLoopFuture<ParentType> in
return { (context: Request, arguments: NoArguments, eventLoop: EventLoopGroup) in
return type[keyPath: keyPath].get(on: context.db) // Get the desired property and make the Fluent database query on it.
}
}, as: TypeReference<ParentType>.self)
}
}
// Siblings Relationship
extension Graphiti.Field where Arguments == NoArguments, Context == Request, ObjectType: Model {
/// Creates a GraphQL field for a many-to-many relationship for Fluent
/// - Parameters:
/// - name: Field name
/// - keyPath: KeyPath to the @Siblings property
public convenience init<ToType: Model, ThroughType: Model>(
_ name: FieldKey,
with keyPath: KeyPath<ObjectType, SiblingsProperty<ObjectType, ToType, ThroughType>>
) where FieldType == [TypeReference<ToType>] {
self.init(name.description, at: { (type) -> (Request, NoArguments, EventLoopGroup) throws -> EventLoopFuture<[ToType]> in
return { (context: Request, arguments: NoArguments, eventLoop: EventLoopGroup) in
return type[keyPath: keyPath].query(on: context.db).all() // Get the desired property and make the Fluent database query on it.
}
}, as: [TypeReference<ToType>].self)
}
}
// OptionalParent Relationship
extension Graphiti.Field where Arguments == NoArguments, Context == Request, ObjectType: Model {
/// Creates a GraphQL field for an optional one-to-many/one-to-one relationship for Fluent
/// - Parameters:
/// - name: Field name
/// - keyPath: KeyPath to the @OptionalParent property
public convenience init<ParentType: Model>(
_ name: FieldKey,
with keyPath: KeyPath<ObjectType, OptionalParentProperty<ObjectType, ParentType>>
) where FieldType == TypeReference<ParentType>? {
self.init(name.description, at: { (type) -> (Request, NoArguments, EventLoopGroup) throws -> EventLoopFuture<Optional<ParentType>> in
return { (context: Request, arguments: NoArguments, eventLoop: EventLoopGroup) throws -> EventLoopFuture<Optional<ParentType>> in
return type[keyPath: keyPath].get(on: context.db) // Get the desired property and make the Fluent database query on it.
}
}, as: TypeReference<ParentType>?.self)
}
}
// OptionalChild Relationship
extension Graphiti.Field where Arguments == NoArguments, Context == Request, ObjectType: Model {
/// Creates a GraphQL field for an optional one-to-many/one-to-one relationship for Fluent
/// - Parameters:
/// - name: Field name
/// - keyPath: KeyPath to the @OptionalParent property
public convenience init<ParentType: Model>(
_ name: FieldKey,
with keyPath: KeyPath<ObjectType, OptionalChildProperty<ObjectType, ParentType>>
) where FieldType == TypeReference<ParentType>? {
self.init(name.description, at: { (type) -> (Request, NoArguments, EventLoopGroup) throws -> EventLoopFuture<Optional<ParentType>> in
return { (context: Request, arguments: NoArguments, eventLoop: EventLoopGroup) throws -> EventLoopFuture<Optional<ParentType>> in
return type[keyPath: keyPath].get(on: context.db) // Get the desired property and make the Fluent database query on it.
}
}, as: TypeReference<ParentType>?.self)
}
}