Skip to content

Commit 92cb0ec

Browse files
committed
fix(pipeline): key GraphQL Routes by operation name, not operation text (DeusData#598)
When a call resolved to a GraphQL client, emit_graphql_edge named the Route after the whole first string argument, stripping only a leading "query " or "mutation " (the mutation strip was also one byte short, so names kept a leading space). An anonymous `query { ... }` therefore became one long Route, and even a named operation carried its variables and selection set in the name ("GetUser($id: ID!) { user(id: $id) { name } }"). Every distinct operation text minted its own Route node, and names were cut off at 256 bytes. Add cbm_service_pattern_graphql_operation(), a small GraphQL document scanner. It skips comments, strings, block strings, `${...}` template interpolations and leading fragment definitions, and reports the first operation's type and name without copying any operation text. Routes are now keyed as follows: named operation __graphql__<Name> name <Name> anonymous query / mutation / __graphql__<type>__anonymous name (anonymous <type>) subscription, incl. the `{ ... }` query shorthand no GraphQL document __graphql__operation__anonymous name (anonymous operation) (URL, bare word, no string argument) Keys are deterministic and bounded by the GraphQL Name grammar. The GRAPHQL_CALLS edge keeps "operation" (now the Route name) and gains "operation_type". The old fallback to the callee name ("client.query") is gone: a callee is not an operation. The sequential path (pass_calls.c) emits no GRAPHQL_CALLS at all, so it had no copy of this logic to fix. Proof on graphql-python/gql (374a70df, Python GraphQL client, index-only): GraphQL Routes 170 -> 16, longest QN 266 -> 34 bytes, 106 -> 0 Routes carrying operation text. GRAPHQL_CALLS 2643 -> 1158 with the same 984 distinct callers: edges are one per (caller, operation) pair, so calls that used to hit distinct text-keyed Routes now share their operation's Route. Tests (edge_types_probe): graphql_route_keyed_by_operation_name_issue598 asserts the exact GraphQL Route set (QN + name) on the parallel path. graphql_operation_identity_parser_issue598 covers braces inside comments, strings and interpolations, fragments before the operation, anonymous operations with variables, name truncation and non-document arguments. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 64c23fa commit 92cb0ec

4 files changed

Lines changed: 308 additions & 25 deletions

File tree

‎internal/cbm/service_patterns.c‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,139 @@ bool cbm_service_pattern_is_http_route_literal(const char *literal, const char *
865865
return true;
866866
}
867867

868+
/* ── GraphQL operation identity (#598) ─────────────────────────── */
869+
870+
static bool gql_name_start(unsigned char c) {
871+
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
872+
}
873+
874+
static bool gql_name_char(unsigned char c) {
875+
return gql_name_start(c) || (c >= '0' && c <= '9');
876+
}
877+
878+
/* Skip one lexical unit whose braces are not structure: a # comment, a "..."
879+
* or """...""" string, or a JS ${...} template interpolation. Returns p
880+
* unchanged when p starts none of them. */
881+
static const char *gql_skip_opaque(const char *p) {
882+
if (*p == '#') {
883+
while (*p && *p != '\n' && *p != '\r') {
884+
p++;
885+
}
886+
return p;
887+
}
888+
if (strncmp(p, "\"\"\"", 3) == 0) {
889+
const char *end = strstr(p + 3, "\"\"\"");
890+
return end ? end + 3 : p + strlen(p);
891+
}
892+
if (*p == '"') {
893+
p++;
894+
while (*p && *p != '"') {
895+
p += (*p == '\\' && p[1]) ? 2 : 1;
896+
}
897+
return *p ? p + 1 : p;
898+
}
899+
if (p[0] == '$' && p[1] == '{') {
900+
int depth = 1;
901+
p += 2;
902+
while (*p && depth > 0) {
903+
depth += (*p == '{') - (*p == '}');
904+
p++;
905+
}
906+
return p;
907+
}
908+
return p;
909+
}
910+
911+
/* Skip GraphQL "ignored tokens" (whitespace, commas, comments). */
912+
static const char *gql_skip_ignored(const char *p) {
913+
for (;;) {
914+
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r' || *p == ',') {
915+
p++;
916+
}
917+
if (*p != '#') {
918+
return p;
919+
}
920+
p = gql_skip_opaque(p);
921+
}
922+
}
923+
924+
/* Copy the Name token at p (bounded). Returns false when p is not a Name. */
925+
static bool gql_copy_name(const char *p, char *name_buf, size_t name_sz) {
926+
if (!gql_name_start((unsigned char)*p) || !name_buf || name_sz == 0) {
927+
return false;
928+
}
929+
size_t n = 0;
930+
while (gql_name_char((unsigned char)p[n]) && n + 1 < name_sz) {
931+
name_buf[n] = p[n];
932+
n++;
933+
}
934+
name_buf[n] = '\0';
935+
return true;
936+
}
937+
938+
static const char *gql_operation_keyword(const char *tok, size_t len) {
939+
static const char *const kws[] = {"query", "mutation", "subscription", NULL};
940+
for (int i = 0; kws[i]; i++) {
941+
if (strlen(kws[i]) == len && strncmp(tok, kws[i], len) == 0) {
942+
return kws[i];
943+
}
944+
}
945+
return NULL;
946+
}
947+
948+
bool cbm_service_pattern_graphql_operation(const char *doc, const char **op_type, char *name_buf,
949+
size_t name_sz) {
950+
*op_type = "operation";
951+
if (name_buf && name_sz > 0) {
952+
name_buf[0] = '\0';
953+
}
954+
if (!doc) {
955+
return false;
956+
}
957+
int depth = 0;
958+
bool in_fragment = false;
959+
const char *p = doc;
960+
while (*p) {
961+
const char *q = gql_skip_opaque(p);
962+
if (q != p) {
963+
p = q;
964+
continue;
965+
}
966+
unsigned char c = (unsigned char)*p;
967+
if (c == '{' && depth == 0 && !in_fragment) {
968+
*op_type = "query"; /* `{ ... }` shorthand is an anonymous query */
969+
return false;
970+
}
971+
if (c == '{' || c == '}') {
972+
depth += (c == '{') ? 1 : -1;
973+
depth = depth < 0 ? 0 : depth;
974+
in_fragment = in_fragment && depth > 0;
975+
p++;
976+
continue;
977+
}
978+
if (depth > 0 || in_fragment || !gql_name_start(c)) {
979+
p++;
980+
continue;
981+
}
982+
const char *tok = p;
983+
while (gql_name_char((unsigned char)*p)) {
984+
p++;
985+
}
986+
size_t len = (size_t)(p - tok);
987+
if (len == strlen("fragment") && strncmp(tok, "fragment", len) == 0) {
988+
in_fragment = true;
989+
continue;
990+
}
991+
const char *kw = gql_operation_keyword(tok, len);
992+
if (!kw) {
993+
return false; /* not a GraphQL document (URL, dict key, variable ...) */
994+
}
995+
*op_type = kw;
996+
return gql_copy_name(gql_skip_ignored(p), name_buf, name_sz);
997+
}
998+
return false;
999+
}
1000+
8681001
/* ── Public API ────────────────────────────────────────────────── */
8691002

