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