From d35ea043dc220456afbea0e489a4d442130d25e1 Mon Sep 17 00:00:00 2001 From: jangko Date: Mon, 12 Apr 2021 15:22:40 +0700 Subject: [PATCH] api changes related to server implementation --- graphql.nim | 8 +++++--- graphql/api.nim | 20 +++++++++----------- graphql/executor.nim | 18 +++++++++++++----- graphql/graphql.nim | 1 + 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/graphql.nim b/graphql.nim index 2fd3bec..bcee013 100644 --- a/graphql.nim +++ b/graphql.nim @@ -9,15 +9,17 @@ import faststreams/inputs, - graphql/[server, client, parser, api], + graphql/[parser, api], graphql/builtin/json_respstream as jrs -export server, client, parser - export # faststreams inputs inputs, + # query_parser, schema_parser + # and full_parser + parser, + # ast helper types api.ast_helper, diff --git a/graphql/api.nim b/graphql/api.nim index 9e5f0ab..7d91164 100644 --- a/graphql/api.nim +++ b/graphql/api.nim @@ -20,9 +20,6 @@ export validator, graphql, ast_helper, executor, response, results, types, names, errors, ast -type - ParseResult* = Result[void, ErrorDesc] - const builtinSchema = staticRead("builtin" / "schema.ql") @@ -50,9 +47,10 @@ proc customScalar*(ctx: GraphqlRef, nameStr: string, scalarProc: ScalarProc) = parseLit: scalarProc ) -proc customScalars*(ctx: GraphqlRef, procs: openArray[(string, ScalarProc)]) = +proc customScalars*(ctx: GraphqlRef, + procs: openArray[tuple[name: string, scalarProc: ScalarProc]]) = for c in procs: - ctx.customScalar(c[0], c[1]) + ctx.customScalar(c.name, c.scalarProc) proc addVar*(ctx: GraphqlRef, name: string, val: int) = let name = ctx.names.insert(name) @@ -103,19 +101,19 @@ proc parseVar*(ctx: GraphqlRef, name: string, value: openArray[byte]): ParseResult = parseVariableImpl(name, value) -proc addResolvers*(ctx: GraphqlRef, ud: RootRef, - typeName: Name, resolvers: openArray[(string, ResolverProc)]) = +proc addResolvers*(ctx: GraphqlRef, ud: RootRef, typeName: Name, + resolvers: openArray[tuple[name: string, resolver: ResolverProc]]) = var res = ctx.resolver.getOrDefault(typeName) if res.isNil: res = ResolverRef(ud: ud) ctx.resolver[typeName] = res for v in resolvers: - let field = ctx.names.insert(v[0]) - res.resolvers[field] = v[1] + let field = ctx.names.insert(v.name) + res.resolvers[field] = v.resolver -proc addResolvers*(ctx: GraphqlRef, ud: RootRef, - typeName: string, resolvers: openArray[(string, ResolverProc)]) = +proc addResolvers*(ctx: GraphqlRef, ud: RootRef, typeName: string, + resolvers: openArray[tuple[name: string, resolver: ResolverProc]]) = let name = ctx.names.insert(typeName) ctx.addResolvers(ud, name, resolvers) diff --git a/graphql/executor.nim b/graphql/executor.nim index 2cea1d6..0a0c012 100644 --- a/graphql/executor.nim +++ b/graphql/executor.nim @@ -151,16 +151,24 @@ proc executeQuery(ctx: GraphqlRef, exec: ExecRef, resp: RespStream) = serialize(res, resp) proc executeMutation(ctx: GraphqlRef, exec: ExecRef, resp: RespStream) = - discard + res := executeSelectionSet(exec.fieldSet, exec.opType, exec.opType, nil) + serialize(res, resp) proc executeSubscription(ctx: GraphqlRef, exec: ExecRef, resp: RespStream) = discard -proc executeRequest*(ctx: GraphqlRef, resp: RespStream, opName = "") = +proc executeRequestImpl(ctx: GraphqlRef, resp: RespStream, opName = "") = exec := getOperation(opName) case exec.opSym.sym.kind - of skQuery: visit executeQuery(exec, resp) - of skMutation: visit executeMutation(exec, resp) - of skSubscription: visit executeSubscription(exec, resp) + of skQuery: visit executeQuery(exec, resp) + of skMutation: visit executeMutation(exec, resp) + of skSubscription: visit executeSubscription(exec, resp) else: unreachable() + +proc executeRequest*(ctx: GraphqlRef, resp: RespStream, opName = ""): ParseResult = + ctx.executeRequestImpl(resp, opName) + if ctx.errKind == ErrNone: + ok() + else: + err(ctx.err) diff --git a/graphql/graphql.nim b/graphql/graphql.nim index 5d8077a..1c19575 100644 --- a/graphql/graphql.nim +++ b/graphql/graphql.nim @@ -75,6 +75,7 @@ type opSym* : Node fieldSet* : FieldSet + ParseResult* = Result[void, ErrorDesc] RespResult* = Result[Node, string] ResolverProc* = proc(ud: RootRef, params: Args,