8701003
/* Per-worker TLS cache of cbm_service_pattern_match results.

‎internal/cbm/service_patterns.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define CBM_SERVICE_PATTERNS_H
1313

1414
#include <stdbool.h>
15+
#include <stddef.h>
1516

1617
/* Edge type returned by pattern match. */
1718
typedef enum {
@@ -77,4 +78,17 @@ const char *cbm_service_pattern_route_method(const char *callee_name);
7778
* Returns NULL if not an async pattern. */
7879
const char *cbm_service_pattern_broker(const char *resolved_qn);
7980

81+
/* GraphQL operation identity of a client call argument (#598).
82+
* Scans `doc` as a GraphQL document (skipping comments, strings, `${...}`
83+
* template interpolations and leading fragment definitions) and reports the
84+
* first operation: *op_type is "query", "mutation" or "subscription" ("query"
85+
* for the `{ ... }` shorthand). Returns true and writes the operation name to
86+
* name_buf when the operation is named. Returns false for an anonymous
87+
* operation, or with *op_type = "operation" when `doc` is not a GraphQL
88+
* document (NULL, a URL, a bare word such as a dict key ...). The operation
89+
* text itself is never copied, so callers can key Routes on a bounded
90+
* identity. */
91+
bool cbm_service_pattern_graphql_operation(const char *doc, const char **op_type, char *name_buf,
92+
size_t name_sz);
93+
8094
#endif /* CBM_SERVICE_PATTERNS_H */

‎src/pipeline/pass_parallel.c‎

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,40 +2354,43 @@ static void emit_grpc_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source, cons
23542354
cbm_gbuf_insert_edge(gbuf, source->id, route_id, "GRPC_CALLS", props);
23552355
}
23562356

