Skip to content
This repository has been archived by the owner on May 9, 2023. It is now read-only.

Commit

Permalink
fix: unused pagination arguments causing errors
Browse files Browse the repository at this point in the history
subgrounds.pagination:
- Fix bug where, on the first page, some pagination args were unused
  even though they were included in the query, causing errors in some
  cases

subgrounds.subgrounds:
- Add ability to specify directory where schemas should be saved (if
  `save_schema=True`)
  • Loading branch information
cvauclair committed Jun 3, 2022
1 parent cb63c3d commit 641c20e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 24 deletions.
62 changes: 42 additions & 20 deletions subgrounds/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,29 +449,49 @@ def args(self) -> dict:
dict: _description_
"""
if self.is_leaf:
return {
# `first`
f'first{self.page_node.node_idx}': self.page_node.first_value - self.queried_entities
if self.page_node.first_value - self.queried_entities < PAGE_SIZE
else PAGE_SIZE,

# `skip`
f'skip{self.page_node.node_idx}': self.page_node.skip_value if self.page_count == 0 else 0,

# `filter`
f'lastOrderingValue{self.page_node.node_idx}': self.filter_value
}
if self.filter_value is None:
return {
# `first`
f'first{self.page_node.node_idx}': self.page_node.first_value - self.queried_entities
if self.page_node.first_value - self.queried_entities < PAGE_SIZE
else PAGE_SIZE,

# `skip`
f'skip{self.page_node.node_idx}': self.page_node.skip_value if self.page_count == 0 else 0,
}
else:
return {
# `first`
f'first{self.page_node.node_idx}': self.page_node.first_value - self.queried_entities
if self.page_node.first_value - self.queried_entities < PAGE_SIZE
else PAGE_SIZE,

# `skip`
f'skip{self.page_node.node_idx}': self.page_node.skip_value if self.page_count == 0 else 0,

# `filter`
f'lastOrderingValue{self.page_node.node_idx}': self.filter_value
}
else:
args = {
# `first`
f'first{self.page_node.node_idx}': 1,
if self.filter_value is None:
args = {
# `first`
f'first{self.page_node.node_idx}': 1,

# `skip`
f'skip{self.page_node.node_idx}': self.page_node.skip_value if self.page_count == 0 else 0,
}
else:
args = {
# `first`
f'first{self.page_node.node_idx}': 1,

# `skip`
f'skip{self.page_node.node_idx}': self.page_node.skip_value if self.page_count == 0 else 0,
# `skip`
f'skip{self.page_node.node_idx}': self.page_node.skip_value if self.page_count == 0 else 0,

# `filter`
f'lastOrderingValue{self.page_node.node_idx}': self.filter_value
}
# `filter`
f'lastOrderingValue{self.page_node.node_idx}': self.filter_value
}

inner_args = self.inner[self.inner_idx].args()
return args | inner_args
Expand Down Expand Up @@ -508,6 +528,8 @@ def f(keyval):
match value:
case InputValue.Variable(name) if name in pagination_args and pagination_args[name] is None:
return None
case InputValue.Variable(name) if name not in pagination_args:
return None
case _:
return (key, value)

Expand Down
12 changes: 8 additions & 4 deletions subgrounds/subgrounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Subgrounds:
global_transforms: list[RequestTransform] = field(default_factory=lambda: DEFAULT_GLOBAL_TRANSFORMS)
subgraphs: dict[str, Subgraph] = field(default_factory=dict)

def load_subgraph(self, url: str, save_schema: bool = False) -> Subgraph:
def load_subgraph(self, url: str, save_schema: bool = False, cache_dir: str = 'schemas/') -> Subgraph:
"""Performs introspection on the provided GraphQL API ``url`` to get the
schema, stores the schema if ``save_schema`` is ``True`` and returns a
generated class representing the subgraph with all its entities.
Expand All @@ -45,21 +45,23 @@ def load_subgraph(self, url: str, save_schema: bool = False) -> Subgraph:
Returns:
Subgraph: A generated class representing the subgraph and its entities
"""
filename = url.split("/")[-1] + ".json"
filename = cache_dir + url.split("/")[-1] + ".json"
if os.path.isfile(filename):
with open(filename) as f:
schema = json.load(f)
else:
schema = client.get_schema(url)
if save_schema:
if cache_dir != '.' and not os.path.exists(cache_dir):
os.makedirs(cache_dir)
with open(filename, mode="w") as f:
json.dump(schema, f)

sg = Subgraph(url, mk_schema(schema), DEFAULT_SUBGRAPH_TRANSFORMS)
self.subgraphs[url] = sg
return sg

def load_api(self, url: str, save_schema: bool = False) -> Subgraph:
def load_api(self, url: str, save_schema: bool = False, cache_dir: str = 'schemas/') -> Subgraph:
"""Performs introspection on the provided GraphQL API ``url`` to get the
schema, stores the schema if ``save_schema`` is ``True`` and returns a
generated class representing the GraphQL endpoint with all its entities.
Expand All @@ -72,13 +74,15 @@ def load_api(self, url: str, save_schema: bool = False) -> Subgraph:
Returns:
Subgraph: A generated class representing the subgraph and its entities
"""
filename = url.split("/")[-1] + ".json"
filename = cache_dir + url.split("/")[-1] + ".json"
if os.path.isfile(filename):
with open(filename) as f:
schema = json.load(f)
else:
schema = client.get_schema(url)
if save_schema:
if cache_dir != '.' and not os.path.exists(cache_dir):
os.makedirs(cache_dir)
with open(filename, mode="w") as f:
json.dump(schema, f)

Expand Down

0 comments on commit 641c20e

Please sign in to comment.