Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contextual control of auto select depth #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions sgqlc/operation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,7 @@
__all__ = ('Operation',)

from collections import OrderedDict
from contextlib import contextmanager

from ..types import BaseTypeWithTypename, Union, ContainerType, ArgDict, \
global_schema
Expand All @@ -1539,6 +1540,17 @@
}


@contextmanager
def select_depth(depth):
global DEFAULT_AUTO_SELECT_DEPTH
old_depth = DEFAULT_AUTO_SELECT_DEPTH
DEFAULT_AUTO_SELECT_DEPTH = depth
try:
yield
finally:
DEFAULT_AUTO_SELECT_DEPTH = old_depth


class Selection:
'''Select a field with in a container type.

Expand Down Expand Up @@ -1619,8 +1631,9 @@ def __init__(self, alias, field, args, typename=None):
self.__selection_list = SelectionList(field.type)

def __get_selections_or_auto_select__(
self, auto_select_depth=DEFAULT_AUTO_SELECT_DEPTH,
self, auto_select_depth=None,
typename=None):
auto_select_depth = auto_select_depth or DEFAULT_AUTO_SELECT_DEPTH
selections = self.__selection_list
if selections is None or selections:
return selections
Expand Down Expand Up @@ -1657,8 +1670,9 @@ def __fields__(self, *names, **names_and_args):
self.__selection_list.__fields__(*names, **names_and_args)

def __to_graphql__(self, indent=0, indent_string=' ',
auto_select_depth=DEFAULT_AUTO_SELECT_DEPTH,
auto_select_depth=None,
typename=None):
auto_select_depth = auto_select_depth or DEFAULT_AUTO_SELECT_DEPTH
prefix = indent_string * indent

alias = self.__alias__ + ': ' if self.__alias__ else ''
Expand Down Expand Up @@ -1961,8 +1975,9 @@ def __bytes__(self):
return bytes(self.__to_graphql__(indent_string=''), 'utf-8')

def __to_graphql__(self, indent=0, indent_string=' ',
auto_select_depth=DEFAULT_AUTO_SELECT_DEPTH,
auto_select_depth=None,
typename=None):
auto_select_depth = auto_select_depth or DEFAULT_AUTO_SELECT_DEPTH
prefix = indent_string * indent
next_indent = indent + 1

Expand Down Expand Up @@ -2210,8 +2225,9 @@ def __fields_add_names_and_args(self, names_and_args, typename):

class InlineFragmentSelectionList(SelectionList):
def __to_graphql__(self, indent=0, indent_string=' ',
auto_select_depth=DEFAULT_AUTO_SELECT_DEPTH,
auto_select_depth=None,
typename=None):
auto_select_depth = auto_select_depth or DEFAULT_AUTO_SELECT_DEPTH
selection = SelectionList.__to_graphql__(
self, indent, indent_string, auto_select_depth, typename)
return '... on %s %s' % (self.__type__, selection)
Expand All @@ -2232,8 +2248,9 @@ def __spread__(self):
return '...%s' % (self.__name,)

def __to_graphql__(self, indent=0, indent_string=' ',
auto_select_depth=DEFAULT_AUTO_SELECT_DEPTH,
auto_select_depth=None,
typename=None):
auto_select_depth = auto_select_depth or DEFAULT_AUTO_SELECT_DEPTH
selection = SelectionList.__to_graphql__(
self, indent, indent_string, auto_select_depth, typename)
return 'fragment %s on %s %s' % (
Expand Down Expand Up @@ -2456,8 +2473,9 @@ def _get_kind(self):
)

def __to_graphql__(self, indent=0, indent_string=' ',
auto_select_depth=DEFAULT_AUTO_SELECT_DEPTH,
auto_select_depth=None,
typename=None):
auto_select_depth = auto_select_depth or DEFAULT_AUTO_SELECT_DEPTH
prefix = indent_string * indent
kind = self.__kind
name = ' ' + self.__name if self.__name else ''
Expand Down