-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.swift
169 lines (136 loc) · 5.84 KB
/
schema.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// schema.swift
// PioneerChatExample
//
// Created by d-exclaimation on 18:34.
//
import Pioneer
import Graphiti
import Foundation
/// The schema for this server
/// - Returns: The schema instance using Graphiti
func schema() throws -> Schema<Resolver, Context> {
try .init {
// MARK: - Scalar types
ID.asScalar()
// MARK: - Base Object types
Type(Message.self) {
Field("id", at: \.gid)
.description("Message unique identifier")
Field("content", at: \.content)
.description("Message textual content")
Field("createdAt", at: \.createAtIso)
.description("Message creation date and time")
Field("author", at: Message.author, as: User.self)
.description("User who wrote this Message")
Field("room", at: Message.room, as: Room.self)
.description("Room where this Message is sent to")
}
.description("A Message sent to a room by a user")
Type(Room.self) {
Field("id", at: \.gid)
.description("Room unique identifier")
Field("history", at: Room.messages, as: [Message].self)
.description("Message history for this Room sent by any User")
Field("users", at: Room.users, as: [User].self)
.description("Users who have written into this Room")
}
.description("A certain Room / channel of messages")
Type(User.self) {
Field("id", at: \.gid)
.description("User unique identifier")
Field("name", at: \.name)
.description("User public name")
Field("messages", at: User.messages, as: [Message].self)
.description("Message written by this User sent to any Room")
}
.description("A User who can write down messages")
// MARK: - Utility Object types
Type(LoggedUser.self) {
Field("user", at: \.user, as: User.self)
.description("The User for this logged in result")
Field("token", at: \.token)
.description("JWT token for this User")
}
.description("A result with all the information of a Logged in User")
Type(InvalidName.self) {
Field("name", at: \.name)
.description("The name in question")
}
.description("A result given an incorrect name")
Type(Unauthorized.self) {
Field("operation", at: \.operation)
.description("Name of operation being performed")
}
.description("A result given when not logged in")
Type(NewMessage.self) {
Field("message", at: \.message, as: Message.self)
.description("The Message written")
}
.description("A result given a successful write operation")
Type(InvalidRoom.self) {
Field("roomId", at: \.roomId)
.description("The ID in question")
}
.description("A result given when Room id is invalid")
Type(NewRoom.self) {
Field("room", at: \.room, as: Room.self)
.description("The Room opened")
}
.description("A result given a successful open operation")
Input(LoginInfo.self) {
InputField("id", as: ID?.self)
.defaultValue(nil)
.description("Attached ID")
InputField("name", as: String?.self)
.defaultValue(nil)
.description("Attached name")
}
.description("Any login information")
// MARK: Union types
Union(AuthResult.self, members: InvalidName.self, LoggedUser.self)
.description("Results from sign up and log in")
Union(WriteResult.self, members: Unauthorized.self, InvalidRoom.self, NewMessage.self)
.description("Results from writing a message")
Union(OpenResult.self, members: Unauthorized.self, NewRoom.self)
.description("Results from opening a room")
// MARK: Root operations
Query {
Field("me", at: Resolver.me)
.description("Check for the sign in status")
Field("users", at: Resolver.users)
.description("List all signed up Users")
Field("rooms", at: Resolver.rooms)
.description("List all available Rooms")
}
Mutation {
Field("signup", at: Resolver.signup, as: AuthResult.self) {
Argument("name", at: \.name)
.description("Name of the User")
}
.description("Sign up a new User")
Field("login", at: Resolver.login, as: AuthResult.self) {
Argument("info", at: \.info)
.defaultValue(.init())
.description("Any login information")
}
.description("Log into an exisiting User")
Field("write", at: Resolver.write, as: WriteResult.self) {
Argument("to", at: \.to)
.description("The Room id sent the Message to")
Argument("content", at: \.content)
.description("The content of the Message")
}
.description("Write a Message to a Room (must be logged in)")
Field("open", at: Resolver.open, as: OpenResult.self)
.description("Open a Room (must be logged in)")
}
Subscription {
SubscriptionField("listen", as: Message.self, atSub: Resolver.listen) {
Argument("to", at: \.to)
.description("The Room id to listen to")
}
.description("Listen to all Messages sent to a Room (must be logged in)")
}
}
}