Coverage for graphqler / compiler / parsers / union_list_parser.py: 63%
19 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-20 10:09 -0400
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-20 10:09 -0400
1"""
2Union objects: https://spec.graphql.org/October2021/#sec-Unions
3"""
5from .parser import Parser
6from typing import List
9class UnionListParser(Parser):
10 def __init__(self):
11 self.excluded_types = [
12 "Mutation",
13 "Query",
14 "__Schema",
15 "__Type",
16 "__TypeKind",
17 "__Field",
18 "__InputValue",
19 "__EnumValue",
20 "__Directive",
21 "__DirectiveLocation",
22 ]
24 def extract_union_values(self, union_values: List[dict]) -> List[dict]:
25 """Extract only the relavent fields from the possible union values
27 Args:
28 union_valies (List[dict]): List of possible values of the UNION object
30 Returns:
31 List[dict]: List of possible balues of the UNION object but filtered for only relavent fields.
32 We also add the type field to the object if the kind if an object
33 """
34 list_of_union_values = []
35 for union_value in union_values:
36 filtered_union_value = {
37 "kind": union_value["kind"],
38 "name": union_value["name"],
39 "ofType": union_value["ofType"],
40 "type": union_value["name"] if union_value["kind"] == "OBJECT" else None,
41 }
42 list_of_union_values.append(filtered_union_value)
43 return list_of_union_values
45 def parse(self, introspection_data: dict) -> dict:
46 """Parses the introspection data for only objects
48 Args:
49 data (dict): Introspection JSON as a dictionary
51 Returns:
52 dict: List of objects with their types
53 """
54 # Grab just the objects from the dict
55 schema_types = introspection_data.get("data", {}).get("__schema", {}).get("types", [])
56 union_objects = [t for t in schema_types if t.get("kind") == "UNION" and t.get("name") not in self.excluded_types]
58 built_union_objects = {}
59 for union_object in union_objects:
60 union_name = union_object["name"]
61 built_union_objects[union_name] = {"possibleTypes": self.extract_union_values(union_object["possibleTypes"])}
62 return built_union_objects