From 17ec30cc63a805fed87742ec3f5824889a55aeb6 Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Tue, 28 Dec 2021 23:24:02 +0530 Subject: kolam: Support field alias. * kolam/graphql.scm (tree->root+alias+args+children): New function. (eval-graphql): Call tree->root+alias+args+children and use the returned alias in the response. --- kolam/graphql.scm | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/kolam/graphql.scm b/kolam/graphql.scm index 676caa4..c8cc706 100644 --- a/kolam/graphql.scm +++ b/kolam/graphql.scm @@ -139,12 +139,19 @@ (eval-graphql tree (schema-query schema))) (operation (error "Invalid GraphQL operation:" operation)))) +(define (tree->root+alias+args+children tree) + (let ((root children (match tree + ((root children ...) (values root children)) + (leaf (values leaf #f))))) + (match root + (#((? symbol? root) (? symbol? alias) args ...) + (values root alias args children)) + (#((? symbol? root) args ...) + (values root #f args children)) + (root (values root #f '() children))))) + (define* (eval-graphql tree parent-type #:optional parent) - (let* ((root args children (match tree - ((#(root args ...) children ...) (values root args children)) - ((root children ...) (values root '() children)) - (#(leaf args ...) (values leaf args #f)) - (leaf (values leaf '() #f)))) + (let* ((root alias args children (tree->root+alias+args+children tree)) ;; TODO: Check if required args are present. (root-field (or (find-field parent-type root) (error "Unknown field:" root))) @@ -161,7 +168,7 @@ (not (or (scalar-type? underlying-type) (enum-type? underlying-type)))) (error "Leaf node must be scalar or enum type:" root-field underlying-type)) - (cons root + (cons (or alias root) (cond ((eq? next-parent 'null) 'null) ;; List of non-object types -- cgit v1.2.3