2357-
/* Emit GRAPHQL_CALLS edge. Extract operation from first string arg if available. */
2357+
/* Emit GRAPHQL_CALLS edge. The Route is keyed by the operation NAME only
2358+
* (#598), never by the operation text:
2359+
* named operation -> __graphql__<Name> name "<Name>"
2360+
* anonymous query/mutation/ -> __graphql__<type>__anonymous name "(anonymous <type>)"
2361+
* subscription (incl. the `{ ... }` query shorthand)
2362+
* no GraphQL document -> __graphql__operation__anonymous name "(anonymous operation)"
2363+
* (URL, variable, missing string argument)
2364+
* Keys are bounded by the GraphQL Name grammar and deterministic. */
23582365
static void emit_graphql_edge(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source, const CBMCall *call,
23592366
const cbm_resolution_t *res) {
2360-
const char *op = call->first_string_arg;
2361-
if (!op || !op[0]) {
2362-
op = call->callee_name;
2363-
}
2364-
/* Try to extract a query/mutation name from the operation string */
2365-
char op_name[CBM_SZ_256];
2366-
snprintf(op_name, sizeof(op_name), "%s", op);
2367-
/* Trim leading whitespace and "query "/"mutation " prefix */
2368-
const char *p = op_name;
2369-
while (*p == ' ' || *p == '\t' || *p == '\n') {
2370-
p++;
2371-
}
2372-
if (strncmp(p, "query ", CBM_SZ_6) == 0) {
2373-
p += CBM_SZ_6;
2374-
} else if (strncmp(p, "mutation ", CBM_SZ_8) == 0) {
2375-
p += CBM_SZ_8;
2376-
}
2367+
const char *op_type = "operation";
2368+
char op_name[CBM_SZ_128];
2369+
bool named = cbm_service_pattern_graphql_operation(call->first_string_arg, &op_type, op_name,
2370+
sizeof(op_name));
23772371

2378-
char route_qn[CBM_SZ_512];
2379-
snprintf(route_qn, sizeof(route_qn), "__graphql__%s", p);
2372+
char route_name[CBM_SZ_256];
2373+
char route_qn[CBM_SZ_256];
2374+
if (named) {
2375+
snprintf(route_name, sizeof(route_name), "%s", op_name);
2376+
snprintf(route_qn, sizeof(route_qn), "__graphql__%s", op_name);
2377+
} else {
2378+
snprintf(route_name, sizeof(route_name), "(anonymous %s)", op_type);
2379+
snprintf(route_qn, sizeof(route_qn), "__graphql__%s__anonymous", op_type);
2380+
}
23802381

2381-
int64_t route_id =
2382-
cbm_gbuf_upsert_node(gbuf, "Route", p, route_qn, "", 0, 0, "{\"source\":\"graphql\"}");
2382+
int64_t route_id = cbm_gbuf_upsert_node(gbuf, "Route", route_name, route_qn, "", 0, 0,
2383+
"{\"source\":\"graphql\"}");
23832384

23842385
char esc_c[CBM_SZ_256];
23852386
char esc_op[CBM_SZ_512];
23862387
cbm_json_escape(esc_c, sizeof(esc_c), call->callee_name);
2387-
cbm_json_escape(esc_op, sizeof(esc_op), p);
2388+
cbm_json_escape(esc_op, sizeof(esc_op), route_name);
23882389
char props[CBM_SZ_1K];
2389-
snprintf(props, sizeof(props), "{\"callee\":\"%s\",\"operation\":\"%s\",\"confidence\":%.2f}",
2390-
esc_c, esc_op, res->confidence);
2390+
snprintf(props, sizeof(props),
2391+
"{\"callee\":\"%s\",\"operation\":\"%s\",\"operation_type\":\"%s\","
2392+
"\"confidence\":%.2f}",
2393+
esc_c, esc_op, op_type, res->confidence);
23912394
cbm_gbuf_insert_edge(gbuf, source->id, route_id, "GRAPHQL_CALLS", props);
23922395
}
23932396

