Buckets:

download
raw
18 kB
"""GraphQL schema introspection parser and converter.
Parses GraphQL introspection responses and SDL schemas, converting them
to Tessera assets with JSON Schema contracts.
"""
from __future__ import annotations
import logging
from typing import Any
from uuid import UUID
from pydantic import BaseModel
from tessera.models.enums import ResourceType
logger = logging.getLogger(__name__)
class GraphQLOperation(BaseModel):
"""Parsed GraphQL query or mutation."""
name: str
operation_type: str # "query" or "mutation"
description: str | None
args: list[dict[str, Any]] # List of argument definitions
return_type: dict[str, Any] # JSON Schema for return type
combined_schema: dict[str, Any] # Combined args + return for contract
guarantees: dict[str, Any] | None = None # Inferred nullability guarantees
class GraphQLParseResult(BaseModel):
"""Result of parsing a GraphQL schema."""
schema_name: str
description: str | None
operations: list[GraphQLOperation]
types: dict[str, dict[str, Any]] # Type name -> JSON Schema
errors: list[str]
# GraphQL scalar to JSON Schema type mapping
SCALAR_MAPPING: dict[str, dict[str, Any]] = {
"String": {"type": "string"},
"Int": {"type": "integer"},
"Float": {"type": "number"},
"Boolean": {"type": "boolean"},
"ID": {"type": "string"},
# Common custom scalars
"DateTime": {"type": "string", "format": "date-time"},
"Date": {"type": "string", "format": "date"},
"Time": {"type": "string", "format": "time"},
"JSON": {"type": "object"},
"JSONObject": {"type": "object"},
"BigInt": {"type": "integer"},
"Decimal": {"type": "number"},
"UUID": {"type": "string", "format": "uuid"},
"URL": {"type": "string", "format": "uri"},
"Email": {"type": "string", "format": "email"},
}
def _graphql_type_to_json_schema(
type_ref: dict[str, Any],
types_map: dict[str, dict[str, Any]],
depth: int = 0,
) -> tuple[dict[str, Any], bool]:
"""Convert a GraphQL type reference to JSON Schema.
Returns:
Tuple of (schema, is_required) where is_required indicates NON_NULL wrapping
"""
if depth > 10:
return {"type": "object"}, False
kind = type_ref.get("kind", "")
name = type_ref.get("name")
of_type = type_ref.get("ofType")
if kind == "NON_NULL":
# Non-null wrapping - recurse and mark as required
inner_schema, _ = _graphql_type_to_json_schema(of_type or {}, types_map, depth + 1)
return inner_schema, True
if kind == "LIST":
# List type - wrap in array
inner_schema, _ = _graphql_type_to_json_schema(of_type or {}, types_map, depth + 1)
return {"type": "array", "items": inner_schema}, False
if kind == "SCALAR":
# Map scalar to JSON Schema type
return SCALAR_MAPPING.get(name or "", {"type": "string"}), False
if kind == "ENUM":
# Enum - get values from types map
type_def = types_map.get(name or "", {})
enum_values = type_def.get("enumValues", [])
if enum_values:
return {
"type": "string",
"enum": [v.get("name") for v in enum_values if v.get("name")],
}, False
return {"type": "string"}, False
if kind in ("OBJECT", "INPUT_OBJECT", "INTERFACE"):
# Object types - build properties from fields
type_def = types_map.get(name or "", {})
fields = type_def.get("fields") or type_def.get("inputFields") or []
properties: dict[str, Any] = {}
required: list[str] = []
for field in fields:
field_name = field.get("name")
if not field_name:
continue
field_type = field.get("type", {})
field_schema, is_required = _graphql_type_to_json_schema(
field_type, types_map, depth + 1
)
# Preserve field descriptions from GraphQL introspection
field_desc = field.get("description")
if field_desc:
field_schema = {**field_schema, "description": field_desc}
properties[field_name] = field_schema
if is_required:
required.append(field_name)
result: dict[str, Any] = {"type": "object", "properties": properties}
if required:
result["required"] = required
return result, False
if kind == "UNION":
# Union type - use anyOf
type_def = types_map.get(name or "", {})
possible_types = type_def.get("possibleTypes", [])
if possible_types:
any_of: list[dict[str, Any]] = []
for pt in possible_types:
pt_schema, _ = _graphql_type_to_json_schema(
{"kind": "OBJECT", "name": pt.get("name")},
types_map,
depth + 1,
)
any_of.append(pt_schema)
return {"anyOf": any_of}, False
return {"type": "object"}, False
# Fallback for named types (lookup in types_map)
if name:
# Try to resolve from types_map
type_def = types_map.get(name, {})
type_kind = type_def.get("kind", "")
if type_kind:
return _graphql_type_to_json_schema(
{"kind": type_kind, "name": name},
types_map,
depth + 1,
)
return {"type": "object"}, False
def _build_types_map(introspection_types: list[dict[str, Any]]) -> dict[str, dict[str, Any]]:
"""Build a map of type name -> type definition from introspection."""
types_map: dict[str, dict[str, Any]] = {}
for type_def in introspection_types:
name = type_def.get("name")
if name and not name.startswith("__"):
types_map[name] = type_def
return types_map
def _extract_operations(
type_def: dict[str, Any] | None,
operation_type: str,
types_map: dict[str, dict[str, Any]],
) -> list[GraphQLOperation]:
"""Extract operations from Query or Mutation type."""
if not type_def:
return []
operations: list[GraphQLOperation] = []
fields = type_def.get("fields") or []
for field in fields:
name = field.get("name")
if not name:
continue
description = field.get("description")
args = field.get("args", [])
# Build args schema
args_properties: dict[str, Any] = {}
args_required: list[str] = []
args_list: list[dict[str, Any]] = []
for arg in args:
arg_name = arg.get("name")
if not arg_name:
continue
arg_type = arg.get("type", {})
arg_schema, is_required = _graphql_type_to_json_schema(arg_type, types_map)
args_properties[arg_name] = arg_schema
if is_required:
args_required.append(arg_name)
args_list.append(
{
"name": arg_name,
"description": arg.get("description"),
"type": arg_schema,
"required": is_required,
}
)
args_schema: dict[str, Any] = {"type": "object", "properties": args_properties}
if args_required:
args_schema["required"] = args_required
# Build return type schema
return_type = field.get("type", {})
return_schema, _ = _graphql_type_to_json_schema(return_type, types_map)
# Combined schema for contract
# Response is always required (a query always returns something)
# Arguments is only required if it has required fields
combined_required = ["response"]
if args_required:
combined_required.insert(0, "arguments")
combined: dict[str, Any] = {
"type": "object",
"properties": {
"arguments": args_schema,
"response": return_schema,
},
"required": combined_required,
}
# Build guarantees from required args (NON_NULL in GraphQL)
nullability: dict[str, str] = {}
for arg_info in args_list:
if arg_info.get("required"):
nullability[arg_info["name"]] = "never"
guarantees: dict[str, Any] | None = None
if nullability:
guarantees = {"nullability": nullability}
operations.append(
GraphQLOperation(
name=name,
operation_type=operation_type,
description=description,
args=args_list,
return_type=return_schema,
combined_schema=combined,
guarantees=guarantees,
)
)
return operations
def parse_graphql_introspection(introspection: dict[str, Any]) -> GraphQLParseResult:
"""Parse a GraphQL introspection response.
The introspection should be the result of the standard introspection query:
{ __schema { ... } }
Args:
introspection: The introspection response dict with __schema or data.__schema
Returns:
GraphQLParseResult with parsed operations and any errors
"""
errors: list[str] = []
operations: list[GraphQLOperation] = []
# Handle different response formats
# Could be: {"__schema": {...}}, {"data": {"__schema": {...}}}, or just the schema
schema = introspection.get("__schema")
if not schema:
data = introspection.get("data", {})
schema = data.get("__schema")
if not schema:
# Maybe the introspection IS the schema
if "types" in introspection and "queryType" in introspection:
schema = introspection
if not schema:
errors.append("No __schema found in introspection response")
return GraphQLParseResult(
schema_name="unknown",
description=None,
operations=[],
types={},
errors=errors,
)
# Build types map
introspection_types = schema.get("types", [])
types_map = _build_types_map(introspection_types)
# Convert types to JSON Schema
json_schema_types: dict[str, dict[str, Any]] = {}
for name, type_def in types_map.items():
if type_def.get("kind") in ("OBJECT", "INPUT_OBJECT", "INTERFACE", "ENUM"):
schema_def, _ = _graphql_type_to_json_schema(
{"kind": type_def.get("kind"), "name": name},
types_map,
)
json_schema_types[name] = schema_def
# Extract Query operations
query_type_name = (schema.get("queryType") or {}).get("name")
if query_type_name:
query_type = types_map.get(query_type_name)
operations.extend(_extract_operations(query_type, "query", types_map))
# Extract Mutation operations
mutation_type_name = (schema.get("mutationType") or {}).get("name")
if mutation_type_name:
mutation_type = types_map.get(mutation_type_name)
operations.extend(_extract_operations(mutation_type, "mutation", types_map))
# Get schema description from Query type if available
schema_description = None
if query_type_name:
query_type = types_map.get(query_type_name)
if query_type:
schema_description = query_type.get("description")
# Determine schema name - use the API name if provided, otherwise default
schema_name = "GraphQL API"
# Check for common directives or extensions that might have a name
description = schema.get("description")
if description and isinstance(description, str):
schema_name = description[:50] # Truncate if too long
return GraphQLParseResult(
schema_name=schema_name,
description=schema_description,
operations=operations,
types=json_schema_types,
errors=errors,
)
def _normalize_for_fqn(value: str) -> str:
"""Normalize a pre-validated value for FQN use.
Lowercases and replaces spaces/hyphens with underscores.
Collapses consecutive underscores and strips leading/trailing ones.
This must only be called on values that have already passed
:func:`~tessera.services.fqn.validate_fqn_component`.
"""
normalized = value.lower().replace(" ", "_").replace("-", "_")
while "__" in normalized:
normalized = normalized.replace("__", "_")
normalized = normalized.strip("_")
return normalized or "unknown"
def generate_fqn(schema_name: str, operation_name: str, operation_type: str) -> str:
"""Generate a fully qualified name for a GraphQL operation.
Format: graphql.<schema_name>.<type>_<operation_name>
Example: graphql.users_api.query_list_users
All inputs are validated to reject dots, slashes, and other
special characters that could cause FQN injection.
Args:
schema_name: The schema/API name (alphanumeric, underscores,
hyphens, spaces only).
operation_name: The operation name (e.g., listUsers).
operation_type: "query" or "mutation".
Returns:
A valid FQN string.
Raises:
FQNComponentError: If any component contains unsafe characters
like dots or slashes.
"""
from tessera.services.fqn import validate_fqn_component
# Spaces and hyphens are safe (normalized to underscores below),
# so validate after replacing them.
_schema_check = schema_name.replace(" ", "_").replace("-", "_")
validate_fqn_component(_schema_check, "GraphQL schema name")
validate_fqn_component(operation_name, "GraphQL operation name")
validate_fqn_component(operation_type, "GraphQL operation type")
normalized_name = _normalize_for_fqn(schema_name)
normalized_op = _normalize_for_fqn(operation_name)
return f"graphql.{normalized_name}.{operation_type}_{normalized_op}"
class AssetFromGraphQL(BaseModel):
"""Asset to be created from a GraphQL operation."""
fqn: str
resource_type: ResourceType
metadata: dict[str, Any]
schema_def: dict[str, Any]
guarantees: dict[str, Any] | None = None
field_descriptions: dict[str, str] = {}
field_tags: dict[str, list[str]] = {}
description: str | None = None
def operations_to_assets(
result: GraphQLParseResult,
owner_team_id: UUID,
environment: str = "production",
schema_name_override: str | None = None,
) -> list[AssetFromGraphQL]:
"""Convert parsed GraphQL operations to Tessera asset definitions.
Args:
result: The parsed GraphQL result
owner_team_id: The team that will own these assets
environment: The environment for the assets
schema_name_override: Optional override for the schema name in FQN generation
Returns:
List of AssetFromGraphQL ready to be created
"""
assets: list[AssetFromGraphQL] = []
schema_name = schema_name_override or result.schema_name
for op in result.operations:
fqn = generate_fqn(schema_name, op.name, op.operation_type)
metadata = {
"graphql_source": {
"schema_name": schema_name,
"schema_description": result.description,
"operation_name": op.name,
"operation_type": op.operation_type,
"description": op.description,
"arguments": op.args,
}
}
# Extract field descriptions from argument descriptions
field_descs: dict[str, str] = {}
for arg_info in op.args:
arg_name = arg_info.get("name")
arg_desc = arg_info.get("description")
if arg_name and arg_desc:
field_descs[f"$.properties.arguments.properties.{arg_name}"] = arg_desc
# Extract field descriptions from return type fields
return_props = op.return_type.get("properties", {})
for field_name, field_schema in return_props.items():
if isinstance(field_schema, dict):
desc = field_schema.get("description")
if desc:
field_descs[f"$.properties.response.properties.{field_name}"] = desc
assets.append(
AssetFromGraphQL(
fqn=fqn,
resource_type=ResourceType.GRAPHQL_QUERY,
metadata=metadata,
schema_def=op.combined_schema,
guarantees=op.guarantees,
field_descriptions=field_descs,
description=op.description,
)
)
return assets
# ---------------------------------------------------------------------------
# SDL → introspection bridge
# ---------------------------------------------------------------------------
def sdl_to_introspection(sdl_text: str) -> dict[str, Any]:
"""Convert a GraphQL SDL string to the introspection format.
Uses ``graphql-core``'s ``build_schema()`` to parse SDL, then
``introspection_from_schema()`` to produce the standard introspection
dict that :func:`parse_graphql_introspection` expects.
Args:
sdl_text: Raw GraphQL SDL content (e.g. from a ``.graphql`` file).
Returns:
Introspection dict with a top-level ``__schema`` key.
Raises:
ValueError: If the SDL cannot be parsed.
"""
try:
from graphql import build_schema, introspection_from_schema
except ImportError as e:
raise ImportError(
"graphql-core is required for SDL parsing. Install it with: pip install graphql-core"
) from e
try:
schema = build_schema(sdl_text)
except Exception as e:
raise ValueError(f"Invalid GraphQL SDL: {e}") from e
result: dict[str, Any] = dict(introspection_from_schema(schema))
# introspection_from_schema returns {"__schema": {...}} directly
if "__schema" not in result:
# Defensive: wrap if the library returns the schema dict directly
return {"__schema": result}
return result

Xet Storage Details

Size:
18 kB
·
Xet hash:
43a1cd9dc540936ce07df4c9a2bdc864451887f4678f0068f3ef09c167c651e0

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.