graphdoc.data.parser module

class graphdoc.data.parser.Parser(type_mapping: dict[type, str] | None = None)[source]

Bases: object

A class for parsing and handling of GraphQL objects.

DEFAULT_NODE_TYPES = {<class 'graphql.language.ast.DocumentNode'>: 'full schema', <class 'graphql.language.ast.EnumTypeDefinitionNode'>: 'enum schema', <class 'graphql.language.ast.EnumValueDefinitionNode'>: 'enum value', <class 'graphql.language.ast.ObjectTypeDefinitionNode'>: 'table schema'}
static _check_node_type(node: Node, type_mapping: dict[type, str] | None = None) str[source]

Check the type of a schema node.

Parameters:
  • node (Node) – The schema node to check

  • type_mapping (Optional[dict[type, str]]) – Custom mapping of node types to strings. Defaults to DEFAULT_NODE_TYPES

Returns:

The type of the schema node

Return type:

str

static parse_schema_from_file(schema_file: str | Path, schema_directory_path: str | Path | None = None) DocumentNode[source]

Parse a schema from a file.

Parameters:
  • schema_file (Union[str, Path]) – The name of the schema file

  • schema_directory_path (Optional[Union[str, Path]]) – A path to a directory containing schemas

Returns:

The parsed schema

Return type:

DocumentNode

Raises:

Exception – If the schema cannot be parsed

static update_node_descriptions(node: Node, new_value: str | None = None) Node[source]

Given a GraphQL node, recursively traverse the node and its children, updating all descriptions with the new value. Can also be used to remove descriptions by passing None as the new value.

Parameters:
  • node (Node) – The GraphQL node to update

  • new_value (Optional[str]) – The new description value. If None, the description will be removed.

Returns:

The updated node

Return type:

Node

static count_description_pattern_matching(node: Node, pattern: str) dict[str, int][source]

Counts the number of times a pattern matches a description in a node and its children.

Parameters:
  • node (Node) – The GraphQL node to count the pattern matches in

  • pattern (str) – The pattern to count the matches of

Returns:

A dictionary with the counts of matches

Return type:

dict[str, int]

static fill_empty_descriptions(node: Node, new_column_value: str = 'Description for column: {}', new_table_value: str = 'Description for table: {}', use_value_name: bool = True, value_name: str | None = None)[source]

Recursively traverse the node and its children, filling in empty descriptions with the new column or table value. Do not update descriptions that already have a value. Default values are provided for the new column and table descriptions.

Parameters:
  • node (Node) – The GraphQL node to update

  • new_column_value (str) – The new column description value

  • new_table_value (str) – The new table description value

  • use_value_name (bool) – Whether to use the value name in the description

  • value_name (Optional[str]) – The name of the value

Returns:

The updated node

Return type:

Node

static schema_equality_check(gold_node: Node, check_node: Node) bool[source]

A method to check if two schema nodes are equal. Only checks that the schemas structures are equal, not the descriptions.

Parameters:
  • gold_node (Node) – The gold standard schema node

  • check_node (Node) – The schema node to check

Returns:

Whether the schemas are equal

Return type:

bool

static schema_object_from_file(schema_file: str | Path, category: str | None = None, rating: int | None = None) SchemaObject[source]

Parse a schema object from a file.

static parse_objects_from_full_schema_object(schema: SchemaObject, type_mapping: dict[type, str] | None = None) dict[str, SchemaObject] | None[source]

Parse out all available tables from a full schema object.

Parameters:
  • schema (SchemaObject) – The full schema object to parse

  • type_mapping (Optional[dict[type, str]]) – Custom mapping of node types to strings. Defaults to DEFAULT_NODE_TYPES

Returns:

The parsed objects (tables and enums)

Return type:

Union[dict, None]