M example/example1.lisp +17 -16
@@ 1,33 1,34 @@
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload '(:gql :cl-json :hunchentoot) :silent t))
-(in-package :gql-example)
+(defpackage :gql-example1
+ (:use :cl :gql))
+
+(in-package :gql-example1)
(defvar *example-schema*
(build-schema (asdf:system-relative-pathname 'gql "example/schema.graphql")))
(defvar *variable-values* (make-hash-table :test #'equal))
-;; We make the hash table corresponding to the type in "example/schema.graphql"
-(defparameter *Query* (make-hash-table :test #'equal))
+(hunchentoot:define-easy-handler (home :uri "/home") (item)
+ (setf (hunchentoot:content-type*) "text/plain")
+ (when item
+ (let* ((query-resolvers
+ (make-resolvers
+ ("name" . (constantly "Theodor Thornhill"))
+ ("age" . (constantly 31))))
-;; The functions in here are to be used by the `resolve' internally in gql
-(setf (gethash "name" *Query*) (lambda () "Theodor Thornhill"))
-(setf (gethash "age" *Query*) (lambda () 31))
+ (*resolvers*
+ (make-resolvers
+ ("Query" . query-resolvers))))
-;; Make sure that we actually set the resolver
-(setf *resolvers* (make-hash-table :test #'equal))
-(setf (gethash "Query" *resolvers*) *Query*)
+ (with-schema *example-schema*
+ (let ((result (execute-request (query item) nil *variable-values* nil)))
+ (format nil "~a~%" (cl-json:encode-json-to-string result)))))))
(defvar *server* (make-instance 'hunchentoot:easy-acceptor :port 3000))
(defun query (item)
(build-schema (format nil "query { ~a }" item)))
-(hunchentoot:define-easy-handler (home :uri "/home") (item)
- (setf (hunchentoot:content-type*) "text/plain")
- (when item
- (with-schema *example-schema*
- (let ((result (execute-request (query item) nil *variable-values* nil)))
- (format nil "~a~%" (cl-json:encode-json-to-string result))))))
-
;; Eval this when you want to run the app (hunchentoot:start *server*)
M example/example2.lisp +4 -1
@@ 1,7 1,10 @@
(eval-when (:compile-toplevel :load-toplevel :execute)
(ql:quickload '(:gql :cl-json) :silent t))
-(in-package #:gql-example)
+(defpackage :gql-example2
+ (:use :cl :gql))
+
+(in-package #:gql-example2)
(defclass pet (gql-object)
((name :initarg :name :accessor name)))
R example/package.lisp => +0 -2
@@ 1,2 0,0 @@
-(defpackage :gql-example
- (:use :cl :gql))
M wiki/examples/example1.md +22 -11
@@ 11,10 11,10 @@ start by adding a couple of quickloads,
Then, we need to define our package and go inside of it.
```lisp
-(defpackage :gql-exampleapp
+(defpackage :gql-exampleapp1
(:use :cl :gql))
-(in-package :gql-exampleapp)
+(in-package :gql-exampleapp1)
```
@@ 40,19 40,21 @@ We define it, along with our variable-va
Great, this is a good start! The last item on our agenda is resolving
information. `gql` provides a dynamic variable, `*resolvers*`, which sole
-purpose is to deal with this. We need only a simple one here:
+purpose is to deal with this. We need only simple ones here:
```lisp
-(setf (gethash "name" *Query*) (lambda () "Bongodor"))
-(setf (gethash "age" *Query*) (lambda () 22))
+(make-resolvers
+ ("name" . (constantly "Theodor Thornhill"))
+ ("age" . (constantly 31)))
-(setf *resolvers* (make-hash-table :test #'equal))
-(setf (gethash "Query" *resolvers*) *Query*)
+(make-resolvers
+ ("Query" . query-resolvers))
+ (setf (gethash "Query" *resolvers*) *Query*)
```
The main point here is that we want to mimick the structure from the schema, but
return functions adhering to the contract defined in the schema. In this case
-it is easy, we just supply a lambda that returns a value. These functions are
+it is easy, we just supply a function that returns a value. These functions are
then called internally by `gql`. We could supply arguments and variables here,
and that would make our functions take an argument, a hash-table of
param->value. We don't need that here, so we just supply the functions without
@@ 70,9 72,18 @@ The last few things is running a server
(hunchentoot:define-easy-handler (home :uri "/home") (item)
(setf (hunchentoot:content-type*) "text/plain")
(when item
- (with-schema *example-schema*
- (let ((result (execute-request (query item) nil *variable-values* nil)))
- (format nil "~a~%" (cl-json:encode-json-to-string result))))))
+ (let* ((query-resolvers
+ (make-resolvers
+ ("name" . (constantly "Theodor Thornhill"))
+ ("age" . (constantly 31))))
+
+ (*resolvers*
+ (make-resolvers
+ ("Query" . query-resolvers))))
+
+ (with-schema *example-schema*
+ (let ((result (execute-request (query item) nil *variable-values* nil)))
+ (format nil "~a~%" (cl-json:encode-json-to-string result)))))))
```
Very nice. The last thing is now to eval this line in your repl, from inside of