-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcustom_context_schema.cr
174 lines (141 loc) · 3.15 KB
/
custom_context_schema.cr
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
170
171
172
173
174
require "json"
class CustomContext < GraphQL::Schema::Context
def initialize(@user : {authenticated: Bool, name: String}, @schema, @max_depth); end
def authenticated
@user[:authenticated]
end
def username
@user[:name]
end
end
class ProcessType
include GraphQL::ObjectType
def initialize(@name : String, @pid : Int32); end
JSON.mapping({name: String, pid: Int32})
field :name
field :pid
end
class LogType
include GraphQL::ObjectType
def initialize(
@time : String, @hostName : String,
@userName : String, @process : ProcessType,
@message : String
); end
JSON.mapping(
time: String,
hostName: String,
userName: String,
process: ProcessType,
message: String
)
field :time
field :userName
field :hostName
field :process
field :message
end
#
# Structs to hold input data
#
struct ProcessInput < GraphQL::Schema::InputType
JSON.mapping(
name: String,
pid: Int32
)
def to_process_type
ProcessType.new(@name, @pid)
end
end
struct LogInput < GraphQL::Schema::InputType
JSON.mapping(
time: String,
hostName: String,
process: ProcessInput,
message: String
)
def to_log_type(username)
LogType.new(@time, @hostName, username, @process.to_process_type, @message)
end
end
module LogStore
extend self
TEMPFILENAME = "./__logs.log"
`touch #{TEMPFILENAME}`
def read_logs
raw_content = File.read(TEMPFILENAME)
Array(LogType).from_json raw_content
rescue
[] of LogType
end
def write_logs(logs)
File.write(TEMPFILENAME, logs.to_json)
end
end
module QueryType
include ::GraphQL::ObjectType
extend self
field :logs do |args, context|
context = context.as(CustomContext)
unless context.authenticated
raise "you are not allowed to read the logs #{context.username}!"
end
LogStore.read_logs
end
end
module MutationType
include ::GraphQL::ObjectType
extend self
field :log do |args, context|
context = context.as(CustomContext)
unless context.authenticated
raise "you are not allowed to read the logs #{context.username}!"
end
new_log = args["log"].as(LogInput).to_log_type(context.username)
LogStore.write_logs LogStore.read_logs << new_log
new_log
end
end
CUSTOM_CONTEXT_SCHEMA = ::GraphQL::Schema.from_schema(
%{
schema {
query: QueryType,
mutation: MutationType
}
type QueryType {
logs: [LogType]
}
type MutationType {
log(log: LogInput) : LogType
}
input LogInput {
time: String!
hostName: String!
process: ProcessInput!
message: String!
}
input ProcessInput {
name: String!
pid: ID
}
type LogType {
time: String!
userName: String!
hostName: String!,
process: ProcessType!
message: String
}
type ProcessType {
name: String!,
pid: ID!
}
}
)
CUSTOM_CONTEXT_SCHEMA.tap do |schema|
schema.query_resolver = QueryType
schema.mutation_resolver = MutationType
# add Types to parse from respective
# Json Input Types
schema.add_input_type("LogInput", LogInput)
schema.add_input_type("ProcessInput", ProcessInput)
end