‎tests/test_edge_types_probe.c‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "test_framework.h"
4444
#include "test_helpers.h"
4545
#include "cbm.h"
46+
#include "service_patterns.h"
4647
#include <mcp/mcp.h>
4748
#include <store/store.h>
4849
#include <pipeline/pipeline.h>
@@ -222,6 +223,136 @@ static cbm_store_t *et_index_parallel(EtProj *lp, const EtFile *meaningful, int
222223
return et_index_files(lp, files, n);
223224
}
224225

226+
/* #598: a call that resolves to a GraphQL client used to name its Route after
227+
* the WHOLE operation text (minus a leading "query "/"mutation "), so an
228+
* anonymous `query { ... }` became one long Route and even a named operation
229+
* carried its variables and selection set in the name. GraphQL Routes are now
230+
* keyed by operation name only; anonymous operations share one bounded key per
231+
* operation type. The Route set is asserted exactly (QN + name). Parallel path
232+
* only: GRAPHQL_CALLS is emitted by pass_parallel.c. */
233+
typedef struct {
234+
const char *qn;
235+
const char *name;
236+
} EtGqlRoute;
237+
238+
static int et_graphql_routes_exact(const EtFile *files, int nfiles, const EtGqlRoute *want,
239+
int nwant) {
240+
EtProj lp;
241+
cbm_store_t *store = et_index_parallel(&lp, files, nfiles);
242+
cbm_node_t *nodes = NULL;
243+
int node_count = 0;
244+
int ok = store != NULL;
245+
int got = 0;
246+
if (!store || cbm_store_find_nodes_by_label(store, lp.project, "Route", &nodes, &node_count) !=
247+
CBM_STORE_OK) {
248+
ok = 0;
249+
}
250+
for (int ni = 0; nodes && ni < node_count; ni++) {
251+
const char *qn = nodes[ni].qualified_name ? nodes[ni].qualified_name : "";
252+
const char *name = nodes[ni].name ? nodes[ni].name : "";
253+
if (strncmp(qn, "__graphql__", 11) != 0) {
254+
continue;
255+
}
256+
got++;
257+
int matched = 0;
258+
for (int wi = 0; wi < nwant; wi++) {
259+
if (strcmp(qn, want[wi].qn) == 0 && strcmp(name, want[wi].name) == 0) {
260+
matched = 1;
261+
break;
262+
}
263+
}
264+
/* Operation text (selection sets, variables) must never leak into a key. */
265+
if (!matched || strchr(qn, '{') || strchr(name, '{') || strlen(qn) > 160) {
266+
fprintf(stderr, " [ET-GQL] unexpected Route qn='%s' name='%s'\n", qn, name);
267+
ok = 0;
268+
}
269+
}
270+
if (got != nwant) {
271+
fprintf(stderr, " [ET-GQL] FAIL graphql routes expected=%d actual=%d\n", nwant, got);
272+
ok = 0;
273+
}
274+
int edges = store ? cbm_store_count_edges_by_type(store, lp.project, "GRAPHQL_CALLS") : -1;
275+
if (edges < nwant) {
276+
fprintf(stderr, " [ET-GQL] FAIL GRAPHQL_CALLS=%d expected>=%d\n", edges, nwant);
277+
ok = 0;
278+
}
279+
cbm_store_free_nodes(nodes, node_count);
280+
et_cleanup(&lp, store);
281+
return ok;
282+
}
283+
284+
TEST(graphql_route_keyed_by_operation_name_issue598) {
285+
static const EtFile f[] = {
286+
{"graphql/client.py", "def gql(query):\n return query\n"},
287+
{"api/ops.py",
288+
"from graphql.client import gql\n\n\n"
289+
"def get_user():\n"
290+
" return gql('query GetUser($id: ID!) { user(id: $id) { name } }')\n\n\n"
291+
"def update_user():\n"
292+
" return gql('mutation UpdateUser($id: ID!) { updateUser(id: $id) { id } }')\n\n\n"
293+
"def list_users():\n"
294+
" return gql('fragment U on User { id } query ListUsers { users { ...U } }')\n\n\n"
295+
"def on_event():\n"
296+
" return gql('subscription OnEvent { event { id } }')\n\n\n"
297+
"def products():\n"
298+
" return gql('query { products(search: \"x\") { items { sku } } }')\n\n\n"
299+
"def orders():\n"
300+
" return gql('{ orders { id total } }')\n\n\n"
301+
"def logout():\n"
302+
" return gql('mutation { logout }')\n"}};
303+
static const EtGqlRoute want[] = {
304+
{"__graphql__GetUser", "GetUser"},
305+
{"__graphql__UpdateUser", "UpdateUser"},
306+
{"__graphql__ListUsers", "ListUsers"},
307+
{"__graphql__OnEvent", "OnEvent"},
308+
/* products + orders: `query { }` and the `{ }` shorthand are both
309+
* anonymous queries and share one key. */
310+
{"__graphql__query__anonymous", "(anonymous query)"},
311+
{"__graphql__mutation__anonymous", "(anonymous mutation)"},
312+
};
313+
ASSERT_TRUE(et_graphql_routes_exact(f, (int)(sizeof(f) / sizeof(f[0])), want,
314+
(int)(sizeof(want) / sizeof(want[0]))));
315+
PASS();
316+
}
317+
318+
/* #598 parser edges the end-to-end fixture cannot reach through one language's
319+
* string extraction: comments/strings/interpolations that contain braces, a
320+
* bare word, non-document arguments and name truncation. */
321+
TEST(graphql_operation_identity_parser_issue598) {
322+
const char *type = NULL;
323+
char name[16];
324+
static const struct {
325+
const char *doc;
326+
int named;
327+
const char *type;
328+
const char *name;
329+
} cases[] = {
330+
{"# fetch { all }\nquery Q1 { a }", 1, "query", "Q1"},
331+
{"${UserFields} query WithFrag($id: ID) { u { ...UserFields } }", 1, "query", "WithFrag"},
332+
{"fragment F on T @dir(x: \"{\") { a } mutation M2 { b }", 1, "mutation", "M2"},
333+
{"\"\"\"doc { brace\"\"\" subscription S3 { c }", 1, "subscription", "S3"},
334+
{" query \n ( $x: Int ) { a }", 0, "query", ""},
335+
{" { viewer { id } }", 0, "query", ""},
336+
{"query GetUserByIdentifier { a }", 1, "query", "GetUserByIdenti"}, /* bounded */
337+
{"data", 0, "operation", ""}, /* a bare word is not evidence of an operation */
338+
{"https://api.example.com/graphql", 0, "operation", ""},
339+
{"/graphql", 0, "operation", ""},
340+
{"", 0, "operation", ""},
341+
};
342+
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
343+
bool named = cbm_service_pattern_graphql_operation(cases[i].doc, &type, name, sizeof(name));
344+
if ((int)named != cases[i].named || strcmp(type, cases[i].type) != 0 ||
345+
strcmp(name, cases[i].name) != 0) {
346+
fprintf(stderr, " [ET-GQL] case %zu '%s' -> named=%d type=%s name=%s\n", i,
347+
cases[i].doc, (int)named, type, name);
348+
ASSERT_TRUE(0);
349+
}
350+
}
351+
ASSERT_FALSE(cbm_service_pattern_graphql_operation(NULL, &type, name, sizeof(name)));
352+
ASSERT_STR_EQ(type, "operation");
353+
PASS();
354+
}
355+
225356
/* #1085: count CALLS edges whose target node has `name`, indexing via the
226357
* PARALLEL path (et_index_parallel pads to >50 files). The per-file TS resolver
227358
* used to treat a raw tsconfig alias as a confident target. That false row made
@@ -1626,6 +1757,8 @@ TEST(override_go_interface) {
16261757
SUITE(edge_types_probe) {
16271758
/* HANDLES — route→handler across web frameworks */
16281759
RUN_TEST(calls_jsx_component_via_tsconfig_alias_parallel_issue1085);
1760+
RUN_TEST(graphql_route_keyed_by_operation_name_issue598);
1761+
RUN_TEST(graphql_operation_identity_parser_issue598);
16291762
RUN_TEST(handles_flask_python);
16301763
RUN_TEST(handles_fastapi_python);
16311764
RUN_TEST(handles_drf_action_python);

0 commit comments

Comments
 (0)