ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
graphql.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file graphql.h
6 * @brief Zero-heap GraphQL executor over a query subset (PROTOCORE_ENABLE_GRAPHQL).
7 *
8 * **The governing standard is not IETF.** GraphQL is specified by the GraphQL Foundation and
9 * published at spec.graphql.org, released by date. Every section cited in this module is the
10 * **October 2021** release. There is no RFC for GraphQL.
11 *
12 * The document source text is parsed into fixed pools (no heap) by the sec 2 grammar, the operation
13 * is executed by the sec 6 algorithms, and the result is serialized as the sec 7.1 response map in
14 * the sec 7.2.1 JSON form. The client picks the shape: sec 2.4 says an operation "selects the set of
15 * information it needs, and will receive exactly that information and nothing more".
16 *
17 * **Schema-free model.** There is no type system (sec 3). A Field carrying a SelectionSet
18 * (`obj { a b }`, sec 2.5) is completed by executing that selection set; a Field with none is a
19 * scalar leaf, completed by calling the resolver (ResolveFieldValue, sec 6.4.2). Arguments met along
20 * the path (sec 2.6) stay in scope, so a resolver for `sensor.value` reads `id` from
21 * `sensor(id: 2) { value }`. The application implements one function: the value of the scalar at
22 * this dotted path, given the arguments in scope.
23 *
24 * Supported: one operation, either the sec 2.3 query shorthand (`{...}`) or `query [Name] {...}`;
25 * nested selection sets (sec 2.4). Field arguments (sec 2.6) taking Int, Float, String, Boolean and
26 * Null values (sec 2.9.1 to sec 2.9.5); comments (sec 2.1.4) and insignificant commas (sec 2.1.5).
27 * Out of scope: mutations and subscriptions (sec 2.3), fragments (sec 2.8), variables (sec 2.10),
28 * directives (sec 2.12), aliases (sec 2.7), list values (sec 2.9.7), input objects (sec 2.9.8),
29 * block strings and `\uXXXX` escapes (sec 2.9.4), and lists of objects (sec 3.11). Each parses as a
30 * request error.
31 *
32 * Two deviations from the released spec, both stated rather than hidden: Int is carried as a 64-bit
33 * value where sec 3.5.1 defines a signed 32-bit scalar, and a leaf that fails to resolve completes
34 * as null with no `errors` entry, where sec 7.1.2 says a field error should be listed.
35 *
36 * A malformed document raises a request error (sec 7.1.2): execution does not begin and the response
37 * map carries `errors` and no `data`.
38 *
39 * Bounds are compile-time (PROTOCORE_GQL_*); parsing and execution allocate nothing.
40 *
41 * The module exports one symbol, @ref GraphQL. Everything in graphql.c has internal linkage.
42 *
43 * @author Douglas Quigg (dstroy0)
44 * @date 2026
45 */
46
47#ifndef PROTOCORE_GRAPHQL_H
48#define PROTOCORE_GRAPHQL_H
49
50#include "protocore_config.h" // the entry point: protocore_types.h for the widths
51
52#if PROTOCORE_ENABLE_GRAPHQL
53
54EMBED_BEGIN_DECLS
55
56/** @brief The scalar kinds a resolved leaf or an argument carries (GraphQL spec sec 3.5). */
57EMBED_ENUM_BYTE(protocore_gql_type,
58 PROTOCORE_GQL_NULL = 0, ///< the Null value (sec 2.9.5).
59 PROTOCORE_GQL_INT, ///< Int (sec 3.5.1), read from @c i.
60 PROTOCORE_GQL_FLOAT, ///< Float (sec 3.5.2), read from @c f.
61 PROTOCORE_GQL_BOOL, ///< Boolean (sec 3.5.4), read from @c b.
62 PROTOCORE_GQL_STR, ///< String (sec 3.5.3), read from @c s, NUL-terminated and stable for the call.
63);
64
65/** @brief One scalar: a resolved field value, or an argument's Value (spec sec 2.9). */
66typedef struct
67{
68 protocore_gql_type type; ///< which member below holds the value.
69 long long i; ///< the Int.
70 double f; ///< the Float.
71 embed_bool b; ///< the Boolean.
72 const char *s; ///< the String.
73} protocore_gql_value;
74
75/** @brief The argument values in scope at a resolved field (spec sec 6.4.1 coercedValues). */
76struct protocore_gql_args;
77
78/**
79 * @brief ResolveFieldValue (spec sec 6.4.2): the scalar at dotted @p path, e.g. "device.uptime".
80 *
81 * Fills @p out and returns true, or returns false to complete the field as null (sec 6.4.3).
82 * @p args names the argument values in scope, read back through ::GraphQLNs::arg_int,
83 * ::GraphQLNs::arg_str and ::GraphQLNs::arg_bool.
84 */
85typedef embed_bool (*protocore_gql_resolver_fn)(const char *path, const struct protocore_gql_args *args,
86 protocore_gql_value *out);
87
88/** @brief What one execute reports. */
89EMBED_ENUM_BYTE(protocore_gql_result,
90 PROTOCORE_GQL_OK = 0, ///< Executed; the response map holds `data` (spec sec 7.1.1).
91 PROTOCORE_GQL_ERR_PARSE = -1, ///< Request error (sec 7.1.2): the document does not parse.
92 PROTOCORE_GQL_ERR_LIMIT = -2, ///< Request error (sec 7.1.2): a PROTOCORE_GQL_* bound was exceeded.
93 PROTOCORE_GQL_ERR_OVERFLOW = -3 ///< The serialized response did not fit the buffer.
94);
95
96/** @brief ExecuteRequest (spec sec 6.1): the document to run and the resolver its leaves call. */
97typedef struct
98{
99 const char *document; ///< the ExecutableDocument source text (sec 2.2)
100 size_t len; ///< how many octets of it there are
101 protocore_gql_resolver_fn resolver; ///< ResolveFieldValue (sec 6.4.2); NULL completes every leaf as null
102} GraphQLRequestArgs;
103
104/** @brief Where the response map is serialized (spec sec 7.1, in the sec 7.2.1 JSON form). */
105typedef struct
106{
107 char *out; ///< the buffer the response is written into
108 size_t cap; ///< how much room it has, the NUL included
109} GraphQLResponseArgs;
110
111/** @brief One Argument read by name out of the values in scope (spec sec 2.6). */
112typedef struct
113{
114 const struct protocore_gql_args *values; ///< the argument values a resolver was handed (sec 6.4.1)
115 const char *name; ///< the argument's Name, matched case-sensitively (sec 2.1.9)
116} GraphQLArgumentArgs;
117
118/**
119 * @brief The GraphQL executor (GraphQL spec, October 2021 release; not an IETF standard).
120 *
121 * A caller sets the members a call takes, invokes it through ::GraphQL, and reads the outcome off
122 * the same handle. A resolver running inside an execute sets @c argument and reads @c ok with
123 * @c i64, @c text or @c b.
124 *
125 * No slot member: one document executes at a time, so no call names a row.
126 *
127 * @var GraphQLNs::request the document an execute runs and the resolver its leaves call (sec 6.1)
128 * @var GraphQLNs::response where an execute serializes the response map (sec 7.1)
129 * @var GraphQLNs::argument the argument an accessor reads, and the values it reads from (sec 2.6)
130 * @var GraphQLNs::ok a call's true/false outcome: an execute succeeded, or an argument was
131 * present with the type the accessor asked for
132 * @var GraphQLNs::n octets an execute wrote to @c response.out, excluding the NUL, 0 if
133 * nothing was emitted
134 * @var GraphQLNs::result an execute's outcome code
135 * @var GraphQLNs::i64 the Int an arg_int read (sec 3.5.1), 0 when @c ok is false
136 * @var GraphQLNs::text the String an arg_str read (sec 3.5.3), NULL when @c ok is false
137 * @var GraphQLNs::b the Boolean an arg_bool read (sec 3.5.4), false when @c ok is false
138 * @var GraphQLNs::execute parse @c request, execute its operation, and serialize the response map
139 * into @c response (sec 6.1)
140 * @var GraphQLNs::arg_int read @c argument as an Int
141 * @var GraphQLNs::arg_str read @c argument as a String
142 * @var GraphQLNs::arg_bool read @c argument as a Boolean
143 */
144typedef struct
145{
146 GraphQLRequestArgs request; ///< what an execute runs
147 GraphQLResponseArgs response; ///< where its response lands
148 GraphQLArgumentArgs argument; ///< what an accessor reads
149 embed_bool ok;
150 size_t n;
151 protocore_gql_result result;
152 long long i64;
153 const char *text;
154 embed_bool b;
155} GraphQLVars;
156
157/** @brief The operands and the outcome. */
158extern GraphQLVars GraphQLV;
159
160/** @brief The entries. */
161typedef struct
162{
163 void (*const execute)(uint8_t *work);
164 void (*const arg_int)(uint8_t *work);
165 void (*const arg_str)(uint8_t *work);
166 void (*const arg_bool)(uint8_t *work);
167} GraphQLNs;
168
169// What the table binds, defined once in the .c and taking one parameter each: everything
170// else an entry needs is an operand in GraphQLV or a region of the borrow at a fixed offset.
171void protocore_graph_ql_execute(uint8_t *work);
172void protocore_graph_ql_arg_int(uint8_t *work);
173void protocore_graph_ql_arg_str(uint8_t *work);
174void protocore_graph_ql_arg_bool(uint8_t *work);
175
176// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
177// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
178// `GraphQL.execute(work)` resolves to a named function and becomes a DIRECT call. An extern table
179// leaves the call indirect and the symbol live at every level, -O2 -flto included.
180static const GraphQLNs GraphQL __attribute__((unused)) = {
181 .execute = protocore_graph_ql_execute,
182 .arg_int = protocore_graph_ql_arg_int,
183 .arg_str = protocore_graph_ql_arg_str,
184 .arg_bool = protocore_graph_ql_arg_bool,
185};
186
187/**
188 * @brief The PROTOCORE_GRAPHQL_BORROW bytes this module's state lives in.
189 *
190 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
191 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
192 * walks, so the state lasts the life of the program.
193 *
194 * @return the span.
195 */
196uint8_t *protocore_graphql_span(void);
197
198EMBED_END_DECLS
199
200#endif // PROTOCORE_ENABLE_GRAPHQL
201
202#endif // PROTOCORE_GRAPHQL_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.