~theo/gql

ref: b85b5c7714f66c3b6a9f399b65b081c76756156d gql/t/execution-tests.lisp -rw-r--r-- 15.8 KiB
b85b5c77Theodor Thornhill Make small edits to examples 9 months ago
                                                                                
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
(in-package #:gql-tests)

(deftest execution
  (testing "collect-fields returns the correct fields"
    (with-schema (build-schema "{ a { subfield1 } ...ExampleFragment }
                                   fragment ExampleFragment on Query { a { subfield2 } b }")
      (let ((gql::*all-types* (make-hash-table :test #'equal)))
        ;; HACK: omg, eww!
        (setf (gethash "Query" gql::*all-types*)
              (make-instance 'gql::object-type-definition
                             :name (make-instance 'gql::named-type :name "Query")))
        (let* ((operation (car (gql::definitions gql::*schema*)))
               (operation-type (gql::operation-type operation))
               (selection-set (gql::selection-set operation))
               (result (gql::collect-fields operation-type (gql::selections selection-set) nil nil)))
          (ok (= (hash-table-count result) 2))
          (ok (= (length (gethash "a" result)) 2))
          (ok (= (length (gethash "b" result)) 1)))  )))
  (testing "get-operation should return the correct operation"
    (let ((gql::*schema* (build-schema "{ a { subfield1 } } ")))
      (ok (gql::get-operation gql::*schema* "Query")))
    (let ((gql::*schema* (build-schema "query { a { subfield1 } } ")))
      (ok (gql::get-operation gql::*schema*)))
    (let ((gql::*schema* (build-schema "mutation { a { subfield1 } } ")))
      (ok (signals (gql::get-operation gql::*schema* "Query") 'gql::gql-simple-error)))
    (let ((gql::*schema* (build-schema "mutation { a { subfield1 } }
                                   query { a { subfield1 } } ")))
      (ok (signals (gql::get-operation gql::*schema*) 'gql::gql-simple-error)))
    (let ((gql::*schema* (build-schema "mutation { a { subfield1 } } ")))
      (ok (gql::get-operation gql::*schema*)))
    (let ((gql::*schema* (build-schema "mutation { a { subfield1 } } ")))
      (ok (gql::get-operation gql::*schema* "Mutation"))))
  (testing "merge-selection-sets should merge multiple fields"
    (with-schema (build-schema (asdf:system-relative-pathname 'gql-tests #p"t/test-files/validation-schema.graphql"))
      (let ((*resolvers* (make-hash-table :test #'equal))
            (query-resolver (make-hash-table :test #'equal))
            (dog-resolver (make-hash-table :test #'equal))
            (human-resolver (make-hash-table :test #'equal)))
        (setf (gethash "Query" *resolvers*) query-resolver)
        (setf (gethash "Human" *resolvers*) human-resolver)
        (setf (gethash "name" human-resolver) (lambda (arg) (declare (ignorable arg))
                                                "Bingo-bongo-pappa"))
        (setf (gethash "Dog" *resolvers*) dog-resolver)
        (setf (gethash "dog" query-resolver) (lambda (arg) (declare (ignorable arg)) t))
        (setf (gethash "name" dog-resolver) (lambda (arg) (declare (ignorable arg))
                                              "Bingo-bongo"))
        (setf (gethash "owner" dog-resolver) (lambda (arg) (declare (ignorable arg)) t))
        (let* ((res (gql::execute-request (build-schema "query { dog { name } dog { owner { name } } }") nil (make-hash-table) nil))
               (data (gethash "data" res))
               (dog-res (gethash "dog" data)))
          (ok (typep res 'hash-table))
          (ok (= (hash-table-count res) 2))
          (ok (= (hash-table-count dog-res) 2))
          (ok (gethash "name" dog-res))
          (ok (gethash "owner" dog-res))))))
  (testing "A query should handle alias"
    (with-schema (build-schema (asdf:system-relative-pathname 'gql-tests #p"t/test-files/validation-schema.graphql"))
      (let* ((*resolvers* (make-hash-table :test #'equal))
             (query-resolver (make-hash-table :test #'equal))
             (dog-resolver (make-hash-table :test #'equal))
             (human-resolver (make-hash-table :test #'equal)))
        (setf (gethash "Query" *resolvers*) query-resolver)
        (setf (gethash "Human" *resolvers*) human-resolver)
        (setf (gethash "name" human-resolver) (lambda (arg) (declare (ignorable arg))
                                                "Bingo-bongo-pappa"))
        (setf (gethash "Dog" *resolvers*) dog-resolver)
        (setf (gethash "dog" query-resolver) (lambda (arg) (declare (ignorable arg)) t))
        (setf (gethash "name" dog-resolver) (lambda (arg) (declare (ignorable arg))
                                              "Bingo-bongo"))
        (setf (gethash "owner" dog-resolver) (lambda (arg) (declare (ignorable arg)) t))
        (let* ((res (gql::execute-request (build-schema "query { dog { name owner { name: nameAlias } } }") nil (make-hash-table) nil))
               (data (gethash "data" res))
               (dog-res (gethash "dog" data)))
          (ok (typep res 'hash-table))
          (ok (= (hash-table-count res) 2))
          (ok (= (hash-table-count dog-res) 2))
          (ok (gethash "name" dog-res))
          (ok (gethash "owner" dog-res))))))
  (testing "A query should handle variables and arguments"
    (with-schema (build-schema (asdf:system-relative-pathname 'gql-tests #p"t/test-files/validation-schema.graphql"))
      (let ((variable-values (make-hash-table :test #'equal))
            (query-resolver (make-hash-table :test #'equal))
            (dog-resolver (make-hash-table :test #'equal))
            (*resolvers* (make-hash-table :test #'equal)))
        (setf (gethash "sit" variable-values) "SIT")
        (setf (gethash "Query" *resolvers*) query-resolver)
        (setf (gethash "Dog" *resolvers*) dog-resolver)
        (setf (gethash "dog" query-resolver)
              (lambda (arg) (declare (ignorable arg)) t))
        (setf (gethash "doesKnowCommand" dog-resolver)
              (lambda (arg args) (declare (ignorable arg))
                (if (string= (gethash "dogCommand" args) "SIT")
                    'true 'false)))
        (let* ((res (gql::execute-request
                     (build-schema "query x($sit: String) { dog { doesKnowCommand(dogCommand: $sit) } }")
                     nil
                     variable-values
                     nil))
               (data (gethash "data" res))
               (dog (gethash "dog" data))
               (command (gethash "doesKnowCommand" dog)))
          (ok (string= command "true"))))))
  (testing "Result coercing"
    (flet ((named-type (name)
             (make-instance 'gql::named-type
                            :name (make-instance 'gql::name :name name))))
      (flet ((test (name value ty &optional return-val)
               (let ((res (gql::coerce-result (named-type name) value)))
                 (and (typep res ty)
                      (equalp res (or return-val value))))))
        (ok (test "Int" 3 'integer))
        (ok (test "Int" -3 'integer))
        (ng (test "String" -3 'integer "Field error for int"))
        (ng (test "Boolean" -3 'integer "Field error for int"))

        (ok (test "Float" -3.9 'double-float))
        (ok (test "Float" 3.9 'double-float))
        (ok (test "Float" 3342.91231236 'double-float))
        (ng (test "Int" 3342.91231236 'double-float))
        (ng (test "String" 3342.91231236 'double-float))
        (ng (test "Boolean" 3342.91231236 'double-float))

        (ok (test "String" "Look at this string!" 'string "Look at this string!"))
        (ok (test "ID" "Look at this string!" 'string))
        (ok (test "Boolean" "Look at this string!" 'string "Field error for string"))
        (ok (test "Int" "Look at this string!" 'string "Field error for string"))
        (ok (test "" "Look at this string!" 'string "Field error for string"))

        (ok (test "Boolean" 'true 'string "true"))
        (ok (test "Boolean" 'false 'string "false")))))
  (testing "Using resolvers that access the object from the 'db'"
    (with-schema (build-schema (asdf:system-relative-pathname 'gql-tests #p"t/test-files/validation-schema.graphql"))
      (let* ((*resolvers* (make-hash-table :test #'equal))
             (query-resolver (make-hash-table :test #'equal))
             (dog-resolver (make-hash-table :test #'equal)))
        (gql::defclass* dog name owner)
        (setf (gethash "Query" *resolvers*) query-resolver)
        (setf (gethash "Dog" *resolvers*) dog-resolver)
        (setf (gethash "dog" query-resolver)
              (lambda (arg) (declare (ignorable arg))
                (make-instance 'dog :name "Bingo-bongo")))
        (setf (gethash "name" dog-resolver) (lambda (dog) (name dog)))
        (let* ((res (gql::execute-request
                     (build-schema "query { dog { name } }") nil (make-hash-table) nil))
               (data (gethash "data" res))
               (dog (gethash "dog" data))
               (name (gethash "name" dog)))
          (ok (string= name "Bingo-bongo")))
        (let* ((res (gql::execute-request
                     (build-schema "query { dog { name: bongo } }") nil (make-hash-table) nil))
               (data (gethash "data" res))
               (dog (gethash "dog" data))
               (name (gethash "bongo" dog)))
          (ok (string= name "Bingo-bongo"))))))
  (testing "A query should handle variables and arguments"
    (with-schema (build-schema (asdf:system-relative-pathname 'gql-tests #p"t/test-files/validation-schema.graphql"))
      (let ((variable-values (make-hash-table :test #'equal))
            (query-resolver (make-hash-table :test #'equal))
            (dog-resolver (make-hash-table :test #'equal))
            (*resolvers* (make-hash-table :test #'equal)))
        (gql::defclass* dog name does-know-command)
        (setf (gethash "sit" variable-values) "SIT")
        
        (setf (gethash "Query" *resolvers*) query-resolver)
        (setf (gethash "Dog" *resolvers*) dog-resolver)
        (setf (gethash "dog" query-resolver)
              (lambda (arg) (declare (ignorable arg))
                (make-instance 'dog
                               :name "Bingo-bongo"
                               :does-know-command '("SIT" "DOWN" "HEEL"))))
        (setf (gethash "name" dog-resolver) (lambda (dog) (name dog)))

        (setf (gethash "doesKnowCommand" dog-resolver)
              (lambda (arg args) (declare (ignorable arg))
                (with-slots (does-know-command) arg
                  (if (member (gethash "dogCommand" args) does-know-command
                              :test #'equal)
                      'true 'false))))

        (let* ((res (gql::execute-request
                     (build-schema "query x($sit: String) { dog { doesKnowCommand(dogCommand: $sit) } }")
                     nil
                     variable-values
                     nil))
               (data (gethash "data" res))
               (dog (gethash "dog" data))
               (command (gethash "doesKnowCommand" dog)))
          (ok (string= command "true")))
        (setf (gethash "sit" variable-values) "SITT")
        (let* ((res (gql::execute-request
                     (build-schema "query x($sit: String) { dog { doesKnowCommand(dogCommand: $sit) } }")
                     nil
                     variable-values
                     nil))
               (data (gethash "data" res))
               (dog (gethash "dog" data))
               (command (gethash "doesKnowCommand" dog)))
          (ok (string= command "false")))
        ;; (setf (gethash "sit" variable-values) "SIT")
        ;; (let* ((res (gql::execute-request
        ;;              (build-schema "query { dog { doesKnowCommand(dogCommand: \"SIT\") } }")
        ;;              nil
        ;;              variable-values
        ;;              nil))
        ;;        (data (gethash "data" res))
        ;;        (dog (gethash "dog" data))
        ;;        (command (gethash "doesKnowCommand" dog)))
        ;;   (ok (string= command "true")))
        (let* ((res (gql::execute-request
                     (build-schema "query { dog { doesKnowCommand(dogCommand: \"LOL\") } }")
                     nil
                     variable-values
                     nil))
               (data (gethash "data" res))
               (dog (gethash "dog" data))
               (command (gethash "doesKnowCommand" dog)))
          (ok (string= command "false")))))))

(deftest abstract-type-resolvers
  (testing "Getting object-type-definition from union or interface"
    (defclass pet (gql-object)
      ((name :initarg :name :accessor name)))

    (defclass dog (pet)
      ((owner :initarg :owner :accessor owner)
       (nickname :initarg :nickname :accessor nickname)))

    (let ((doggo (make-instance 'dog :name "Bingo-Bongo" :type-name "Dog")))
      (with-schema (build-schema (asdf:system-relative-pathname 'gql-tests #p"t/test-files/validation-schema.graphql"))
        ;; We want to know if we did get the actual same reference.
        (ok (eq (gql::resolve-abstract-type (gethash "CatOrDog" gql::*all-types*) doggo)
                (gethash "Dog" gql::*all-types*)))
        (ok (eq (gql::resolve-abstract-type (gethash "Pet" gql::*all-types*) doggo)
                (gethash "Dog" gql::*all-types*)))))))

(deftest doggo-test
  (testing "Doggo-testing"
    (defclass pet (gql-object)
      ((name :initarg :name :accessor name)))

    (defclass dog (pet)
      ((owner :initarg :owner :accessor owner)
       (nickname :initarg :nickname :accessor nickname)))

    (defclass cat (pet)
      ((nickname :initarg :nickname :accessor nickname)))

    (defclass sentient (gql-object)
      ((name :initarg :name :accessor name)))

    (defclass human (sentient)
      ((pets :initarg :pets :accessor pets)))

    (let* ((doggo
             (make-instance
              'dog
              :name "Bingo-Bongo"
              :type-name "Dog"
              :nickname "Hund!"
              :owner (make-instance
                      'human
                      :name "Wingle Wangle"
                      :type-name "Human"
                      :pets `(,(make-instance
                                'dog
                                :name "Bingo-Bongo"
                                :nickname "Hund!"
                                :type-name "Dog")
                              ,(make-instance
                                'cat
                                :name "Bango-Wango"
                                :nickname "Mjausig"
                                :type-name "Cat")))))
           (query-resolvers
             (make-resolvers
               ("dog"      . (constantly doggo))))

           (dog-resolvers
             (make-resolvers
               ("name"     . 'name)
               ("nickname" . 'nickname)
               ("owner"    . 'owner)))

           (cat-resolvers
             (make-resolvers
               ("name"     . 'name)
               ("nickname" . 'nickname)
               ("owner"    . 'owner)))

           (human-resolvers
             (make-resolvers
               ("name"     . 'name)
               ("pets"     . 'pets)))

           (*resolvers*
             (make-resolvers
               ("Query"    . query-resolvers)
               ("Dog"      . dog-resolvers)
               ("Cat"      . cat-resolvers)
               ("Human"    . human-resolvers))))

      (flet ((doggo-test (query)
               (with-schema (build-schema (asdf:system-relative-pathname 'gql-tests #p"t/test-files/validation-schema.graphql"))
                 (let* ((res (gql::execute-request (build-schema query) nil (make-hash-table :test #'equal) nil)))
                   (format nil "~a" (cl-json:encode-json-to-string res))))))

        (ok (string=
             (doggo-test "query { dog { name owner { name pets { name nickname } } } }")
             "{\"data\":{\"dog\":{\"name\":\"Bingo-Bongo\",\"owner\":{\"name\":\"Wingle Wangle\",\"pets\":[{\"name\":\"Bingo-Bongo\",\"nickname\":\"Hund!\"},{\"name\":\"Bango-Wango\",\"nickname\":\"Mjausig\"}]}}},\"errors\":null}"))

        (ok (string=
             (doggo-test "query { dog: doggo { name: Bingo owner { name: Wingle pets: dogs { name nickname: thisIsFun } } } }")
             "{\"data\":{\"doggo\":{\"Bingo\":\"Bingo-Bongo\",\"owner\":{\"Wingle\":\"Wingle Wangle\",\"dogs\":[{\"name\":\"Bingo-Bongo\",\"thisIsFun\":\"Hund!\"},{\"name\":\"Bango-Wango\",\"thisIsFun\":\"Mjausig\"}]}}},\"errors\":null}"))))))