Query#
Caution
This module will be broken into a subpackage in the future. As always, internal APIs are not subject to the semver policy this repo holds.
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.VariableDefinition(name, type_, default=None)#
Representation of a GraphQL variable definition
- type_#
GraphQL type of the argument
- Type:
subgrounds.schema.TypeRef.T
- default#
Default value of the variable. Defaults to None.
- Type:
subgrounds.query.InputValue.T | None
- 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
- class subgrounds.query.Argument(name: 'str', value: 'InputValue.T')#
- class subgrounds.query.Selection(fmeta, alias=None, arguments=<factory>, selection=<factory>)#
Represents a GraphQL field selection.
- fmeta#
The type definition of the field being selected.
- Type:
subgrounds.schema.TypeMeta.FieldMeta
- arguments#
The arguments, if any, of the field selection. Defaults to [].
- Type:
- selection#
The inner field selections, if any. Defaults to [].
- Type:
- iter()#
Returns an iterator over all
Selectionsof the current selection tree.
- iter_args(recurse=True)#
Returns an iterator over all
Argumentsof the currentSelection.If
recurse == True, then the iterator also includesArgumentsof innerSelections.
- filter(predicate)#
Returns a new
Selectionobject containing all attributes of the currentSelectionifpredicate(self) == TrueandNoneotherwise. The function if also applied recursively to innerSelections.- Parameters:
- Returns:
_description_
- Return type:
subgrounds.query.Selection | None
- filter_args(predicate, recurse=True)#
Returns a new
Selectionobject which contains all attributes of the currentSelectionexcept forArgumentsfor whichpredicate(arg) == True.If
recurse == True, then the function is applied recursively to innerSelections
- map(map_f, priority='self')#
Returns a new
Selectionobject containing the same selection tree as the currentSelectionwhere eachSelectionobjectsismap_f(s)
- map_args(map_f, recurse=True)#
- Replaces each
Argumentargin the currentSelectionwith map_f(arg)and returns a newSelectionobject containing the modified arguments.
If
recurse == True, then the function is applied recursively to innerSelections.- Parameters:
map_f (Callable[[Argument], subgrounds.query.Argument | list[subgrounds.query.Argument]]) -- _description_
recurse (bool) -- _description_. Defaults to True.
- Returns:
_description_
- Return type:
- Replaces each
- contains_list()#
Returns True i.f.f. the selection
selfselects a field of type list.- Parameters:
self -- The selection to traverse
- Returns:
True if selection or nested selections selects a list field. False otherwise.
- Return type:
- 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=[])]), ]
- Parameters:
self -- The selection to split
- Returns:
The split selections
- Return type:
- add(new_selections)#
Returns a new selection consisting of a copy of
selfexpanded with the selection(s)new_selections. It is assumed thatnew_selectionsare inner selections of the root selectionself.- Parameters:
self -- The Selection object to be expanded
new_selections (subgrounds.query.Selection | list[subgrounds.query.Selection]) -- A single or multiple Selection object(s) to be added to
self
- Returns:
selfexpanded withnew_selections
- Return type:
The resulting new selection, i.e.
- remove(to_remove)#
Returns a new Selection object consisting of a copy of
selfwithout the selections inselections_to_remove.- Parameters:
to_remove (subgrounds.query.Selection | list[subgrounds.query.Selection]) -- The selection(s) to remove from
self- Returns:
selfwithoutselections_to_remove
- Return type:
The new trimmed down selection, i.e.
- 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.
- static merge(selections)#
Returns a list of Selection objects resulting from merging
selectionsto the extent possible.- Parameters:
merged (selections The selections to be) --
- Returns:
_description_
- Return type:
- contains(other)#
Returns True i.f.f. the Selection
otheris a subtree of the Selectionselfand False otherwise
- contains_argument(argname, recurse=True)#
Returns True i.f.f. there is an Argument object in
selfnamedargname. Ifrecurseis True, then the method also checks the nested selections for an argument namedargname.
- get_argument(argname, recurse=True)#
Returns an Argument object corresponding to the argument in the Selection object
selectwith nameargname. Ifselectdoes not contain such an argument andrecurseis True, then the function is called recursively onselect's inner selections. If no such argument is found inselector its inner selections, then the function raises an exception.- Parameters:
- Raises:
KeyError -- If no argument named
argnameexists in the selectionself.- Returns:
The argument in
selectwith nameargname(if any).- Return type:
subgrounds.query.Argument | None
- get_argument_by_variable(varname, recurse=True)#
Returns an Argument object corresponding to the argument in the Selection object
selectwhose value is a variable namedvarname. Ifselectdoes not contain such an argument andrecurseis True, then the function is called recursively onselect's inner selections. If no such argument is found inselector its inner selections, then the function raises an exception- Parameters:
- Raises:
KeyError -- If no argument with variable value named
varnameexists in the selectionself.- Returns:
- The argument in
selectwith variable value namedvarname if it exists
- The argument in
- Return type:
subgrounds.query.Argument | None
- substitute_arg(argname, replacement, recurse=True)#
Returns a new Selection object containing the same data as
selfwith the argument namedargnamereplaced withreplacement. Ifrecurseis True, then the method is called recursively onself's inner selections and the substitution is also applied to the latter.- Parameters:
argname (str) -- The name of the argument to substitute.
replacement (subgrounds.query.Argument | list[subgrounds.query.Argument]) -- The argument(s) replacement
recurse (bool) -- Flag indicating whether or not the method should be run recursively. Defaults to True.
- Returns:
_description_
- Return type:
- prune_undefined(variables)#
Return a new
Selectioncontaining the subtree of the currentSelectionwhere all argumentInputValuesare defined, i.e.: each argument'sInputValueis either1) not of type
InputValue.Variableor 2) of typeInputValue.Variableand the variable name is contained invariables.- Parameters:
variables (Iterator[str]) -- An iterator over defined variables
- Returns:
A new pruned
Selectionobject- Return type:
subgrounds.query.Selection | None
- class subgrounds.query.Query(name: 'str | None' = None, selection: 'list[Selection]' = <factory>, variables: 'list[VariableDefinition]' = <factory>)#
- property graphql: str#
Returns a string containing a GraphQL query matching the current query
- Returns:
The string containing the GraphQL query
- iter()#
Returns an iterator over all
Selectionsof the selection tree of the currentQuery.
- iter_args()#
Returns an iterator over all
Argumentsof the selection tree of the currentQuery.
- iter_vardefs()#
Returns an iterator over all
VariableDefinitionsof the selection tree of the currentQuery.
- filter(predicate)#
Returns a new
Queryobject containing all selectionssthat satisfypredicate(s) == True.
- filter_args(predicate)#
Returns a new
Queryobject containing all selections argumentsargthat satisfypredicate(arg) == True.
- map(map_f, priority='self')#
- Applies the function
map_fto eachSelectionin the current Queryand returns a newQueryobject containing the resultingSelections.
- Applies the function
- map_args(map_f)#
- Applies the function
map_fto eachArgumentin the current Queryand returns a newQueryobject containing the resultingArguments.
- Applies the function
- add(other)#
Returns a new Query containing all selections in :attr:'self' along with the new selections in
other- Parameters:
self (Query) -- The query to which new selection(s) or query are to be added
other (subgrounds.query.Query | subgrounds.query.Selection | list[subgrounds.query.Selection]) -- The new selection(s) or query to be added to the query
- Returns:
A new Query objects containing all selections
- Return type:
- remove(other)#
Returns a new
Queryobject containing all selections inselfminus the subquery or selection(s) specified inother.Note:
otherdoes 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, [], [] )
- contains_selection(selection)#
Returns True i.f.f. the selection tree
selectionis present inquery.
- 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).
- class subgrounds.query.Fragment(name: 'str', type_: 'TypeRef.T', selection: 'list[Selection]' = <factory>, variables: 'list[VariableDefinition]' = <factory>)#
- class subgrounds.query.Document(url: 'str', query: 'Query', fragments: 'list[Fragment]' = <factory>, variables: 'dict[str, Any]' = <factory>)#
- map(map_f)#
Applies the function
map_fto eachSelectionin the currentDocumentand returns a newDocumentobject containing the resultingSelections.
- map_args(map_f)#
Applies the function
map_fto eachArgumentin the currentDocumentand returns a newDocumentobject containing the resultingArguments.
- prune_undefined(variables)#
Returns a new
Documentobject that contains the subset of the currentDocument's query containing only theSelectionsfor which all its arguments are defined (i.e.: either constants or variables invariables).
- class subgrounds.query.DocumentResponse(url: 'str', data: 'dict[str, Any]' = <factory>)#
- class subgrounds.query.DataRequest(documents: 'list[Document]' = <factory>)#
- class subgrounds.query.DataResponse(responses: 'list[DocumentResponse]' = <factory>)#
- 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_