ds_provider_xledger_py_lib.utils.query_builder

File: query_builder.py Region: ds_provider_xledger_py_lib/utils

Description

GraphQL query/mutation template rendering helpers.

Attributes

_FIELDS_PLACEHOLDER

_DBIDS_PLACEHOLDER

_WRITE_OPERATIONS

_QUERY_ARG_MAPPINGS

Functions

build_variables(→ dict[str, Any])

Build GraphQL variables payload for an operation.

build_query(→ str)

Build a rendered GraphQL query for read operations.

build_mutation(→ str)

Build a rendered GraphQL mutation for write operations.

_render_template(→ str)

Render supported placeholders in packaged GraphQL templates.

_build_selection_set(→ str)

Build GraphQL selection set string from flattened field metadata.

_format_tree(→ str)

Format nested selection tree into GraphQL selection syntax.

_resolve_fields(→ list[str])

Resolve fields to render for query/mutation selection.

_apply_query_arguments(→ str)

Apply provided keyword arguments to the first query argument block.

_extract_query_args(→ str)

Extract query arguments from a query string.

_upsert_query_arg(→ str)

Upsert argument value in a query argument block.

_to_graphql_literal(→ str)

Convert Python values into GraphQL literal fragments.

_assign_nested_key(→ None)

Assign value to flat or a_b nested key path.

_build_placeholder_input(→ list[dict[str, Any]])

Build PlaceHolderInput payload list for create/update operations.

_build_node(→ dict[str, Any])

Build a filtered GraphQL node from a dataframe row record.

_build_delete_variables(→ dict[str, Any])

Build delete operation variables from dbId values when available.

Module Contents

ds_provider_xledger_py_lib.utils.query_builder._FIELDS_PLACEHOLDER = '{{ FIELDS }}'
ds_provider_xledger_py_lib.utils.query_builder._DBIDS_PLACEHOLDER = '{{ DBIDS }}'
ds_provider_xledger_py_lib.utils.query_builder._WRITE_OPERATIONS
ds_provider_xledger_py_lib.utils.query_builder._QUERY_ARG_MAPPINGS = (('first', 'first'), ('last', 'last'), ('before', 'before'), ('after', 'after'), ('filter',...
ds_provider_xledger_py_lib.utils.query_builder.build_variables(*, obj: pandas.DataFrame, operation: ds_provider_xledger_py_lib.enums.OperationType, metadata: ds_provider_xledger_py_lib.utils.introspection.MetaData) dict[str, Any][source]

Build GraphQL variables payload for an operation.

Parameters:
  • obj – Input dataframe.

  • operation – Dataset operation type.

  • metadata – Loaded operation metadata.

Returns:

Variables dictionary matching the query template.

ds_provider_xledger_py_lib.utils.query_builder.build_query(*, metadata: ds_provider_xledger_py_lib.utils.introspection.MetaData, **kwargs: Any) str[source]

Build a rendered GraphQL query for read operations.

Parameters:
  • metadata – Operation metadata.

  • **kwargs – Query keyword arguments. fields controls selection set.

Returns:

GraphQL query text.

ds_provider_xledger_py_lib.utils.query_builder.build_mutation(*, metadata: ds_provider_xledger_py_lib.utils.introspection.MetaData, variables: dict[str, Any], return_fields: list[str] | None = None) str[source]

Build a rendered GraphQL mutation for write operations.

Parameters:
  • metadata – Operation metadata.

  • variables – Resolved variables required by the mutation template.

  • return_fields – Optional explicit fields to return.

Returns:

GraphQL mutation text.

ds_provider_xledger_py_lib.utils.query_builder._render_template(*, template: str, field_names: list[str], variables: dict[str, Any]) str[source]

Render supported placeholders in packaged GraphQL templates.

Parameters:
  • template – Raw GraphQL template from metadata.

  • field_names – Field names to include in node selections.

  • variables – Prepared variables payload used for placeholder expansion.

Returns:

Query template with known placeholders expanded.

ds_provider_xledger_py_lib.utils.query_builder._build_selection_set(fields: list[str]) str[source]

Build GraphQL selection set string from flattened field metadata.

Parameters:

fields – Operation fields from metadata.

Returns:

GraphQL selection set string.

ds_provider_xledger_py_lib.utils.query_builder._format_tree(tree: dict[str, Any]) str[source]

Format nested selection tree into GraphQL selection syntax.

Parameters:

tree – Nested dictionary representing selection structure.

Returns:

Selection set fragment string.

ds_provider_xledger_py_lib.utils.query_builder._resolve_fields(*, metadata: ds_provider_xledger_py_lib.utils.introspection.MetaData, requested_fields: list[str] | None) list[str][source]

Resolve fields to render for query/mutation selection.

Parameters:
  • metadata – Operation metadata.

  • requested_fields – Requested fields to include in the selection set.

Returns:

List of fields to include in the selection set.

ds_provider_xledger_py_lib.utils.query_builder._apply_query_arguments(*, query: str, **kwargs: Any) str[source]

Apply provided keyword arguments to the first query argument block.

Parameters:
  • query – The query string.

  • **kwargs – Query keyword arguments.

Returns:

The updated query string.

ds_provider_xledger_py_lib.utils.query_builder._extract_query_args(query: str) str[source]

Extract query arguments from a query string.

Parameters:

query – The query string.

Returns:

The query arguments string.

ds_provider_xledger_py_lib.utils.query_builder._upsert_query_arg(*, query_args: str, arg_name: str, value_literal: str) str[source]

Upsert argument value in a query argument block.

Parameters:
  • query_args – The query arguments string.

  • arg_name – GraphQL argument name.

  • value_literal – GraphQL literal value.

Returns:

Updated query arguments string with arg replaced or appended.

ds_provider_xledger_py_lib.utils.query_builder._to_graphql_literal(value: Any) str[source]

Convert Python values into GraphQL literal fragments.

Parameters:

value – The value to convert.

Returns:

The GraphQL literal string.

ds_provider_xledger_py_lib.utils.query_builder._assign_nested_key(*, target: dict[str, Any], key: str, value: Any) None[source]

Assign value to flat or a_b nested key path.

Parameters:
  • target – Target dictionary being built.

  • key – Field name. Supports flattened relation style (a_b).

  • value – Field value to assign.

ds_provider_xledger_py_lib.utils.query_builder._build_placeholder_input(*, records: list[dict[str, Any]], allowed_fields: set[str]) list[dict[str, Any]][source]

Build PlaceHolderInput payload list for create/update operations.

Parameters:
  • records – List of records to build placeholder input from.

  • allowed_fields – Allowed fields to include in the placeholder input.

Returns:

List of placeholder input.

ds_provider_xledger_py_lib.utils.query_builder._build_node(*, record: dict[str, Any], allowed_fields: set[str]) dict[str, Any][source]

Build a filtered GraphQL node from a dataframe row record.

Parameters:
  • record – Record to build node from.

  • allowed_fields – Allowed fields to include in the node.

Returns:

Dictionary of node.

ds_provider_xledger_py_lib.utils.query_builder._build_delete_variables(*, obj: pandas.DataFrame, records: list[dict[str, Any]]) dict[str, Any][source]

Build delete operation variables from dbId values when available.

Parameters:
  • obj – Input dataframe.

  • records – List of records to build delete variables from.

Returns:

Dictionary of variables.