subgrounds.query module#
Query data structure module
This module contains various data structures in the form of dataclasses that
are used to represent GraphQL queries in Subgrounds using an AST-like approach.
To the extent possible, these dataclasses are immutable (i.e.: frozen=True
)
to enforce a functional programming style and reduce side-effects.
A typical Subgrounds request will have the following dataclass hierarchy:
DataRequest
└── Document
└── Query
├── VariableDefinition
│ └── InputValue
└── Selection
├── Argument
│ └── InputValue
└── Selection
- class subgrounds.query.InputValue#
Bases:
object
- class T(*args, **kwargs)#
Bases:
Protocol
- property graphql: str#
Returns a GraphQL string representation of the input value
- Returns:
The GraphQL string representation of the input value
- Return type:
- property is_variable: bool#
Returns True i.f.f. the input value is of type Variable
- Returns:
True i.f.f. the input value is of type Variable, otherwise False
- Return type:
- property is_number: bool#
Returns True i.f.f. the input value is of type Float or Int
- Returns:
True i.f.f. the input value is of type Float or Int, otherwise False
- Return type:
- iter()#
- class subgrounds.query.VariableDefinition(name, type_, default=None)#
Bases:
object
Representation of a GraphQL variable definition
- default#
Default value of the variable. Defaults to None.
- Type:
InputValue.T, optional
- property graphql: str#
Returns the GraphQL string representation of the variable definition
Example:
>>> vardef = VariableDefinition( ... name='foo', ... type_=TypeRef.NonNull(TypeRef.Named(name="Int", kind="SCALAR")), ... default=InputValue.Int(100) ... ) >>> print(vardef.graphql) $foo: Int! = 100
- Returns:
The GraphQL string representation of the variable definition
- Return type:
- class subgrounds.query.Argument(name: 'str', value: 'InputValue.T')#
Bases:
object
- iter()#
- iter_vars()#
- for_all(predicate)#
- for_all_vars(predicate)#
- exists(predicate)#
- exists_vars(predicate)#
- find(predicate)#
- find_var(predicate)#
- all_defined(variables)#
- class subgrounds.query.Selection(fmeta, alias=None, arguments=<factory>, selection=<factory>)#
Bases:
object
Represents a GraphQL field selection.
- fmeta#
The type definition of the field being selected.
- Type:
- arguments: list[subgrounds.query.Argument]#
- selection: list[subgrounds.query.Selection]#
- property key#
- graphql(level=0)#
- iter()#
Returns an iterator over all
Selections
of the current selection tree.
- iter_args(recurse=True)#
Returns an iterator over all
Arguments
of the currentSelection
.If
recurse == True
, then the iterator also includesArguments
of innerSelections
.
- filter(predicate)#
Returns a new
Selection
object containing all attributes of the currentSelection
ifpredicate(self) == True
andNone
otherwise. The function if also applied recursively to innerSelections
.
- filter_args(predicate, recurse=True)#
Returns a new
Selection
object which contains all attributes of the currentSelection
except forArguments
for whichpredicate(arg) == True
.If
recurse == True
, then the function is applied recursively to innerSelections
- map(map_f, priority='self')#
Returns a new
Selection
object containing the same selection tree as the currentSelection
where eachSelection
objects
ismap_f(s)
- map_args(map_f, recurse=True)#
Replaces each
Argument
arg
in the currentSelection
withmap_f(arg)
and returns a newSelection
object containinf the modified arguments.If
recurse == True
, then the function is applied recursively to innerSelections
.
- filter_map(map_f)#
- filter_map_args(map_f, recurse=True)#
- for_all(predicate)#
- for_all_args(predicate, recurse=True)#
- exists(predicate)#
- exists_args(predicate, recurse=True)#
- find(predicate)#
- find_args(predicate, recurse=True)#
- find_all(predicate)#
- find_all_args(predicate, recurse=True)#
- T#
alias of TypeVar('T')
- fold(fold_f, parents=[])#
- contains_list()#
Returns True i.f.f. the selection
self
selects a field of type list.
- split()#
Returns a list of selections where each of the selections corresponds to a single selection path from the root to a leaf for each leaf selected in
self
.Example (simplified, does not show all attributes):
>>> select = Selection('foo', inner=[ ... Selection('bar', inner=[ ... Selection('field0', inner=[]), ... Selection('field1', inner=[]), ... ]), ... Selection('x', inner=[]) ... ]) >>> split(select) [ Selection('foo', inner=[Selection('bar', inner=[Selection('field0', inner=[])])]), Selection('foo', inner=[Selection('bar', inner=[Selection('field1', inner=[])])]), Selection('foo', inner=[Selection('x', inner=[])]), ]
- extract_data(data)#
- add(new_selections)#
Returns a new selection consisting of a copy of
self
expanded with the selection(s)new_selections
. It is assumed thatnew_selections
are inner selections of the root selectionself
.
- remove(to_remove)#
Returns a new Selection object consisting of a copy of
self
without the selections inselections_to_remove
.
- variable_args(recurse=True)#
Returns all arguments in the current selection which have been given a variable as value.
If
recurse == True
, then the function is applied recursively to inner selections.
- infer_variable_definitions()#
- combine(other)#
- static merge(selections)#
Returns a list of Selection objects resulting from merging
selections
to the extent possible.
- contains(other)#
Returns True i.f.f. the Selection
other
is a subtree of the Selectionself
and False otherwise
- contains_argument(argname, recurse=True)#
Returns True i.f.f. there is an Argument object in
self
namedargname
. Ifrecurse
is True, then the method also checks the nested selections for an argument namedargname
.- Parameters:
- Returns:
True i.f.f. there is an argument named
argname
inself
- Return type:
- get_argument(argname, recurse=True)#
Returns an Argument object corresponding to the argument in the Selection object
select
with nameargname
. Ifselect
does not contain such an argument andrecurse
is True, then the function is called recursively onselect
's inner selections. If no such argument is found inselect
or its inner selections, then the function raises an exception.
- get_argument_by_variable(varname, recurse=True)#
Returns an Argument object corresponding to the argument in the Selection object
select
whose value is a variable namedvarname
. Ifselect
does not contain such an argument andrecurse
is True, then the function is called recursively onselect
's inner selections. If no such argument is found inselect
or its inner selections, then the function raises an exception
- substitute_arg(argname, replacement, recurse=True)#
Returns a new Selection object containing the same data as
self
with the argument namedargname
replaced withreplacement
. Ifrecurse
is True, then the method is called recursively onself
's inner selections and the substitution is also applied to the latter.- Parameters:
- Returns:
_description_
- Return type:
- select(other)#
- prune_undefined(variables)#
Return a new
Selection
containing the subtree of the currentSelection
where all argumentInputValues
are defined, i.e.: each argument'sInputValue
is either1) not of type
InputValue.Variable
or 2) of typeInputValue.Variable
and the variable name is contained invariables
.
- class subgrounds.query.Query(name: 'Optional[str]' = None, selection: 'list[Selection]' = <factory>, variables: 'list[VariableDefinition]' = <factory>)#
Bases:
object
- selection: list[subgrounds.query.Selection]#
- variables: list[subgrounds.query.VariableDefinition]#
- property graphql: str#
Returns a string containing a GraphQL query matching the current query
- Returns:
The string containing the GraphQL query
- Return type:
- iter()#
Returns an iterator over all
Selections
of the selection tree of the currentQuery
.
- iter_args()#
Returns an iterator over all
Arguments
of the selection tree of the currentQuery
.
- iter_vardefs()#
Returns an iterator over all
VariableDefinitions
of the selection tree of the currentQuery
.
- filter(predicate)#
Returns a new
Query
object containing all selectionss
that satisfypredicate(s) == True
.
- filter_args(predicate)#
Returns a new
Query
object containing all selections argumentsarg
that satisfypredicate(arg) == True
.
- filter_vardefs(predicate)#
- map(map_f, priority='self')#
Applies the function
map_f
to eachSelection
in the currentQuery
and returns a newQuery
object containing the resultingSelections
.
- map_args(map_f)#
Applies the function
map_f
to eachArgument
in the currentQuery
and returns a newQuery
object containing the resultingArguments
.
- map_vardefs(map_f)#
- filter_map(map_f)#
- filter_map_args(map_f)#
- filter_map_vardefs(map_f)#
- for_all(predicate)#
- for_all_args(predicate)#
- for_all_vardefs(predicate)#
- exists(predicate)#
- exists_args(predicate)#
- exists_vardefs(predicate)#
- find(predicate)#
- find_args(predicate)#
- find_vardefs(predicate)#
- T#
alias of TypeVar('T')
- fold(fold_f)#
- infer_variable_definitions()#
- add(other)#
Returns a new Query containing all selections in :attr:'self' along with the new selections in
other
- add_vardefs(vardefs)#
- remove(other)#
Returns a new
Query
object containing all selections inself
minus the subquery or selection(s) specified inother
.Note:
other
does not need to be a "full" selection (i.e.: a selection all the way to leaves of the GraphQL schema).Example:
>>> og_selection = Selection(TypeMeta.FieldMeta('pair', description="", args=[], type=TypeRef.non_null_list("Pair", kind="OBJECT")), None, [], [ ... Selection(TypeMeta.FieldMeta('token0', description="", args=[], type=TypeRef.Named(name="Token", kind="OBJECT")), None, [], [ ... Selection(TypeMeta.FieldMeta('id', description="", args=[], type=TypeRef.Named(name="String", kind="SCALAR")), None, [], []), ... Selection(TypeMeta.FieldMeta('name', description="", args=[], type=TypeRef.Named(name="String", kind="SCALAR")), None, [], []), ... Selection(TypeMeta.FieldMeta('symbol', description="", args=[], type=TypeRef.Named(name="String", kind="SCALAR")), None, [], []), ... ]) ... ]) >>> selection_to_remove = Selection(TypeMeta.FieldMeta('token0', description="", args=[], type=TypeRef.Named(name="Token", kind="OBJECT")), None, [], []) >>> og_selection.remove(selection_to_remove) Selection(TypeMeta.FieldMeta('pair', description="", args=[], type=TypeRef.non_null_list("Pair", kind="OBJECT")), None, [], [])
- static transform(query, variable_f=<function identity>, selection_f=<function identity>)#
- contains_selection(selection)#
Returns True i.f.f. the selection tree
selection
is present inquery
.
- contains_argument(argname)#
- get_argument(argname)#
- static substitute_arg(query, arg_name, replacement)#
- static contains(query, other)#
Returns True i.f.f. all selections in other are contained in query. In other words, returns true i.f.f. other is a subset of query.
Note: other does not need to include "full" selections (i.e.: selections all the way to leaves of the GraphQL schema).
- static select(query, other)#
Returns a new Query
- prune_undefined(variables)#
- class subgrounds.query.Fragment(name: 'str', type_: 'TypeRef.T', selection: 'list[Selection]' = <factory>, variables: 'list[VariableDefinition]' = <factory>)#
Bases:
object
- selection: list[subgrounds.query.Selection]#
- variables: list[subgrounds.query.VariableDefinition]#
- property graphql#
- static combine(frag, other)#
- static transform(frag, f)#
- class subgrounds.query.Document(url: 'str', query: 'Query', fragments: 'list[Fragment]' = <factory>, variables: 'dict[str, Any]' = <factory>)#
Bases:
object
- fragments: list[subgrounds.query.Fragment]#
- property graphql#
- static mk_single_query(url, query)#
- filter(predicate)#
- filter_args(predicate)#
- map(map_f)#
Applies the function
map_f
to eachSelection
in the currentDocument
and returns a newDocument
object containing the resultingSelections
.
- map_args(map_f)#
Applies the function
map_f
to eachArgument
in the currentDocument
and returns a newDocument
object containing the resultingArguments
.
- filter_map(map_f)#
- static combine(doc, other)#
- static transform(doc, query_f=<function identity>, fragment_f=<function identity>)#
- prune_undefined(variables)#
Returns a new
Document
object that contains the subset of the currentDocument
's query containing only theSelections
for which all its arguments are defined (i.e.: either constants or variables invariables
).
- class subgrounds.query.DataRequest(documents: 'list[Document]' = <factory>)#
Bases:
object
- documents: list[subgrounds.query.Document]#
- property graphql#
- static combine(req, other)#
- static transform(req, f)#
- static single_query(url, query)#
- static single_document(doc)#
- static add_documents(self, docs)#
- subgrounds.query.selections_of_object(schema, object_)#
Returns generator of Selection objects that selects all non-list fields of GraphQL Object of Interface
object_
.- Parameters:
schema (SchemaMeta) -- _description_
object (TypeMeta.ObjectMeta | TypeMeta.InterfaceMeta) -- _description_
- Yields:
_type_ -- _description_
- subgrounds.query.input_value_of_argument(schema, argmeta, value)#
- subgrounds.query.arguments_of_field_args(schema, field, args)#