Skip to content

Commit

Permalink
Merge branch '0xPlaygrounds:main' into Evan-Kim2028/main
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMochan authored Nov 8, 2023
2 parents de9905e + 5820110 commit cfdbd7e
Show file tree
Hide file tree
Showing 11 changed files with 1,030 additions and 642 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Trigger Docs Rebuild

on:
workflow_call:
push:
branches: [ main ]
paths:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ jobs:
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
poetry run semantic-release publish -v DEBUG -D commit_author="github-actions <[email protected]>"
rebuild-docs:
uses: ./.github/workflows/build-docs.yml
secrets: inherit
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

<!--next-version-placeholder-->

## v1.8.0 (2023-10-10)

### Feature

* Pydantic upgrade to v2, maintaining V1 API internally ([#45](https://github.com/0xPlaygrounds/subgrounds/issues/45)) ([`25570ae`](https://github.com/0xPlaygrounds/subgrounds/commit/25570ae1f4d293f46312f195d5344473ad63e8fa))
* Pandas upgrade to v2 ([#44](https://github.com/0xPlaygrounds/subgrounds/issues/44)) ([`5d1670b`](https://github.com/0xPlaygrounds/subgrounds/commit/5d1670bc1077c0f81fc2426e512118006f07f81b))

## v1.7.1 (2023-09-22)

### Fix

* Nested filters ([#40](https://github.com/0xPlaygrounds/subgrounds/issues/40)) ([`3f3f575`](https://github.com/0xPlaygrounds/subgrounds/commit/3f3f5753bf5dba9ac39664156b76f81697055a23))

### Documentation

* Update CHANGELOG.md ([`7ef0876`](https://github.com/0xPlaygrounds/subgrounds/commit/7ef08764cd27478ab9797f2823a823cbbe095ec2))

## v1.7.0 (2023-09-05)

### Feature
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
[![Github Codepsaces](https://img.shields.io/badge/Github-Codespaces-24292f.svg?logo=Github)](https://codespaces.new/0xPlaygrounds/subgrounds-template?quickstart=1)

<!-- start elevator-pitch -->
An intuitive python library for interfacing with Subgraphs.
An intuitive Python library for interfacing with subgraphs and GraphQL.

## Features
- **Simple**: Leverage a Pythonic API to easily build queries and transformations without the need for raw GraphQL manipulation.
Expand Down Expand Up @@ -44,20 +44,20 @@ Subgrounds also comes bundled with extra modules that may require extra librarie
>>> sg = Subgrounds()

>>> # Load
>>> aave_v2 = sg.load_subgraph('https://api.thegraph.com/subgraphs/name/messari/aave-v2-ethereum')
>>> aave_v2 = sg.load_subgraph("https://api.thegraph.com/subgraphs/name/messari/aave-v2-ethereum")

>>> # Construct the query
>>> latest = aave_v2.Query.markets(
orderBy=aave_v2.Market.totalValueLockedUSD,
orderDirection='desc',
first=5,
)
>>> latest_markets = aave_v2.Query.markets(
... orderBy=aave_v2.Market.totalValueLockedUSD,
... orderDirection='desc',
... first=5,
... )

>>> # Return query to a dataframe
>>> sg.query_df([
latest.name,
latest.totalValueLockedUSD,
])
... latest_markets.name,
... latest_markets.totalValueLockedUSD,
... ])
markets_name markets_totalValueLockedUSD
0 Aave interest bearing STETH 1.522178e+09
1 Aave interest bearing WETH 1.221299e+09
Expand Down
1,506 changes: 905 additions & 601 deletions poetry.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "subgrounds"
version = "1.7.0"
version = "1.8.0"
description = "A Pythonic data access layer for applications querying data from The Graph Network."
authors = [
"cvauclair <[email protected]>",
Expand All @@ -13,9 +13,9 @@ keywords = ["graph", "subgrounds", "graphql", "subgraph"]

[tool.poetry.dependencies]
python = "^3.10"
pandas = "^1.4.2"
pandas = ">=1.4.2, ^2.1"
pipe = "^2.0"
pydantic = "^1.10.2"
pydantic = "^2.0"
dash = { version = "^2.3.1", optional = true }
plotly = { version = "^5.14.1", optional = true }
httpx = { extras = ["http2"], version = "^0.24.1" }
Expand Down
2 changes: 1 addition & 1 deletion subgrounds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from subgrounds.client import AsyncSubgrounds, Subgrounds
from subgrounds.subgraph import FieldPath, Subgraph, SyntheticField

__version__ = "1.7.0"
__version__ = "1.8.0"

__all__ = [
"AsyncSubgrounds",
Expand Down
4 changes: 2 additions & 2 deletions subgrounds/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from typing import Annotated, Literal

from pipe import map, where
from pydantic import BaseModel as PydanticBaseModel
from pydantic import Field, root_validator
from pydantic.v1 import BaseModel as PydanticBaseModel
from pydantic.v1 import Field, root_validator

from .errors import SchemaError

Expand Down
59 changes: 42 additions & 17 deletions subgrounds/subgraph/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@
import warnings
from dataclasses import dataclass
from enum import Enum, auto
from functools import reduce
from typing import TYPE_CHECKING, Any

from subgrounds.schema import TypeMeta
from subgrounds.utils import merge

if TYPE_CHECKING:
from subgrounds.subgraph.fieldpath import FieldPath
from subgrounds.subgraph import FieldPath

logger = logging.getLogger("subgrounds")
warnings.simplefilter("default")


@dataclass
class Filter:
field: TypeMeta.FieldMeta
fields: list[TypeMeta.FieldMeta]
op: Filter.Operator
value: Any

Expand All @@ -29,32 +31,55 @@ class Operator(Enum):
GT = auto()
GTE = auto()

@staticmethod
def mk_filter(fpath: FieldPath, op: Filter.Operator, value: Any) -> Filter:
match fpath._leaf:
case TypeMeta.FieldMeta() as fmeta:
return Filter(fmeta, op, value)
@classmethod
def mk_filter(cls, fpath: FieldPath, op: Filter.Operator, value: Any) -> Filter:
match fpath._path:
case [(_, TypeMeta.FieldMeta()), *_] as path: # assert atleast len of 1
return cls([tup[1] for tup in path], op, value)
case _:
raise TypeError(
f"Cannot create filter on FieldPath {fpath}: not a native field!"
)

@property
def name(self):
def leaf_name(self):
leaf = self.fields[-1].name
match self.op:
case Filter.Operator.EQ:
return self.field.name
return leaf
case Filter.Operator.NEQ:
return f"{self.field.name}_not"
return f"{leaf}_not"
case Filter.Operator.LT:
return f"{self.field.name}_lt"
return f"{leaf}_lt"
case Filter.Operator.GT:
return f"{self.field.name}_gt"
return f"{leaf}_gt"
case Filter.Operator.LTE:
return f"{self.field.name}_lte"
return f"{leaf}_lte"
case Filter.Operator.GTE:
return f"{self.field.name}_gte"
return f"{leaf}_gte"

@staticmethod
def to_dict(filters: list[Filter]) -> dict[str, Any]:
return {f.name: f.value for f in filters}
@classmethod
def to_dict(cls, filters: list[Filter]) -> dict[str, Any]:
"""Converts a series of filters into a single dict.
Each filter represents a single key, value pair where the key is generated based
on the operation (via :func:`~subgrounds.subgraph.filter.Filter.leaf_name`).
For nested filters, this base dict is further stacked under sub-objects:
```py
{"outer_": {"outer2_": {"inner_op": "value"}}}
```
"""

return reduce(
merge,
(
reduce(
lambda x, y: {f"{y.name}_": x},
filter.fields[:-1],
{filter.leaf_name: filter.value},
)
for filter in filters
),
{},
)
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,11 @@ def subgraph(schema):
return Subgraph("www.abc.xyz/graphql", schema)


@pytest.fixture
def filter(subgraph):
return subgraph.Pair.token0.symbol == "CRV"


@pytest.fixture
def subgraph_diff_url(schema):
return Subgraph("www.foo.xyz/graphql", schema)
Expand Down
48 changes: 40 additions & 8 deletions tests/test_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2124,20 +2124,52 @@ def test_synthetic_field_path_3(subgraph: Subgraph):

def test_filter_1(subgraph: Subgraph):
expected = Filter(
TypeMeta.FieldMeta(
name="reserveUSD",
description="",
args=[],
type=TypeRef.Named(name="BigDecimal", kind="SCALAR"),
),
[
TypeMeta.FieldMeta(
name="reserveUSD",
description="",
args=[],
type=TypeRef.Named(name="BigDecimal", kind="SCALAR"),
)
],
Filter.Operator.GT,
100,
)

filter_ = subgraph.Pair.reserveUSD > 100
actual = subgraph.Pair.reserveUSD > 100

with fieldpath_test_mode():
assert actual == expected


def test_filter_2(subgraph: Subgraph):
expected = Filter(
[
TypeMeta.FieldMeta(
name="token0",
args=[],
type=TypeRef.Named(name="Token", kind="OBJECT"),
),
TypeMeta.FieldMeta(
name="symbol",
args=[],
type=TypeRef.Named(name="String", kind="SCALAR"),
),
],
Filter.Operator.EQ,
"CRV",
)

actual = subgraph.Pair.token0.symbol == "CRV"

with fieldpath_test_mode():
assert filter_ == expected
assert actual == expected


def test_filter_to_dict(filter: Filter):
expected = {'token0_': {'symbol': 'CRV'}}

assert Filter.to_dict([filter]) == expected


def test_field_path_args_1(subgraph: Subgraph):
Expand Down

0 comments on commit cfdbd7e

Please sign in to comment.