GraphQL

GraphQL text is tokenized by a hand-written tokenizer, turned into a small query AST, and planned as a scan, insert, update or delete on a SQL table. There is no schema: the root field names a table and sub-selections name its columns. The result runs on QueryEngine::execute_plan (source: src/query/graphql/mod.rs, GraphQLAnalyzer::parse, tokenize, parse_tokens, to_logical_plan; src/protocol/http/routes.rs, execute_graphql).

The wire index lists HTTP/REST and GraphQL as Preview, with unit and integration tests only and no differential suite (source: HTTP/REST and GraphQL).

Entry points

POST /api/v1/graphql and POST /graphql take a JSON body {"query", "operationName", "variables"}. The handler uses only query; operationName and variables are read into the request struct and never used (source: src/protocol/http/routes.rs, execute_graphql; src/protocol/http/models.rs, GraphQLRequest).

A Select result is returned as {"data": {"result": [ {column: value, ...}, ... ]}}, with every value as a JSON string or null. Any other successful result is {"data": {"success": true}}. An error is {"errors": [{"message": ...}]} (source: src/protocol/http/routes.rs, execute_graphql).

QueryEngine::detect_language never returns GraphQL, and QueryLanguage::GraphQL is parsed as SQL there, so GraphQL is not accepted over the SQL wires (source: src/query/engine.rs, QueryEngine::detect_language, QueryEngine::parse_query).

Translation path

flowchart LR T["query text"] --> TK["tokenize"] TK --> PT["parse_tokens"] PT --> LP["to_logical_plan (first root field only)"] LP -->|"query"| Q["Scan, Project, Limit"] LP -->|"mutation createX"| I["Insert into xs"] LP -->|"mutation updateX"| U["Update xs"] LP -->|"mutation deleteX"| D["Delete from xs"] Q --> E["QueryEngine::execute_plan"] I --> E U --> E D --> E

(source: src/query/graphql/mod.rs, tokenize, parse_tokens, to_logical_plan, query_to_plan, mutation_to_plan)

Accepted syntax

Element Handling
Operation query, mutation, subscription, or a bare { (implicit query)
Operation name Not accepted: query Name { ... } fails with "Expected '{'"
Selection set Field names, each with optional arguments and an optional nested selection set
Root fields Only the first is planned; the rest are parsed and ignored
Argument values Strings, integers, floats, true, false, null, and objects {k: v}
Lists, enums, variables ($x) Not accepted: the value parser fails with "Expected value"
Aliases, fragments, directives Not recognised; the tokenizer skips unknown characters such as : in a selection, ... and @, so the remaining names are read as extra fields
Comments # to end of line

(source: src/query/graphql/mod.rs, tokenize, parse_tokens, parse_selection_set, parse_arguments, parse_value, parse_object)

Query translation

For the root field users(where: {...}, limit: n) { id name }:

  • The field name is the table name, used as written (source: src/query/graphql/mod.rs, query_to_plan).
  • Sub-selections become the scanned and projected columns; with no sub-selection the projection is *. Selections nested more than one level are parsed but not used (source: src/query/graphql/mod.rs, query_to_plan).
  • where must be an object of column: value pairs, combined with AND as equalities. A nested object as a value fails with "Nested objects not supported in expressions" (source: src/query/graphql/mod.rs, where_object_to_expr, graphql_value_to_expr).
  • limit takes an integer. offset and orderBy are tokenized as argument names but not applied (source: src/query/graphql/mod.rs, query_to_plan, parse_arguments).

The plan is Scan(table, filter, columns) under Project, under Limit when a limit is given (source: src/query/graphql/mod.rs, query_to_plan).

Mutation translation

The table name is the mutation name without its prefix, lowercased, with s appended: createUser writes to users, deletePost to posts (source: src/query/graphql/mod.rs, mutation_name_to_table). A root field that does not start with create, update or delete fails with "Unknown mutation: <name>" (source: src/query/graphql/mod.rs, mutation_to_plan).

Mutation Plan
createX(a: 1, b: "x") { id } Insert with one row; each argument is a column; sub-selections become RETURNING columns
updateX(where: {...}, data: {...}) Update; data pairs and any other argument become assignments
deleteX(where: {...}) Delete

(source: src/query/graphql/mod.rs, create_to_insert, update_to_plan, delete_to_plan)

An update or delete mutation without where has no filter, so it changes or removes every row of the table (source: src/query/graphql/mod.rs, update_to_plan, delete_to_plan). Not verified: whether the HTTP response for a create mutation with sub-selections returns the RETURNING rows or {"success": true} depends on the result type the engine gives an insert with RETURNING; no test covers it.

Not wired

Not wired: no server path reaches the items below.

  • Introspection. __schema and __type are answered only by GraphQLAnalyzer::execute, from an IntrospectionSchema that is filled by register_table. The HTTP handler calls parse, not execute, so an introspection query is planned as a scan of a table named __schema or __type (source: src/query/graphql/mod.rs, GraphQLAnalyzer::execute, handle_introspection, register_table; src/protocol/http/routes.rs, execute_graphql). No code outside the module calls register_table.
  • Subscriptions. parse refuses a subscription with "Subscriptions cannot be converted to logical plans directly". execute parses subscriptions into a SubscriptionRequest, and subscription.rs and events.rs define a SubscriberManager, WebSocketHandler and EventBus, but nothing outside src/query/graphql/ uses them and no HTTP route serves a GraphQL WebSocket (source: src/query/graphql/mod.rs, to_logical_plan, parse_subscription; src/query/graphql/subscription.rs; src/query/graphql/events.rs).
  • QueryCompiler holds a GraphQLAnalyzer, but QueryCompiler itself has no caller (source: src/query/mod.rs, QueryCompiler).

Tests

  • src/query/graphql/mod.rs (9): test_simple_query, test_query_with_where, test_mutation_create, test_introspection_schema, test_introspection_type, test_execute_regular_query, test_subscription_parsing, test_subscription_with_filter, test_subscription_delete.
  • src/query/graphql/events.rs (9), src/query/graphql/subscription.rs (6), src/query/graphql/introspection.rs (3).

These tests stop at the plan, the introspection JSON or the subscription structures. Not verified: no test sends a GraphQL request through the HTTP handler or executes a GraphQL plan on the engine.