API
- class GraphQLBuilder.GraphQLBuilder
This Class is used to build GraphQL Queries and Mutations. All functions are created to help access the GraphQL API of Hasura.io.
See https://hasura.io/docs/latest/graphql/core/index.html for more information.
The Class is also used to execute the queries and mutations. This is done via the execute_query function.
- build_delete_qry(typename: str, qry_filter: str | None = '') str
Builds a delete query
- Parameters:
typename (str) – Name of the Query Type
qry_filter (str, optional) – Filter als String!! e.g {journalName: {_eq: “Nature”}} or {_and: {contactMail: {_eq: “”}, journalName: {_eq: “”}}}
- Returns:
Final Query ready for execution
- Return type:
str
- build_graphQL_mutation_objects_from_dict(source_data: dict, typeschema: dict, custom_mapping: Dict[str, Any] | None = {}, custom_mapping_append_other: bool | None = False, custom_mapping_value_overwrite: Dict[str, Any] | None = {}, ignore_fields: List[str] | None = [], append_if_missing_fields: Dict[str, Any] | None = {}) str
Builds GraphQL Mutation Objects (=strings in a specfic format) from a source dictonary.
For the correct use with hasuro.io endpoints, you need to provide a valide TypeSchema as a dict. See the docs for more information.
This function also provides a mapping ‘service’, which allows you to map a source key to a target key in the TypeSchema. This is useful, when the source data is not in the correct format. Be aware, that if you use a custom_mapping, all other fields will be ignored. If you want to include them, set custom_mapping_append_other to True.
You can also use point seperated strings for dicts like ‘status.agreed’ to map nested dicts.
- Parameters:
source_data (dict) – Source Data as Dict
typeschema (dict) – The TypeSchema as Dict
custom_mapping (dict, optional) – Custom field mapping. Defaults to {}.
custom_mapping_append_other (bool, optional) – Automatically map all Fields not mentioned in custom mapping. Defaults to False.
custom_mapping_value_overwrite (dict, optional) – Allows to overwrite specific values, when processing data. Can only be used, when using a custom mapping. This will also append fields. Defaults to {}.
ignore_fields (list, optional) – List of fields (=keys) to ignore when building the query objects. Defaults to [].
append_if_missing_fields (dict, optional) – Used to fix broken or incomplete datasets. If a field is missing, it will be added with the given value. Defaults to {}.
- Returns:
returns all the items as a joined string, which can be used in a mutation query
- Return type:
str
- build_graphQL_mutation_objects_from_list(source_data: List[Any], key: str, itemtype: str, return_as_list: bool | None = False) str | List[Any]
Build Graph QL Mutation Objects from a List of Items
- Parameters:
source_data (List[Any]) – The List Containing the Items
key (str) – The Key (field) which the values in the list should be mapped to
itemtype (str) – Type of the Items in List (Int, Boolean, or String)
return_as_list (bool, optional) – If True, return the prepared items as a list instead of a final mutation string. Useful when using the Query Builder
- Returns:
the created Mutation Objects or List[Any]: the created Mutation Objects as List
- Return type:
str
- build_insert_mutation_qry(typename: str, data_objects: List[Any], returning_objects: List[Any], update_constraint: str | None = None, update_field_list: Iterable[str] | None = []) str
Builds a complete Mutation Query
- Parameters:
typename (str) – Name of the Type (without insert_)
data_objects (List[Any]) – List of the generated Mutation Objects (which should be strings by now!)
returning_objects (List[Any]) – List of fields to return. Strings, cannot be empty!
update_constraint ([type], optional) – Name of the update_constraint to check for. Defaults to None.
update_field_list (Iterable, optional) – Fields to be updated, when constraint hits. Defaults to [].
- Returns:
returnes the genrated query as string
- Return type:
str
- build_search_qry(typename: str, qry_filter: str, returning_fields: List[str | Dict[str, Any]], limit: int | None = 10) str
Builds a Search Query with optional filter and returns it
- Parameters:
typename (str) – Name of the Query Type
qry_filter (str) – Filter as String!! e.g {field: {_eq: “value”}} or {_and: {field_one: {_eq: “foo”}, field_two: {_eq: “bar”}}}
returning_fields (List[Any]) – List of fields which should be returned. Cannot be empty! For nested fields, use a dict. e.g. {“field”: [“subfield_one”, “subfield_two”]}
limit (int) – Amount of returned items, default 10
- Returns:
Final Query ready for execution
- Return type:
str
- execute_query(endpoint_url: str, qry: str, bearer_token: str | None = '') Dict[str, Any]
Executes a GraphQL Query and returns the result as a List of Dicts
- Parameters:
endpoint_url (str) – URL of the GraphQL Endpoint
qry (str) – Query to execute
bearer_token (str, optional) – Bearer Token for Auth. Defaults to “”.
- Returns:
(JSON) Result of the Query
- Return type:
List[Dict[str, Any]]
- get_path(path: List[str], source: Dict[Any, Any], fallback_return_value: Any | None = None) Any
Function the traverse a dict via a path and return the value of the last element in the path.
Attention: This CAN ALSO work if a stage of the path is a dict nested in a list with length 1, since it will always return the first element of a list.
- Parameters:
path (List[str]) – Path of Knots as List
source (Dict[Any, Any]) – Source Dict
fallback_return_value (Optional[Any]) – Allows to specify a return value if no value in the path was found, default None
- Returns:
value of Knot or None
- Return type:
Any
Examples
>>> get_path(["a", "b", "c"], {"a": {"b": {"c": "d"}}}) "d"