Skip to content

Commit

Permalink
v0.4.0 - Async BugFixes & Optimisations (#225)
Browse files Browse the repository at this point in the history
* Fix(register): updated register_enum function support custom classes

 Use isinstance instead of type to support instances of enum class in addition to exact same type.

* Support Async

* Convert: get_node to async

* Fix: Test Case

* Bug Fix on connection_resolver

* Support Async

* Fix: connection_resolver

* Support Async, Test Case Added

* Bump Version : 0.4.0

* Add asgiref to dependency

* Add pytest-asyncio to dependency

* Fix: Test Case

* Update: README.md

* Blocking Threaded to Async

* Bug Fix: Argument Parser

* (Deprecated) get_resolver to wrap_resolve

* Fix: Count Performance

* Fix: Queryset Check

* Optimise: Lazy Reference Resolver

* Fix: DeprecationWarnings

* Fix: Queryset evaluation

* feat: add support for mongo date field

* refactor: change logging level

* fix(connection_field): respects non_filter_fields when argument name is same as field name

* refact: sync to async wrapper

* chore: add ruff formatting

* fix: inconsistent has_next_page when pk is not sorted

* fix: self.get_queryset not being async

* fix[pipeline]: add ruff remove flake8

* refact: filter connection

* refact: has_next_page logic

* fix: has_next_page using limit

* fix: ci change flake8 to ruff

* fix: convert | syntax to union type for backward compatibility

* chore: add support for python 12

* chore: drop support for python 3.7, add support python 3.12

* refact: add typings

* fix: ruff lint

* chore: add setup tools for python 12 to fix mongomock ImportError: No module named pkg_resources

---------

Co-authored-by: Adithyan Jothir <adithyan@strollby.com>
Co-authored-by: Adarsh D <adarshdevamritham@gmail.com>
Co-authored-by: Arun Suresh Kumar <asuresh960@gmail.com>
Co-authored-by: M Aswin Kishore <mak26.work@gmail.com>
Co-authored-by: Edwin Jose George <edwin@strollby.com>
Co-authored-by: Roshan R Chandar <roshanr2001@gmail.com>
  • Loading branch information
7 people authored Nov 25, 2023
1 parent 615adb3 commit 2a094fc
Show file tree
Hide file tree
Showing 38 changed files with 3,172 additions and 919 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python: ["3.7", "3.8", "3.9", "3.10", "3.11"]
python: ["3.8", "3.9", "3.10", "3.11","3.12"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python }}
- name: Lint with flake8
- name: Lint with ruff
run: |
python -m pip install flake8
python -m pip install ruff
make lint
- name: Install dependencies
run: |
Expand Down
10 changes: 4 additions & 6 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ jobs:
build:
strategy:
matrix:
python: ["3.11"]
python: ["3.12"]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python }}
- name: Install dependencies
- name: Lint with ruff
run: |
python -m pip install --upgrade pip
pip install flake8
- name: Lint with flake8
run: make lint
python -m pip install ruff
make lint
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ clean:
@find . -name "__pycache__" -delete

lint:
@flake8 graphene_mongo --count --show-source --statistics
@ruff check graphene_mongo
@ruff format . --check

test: clean
pytest graphene_mongo/tests --cov=graphene_mongo --cov-report=html --cov-report=term
Expand Down
54 changes: 49 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

A [Mongoengine](https://mongoengine-odm.readthedocs.io/) integration for [Graphene](http://graphene-python.org/).


## Installation

For installing graphene-mongo, just run this command in your shell
Expand All @@ -23,13 +22,14 @@ Here is a simple Mongoengine model as `models.py`:
from mongoengine import Document
from mongoengine.fields import StringField


class User(Document):
meta = {'collection': 'user'}
first_name = StringField(required=True)
last_name = StringField(required=True)
```

To create a GraphQL schema for it you simply have to write the following:
To create a GraphQL schema and sync executor; for it you simply have to write the following:

```python
import graphene
Expand All @@ -38,15 +38,60 @@ from graphene_mongo import MongoengineObjectType

from .models import User as UserModel


class User(MongoengineObjectType):
class Meta:
model = UserModel


class Query(graphene.ObjectType):
users = graphene.List(User)

def resolve_users(self, info):
return list(UserModel.objects.all())
return list(UserModel.objects.all())


schema = graphene.Schema(query=Query)
```

Then you can simply query the schema:

```python
query = '''
query {
users {
firstName,
lastName
}
}
'''
result = await schema.execute(query)
```

To create a GraphQL schema and async executor; for it you simply have to write the following:

```python
import graphene

from graphene_mongo import AsyncMongoengineObjectType
from asgiref.sync import sync_to_async
from concurrent.futures import ThreadPoolExecutor

from .models import User as UserModel


class User(AsyncMongoengineObjectType):
class Meta:
model = UserModel


class Query(graphene.ObjectType):
users = graphene.List(User)

async def resolve_users(self, info):
return await sync_to_async(list, thread_sensitive=False,
executor=ThreadPoolExecutor())(UserModel.objects.all())


schema = graphene.Schema(query=Query)
```
Expand All @@ -71,7 +116,6 @@ To learn more check out the following [examples](examples/):
* [Django MongoEngine example](examples/django_mongoengine)
* [Falcon MongoEngine example](examples/falcon_mongoengine)


## Contributing

After cloning this repo, ensure dependencies are installed by running:
Expand Down
20 changes: 8 additions & 12 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,18 @@
master_doc = "index"

# General information about the project.
project = u"Graphene Mongo"
copyright = u"Graphene 2018"
author = u"Abaw Chen"
project = "Graphene Mongo"
copyright = "Graphene 2018"
author = "Abaw Chen"

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = u"0.1"
version = "0.1"
# The full version, including alpha/beta/rc tags.
release = u"0.1.2"
release = "0.1.2"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down Expand Up @@ -275,9 +275,7 @@
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, "Graphene.tex", u"Graphene Documentation", u"Syrus Akbary", "manual")
]
latex_documents = [(master_doc, "Graphene.tex", "Graphene Documentation", "Syrus Akbary", "manual")]

# The name of an image file (relative to this directory) to place at the top of
# the title page.
Expand Down Expand Up @@ -316,9 +314,7 @@

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, "graphene_django", u"Graphene Django Documentation", [author], 1)
]
man_pages = [(master_doc, "graphene_django", "Graphene Django Documentation", [author], 1)]

# If true, show URL addresses after external links.
#
Expand All @@ -334,7 +330,7 @@
(
master_doc,
"Graphene-Django",
u"Graphene Django Documentation",
"Graphene Django Documentation",
author,
"Graphene Django",
"One line description of project.",
Expand Down
4 changes: 1 addition & 3 deletions examples/django_mongoengine/bike/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@
from graphene_django.views import GraphQLView

urlpatterns = [
path(
"graphql", csrf_exempt(GraphQLView.as_view(graphiql=True)), name="graphql-query"
)
path("graphql", csrf_exempt(GraphQLView.as_view(graphiql=True)), name="graphql-query")
]
4 changes: 1 addition & 3 deletions examples/django_mongoengine/bike_catalog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"
},
{"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"},
{"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"},
{"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"},
{"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"},
Expand Down
4 changes: 1 addition & 3 deletions examples/django_mongoengine/bike_catalog/settings_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from .settings import * # flake8: noqa

mongoengine.connect(
"graphene-mongo-test", host="mongomock://localhost", alias="default"
)
mongoengine.connect("graphene-mongo-test", host="mongomock://localhost", alias="default")
4 changes: 1 addition & 3 deletions examples/falcon_mongoengine/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from .schema import schema


def set_graphql_allow_header(
req: falcon.Request, resp: falcon.Response, resource: object
):
def set_graphql_allow_header(req: falcon.Request, resp: falcon.Response, resource: object):
resp.set_header("Allow", "GET, POST, OPTIONS")


Expand Down
22 changes: 13 additions & 9 deletions examples/falcon_mongoengine/tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import mongoengine
from graphene.test import Client

from examples.falcon_mongoengine.schema import schema
from .fixtures import fixtures_data

mongoengine.connect(
"graphene-mongo-test", host="mongomock://localhost", alias="default"
)
mongoengine.connect("graphene-mongo-test", host="mongomock://localhost", alias="default")


def test_category_last_1_item_query(fixtures_data):
Expand All @@ -23,7 +22,16 @@ def test_category_last_1_item_query(fixtures_data):

expected = {
"data": {
"categories": {"edges": [{"node": {"name": "Work", "color": "#1769ff"}}]}
"categories": {
"edges": [
{
"node": {
"name": "Work",
"color": "#1769ff",
}
}
]
}
}
}

Expand All @@ -45,11 +53,7 @@ def test_category_filter_item_query(fixtures_data):
}
}"""

expected = {
"data": {
"categories": {"edges": [{"node": {"name": "Work", "color": "#1769ff"}}]}
}
}
expected = {"data": {"categories": {"edges": [{"node": {"name": "Work", "color": "#1769ff"}}]}}}

client = Client(schema)
result = client.execute(query)
Expand Down
5 changes: 2 additions & 3 deletions examples/falcon_mongoengine/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import graphene
from graphene import relay
from graphene_mongo import MongoengineObjectType

from .models import Category, Bookmark
from graphene_mongo import MongoengineObjectType
from .models import Bookmark, Category


class CategoryType(MongoengineObjectType):
Expand Down
4 changes: 1 addition & 3 deletions examples/flask_mongoengine/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@
}
}""".strip()

app.add_url_rule(
"/graphql", view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True)
)
app.add_url_rule("/graphql", view_func=GraphQLView.as_view("graphql", schema=schema, graphiql=True))

if __name__ == "__main__":
init_db()
Expand Down
4 changes: 0 additions & 4 deletions examples/flask_mongoengine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@


class Department(Document):

meta = {"collection": "department"}
name = StringField()


class Role(Document):

meta = {"collection": "role"}
name = StringField()


class Task(EmbeddedDocument):

name = StringField()
deadline = DateTimeField(default=datetime.now)


class Employee(Document):

meta = {"collection": "employee"}
name = StringField()
hired_on = DateTimeField(default=datetime.now)
Expand Down
13 changes: 10 additions & 3 deletions examples/flask_mongoengine/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import graphene
from graphene.relay import Node
from graphene_mongo.tests.nodes import PlayerNode, ReporterNode

from graphene_mongo import MongoengineConnectionField, MongoengineObjectType
from .models import Department as DepartmentModel
Expand All @@ -20,7 +19,11 @@ class Meta:
model = RoleModel
interfaces = (Node,)
filter_fields = {
'name': ['exact', 'icontains', 'istartswith']
"name": [
"exact",
"icontains",
"istartswith",
]
}


Expand All @@ -35,7 +38,11 @@ class Meta:
model = EmployeeModel
interfaces = (Node,)
filter_fields = {
'name': ['exact', 'icontains', 'istartswith']
"name": [
"exact",
"icontains",
"istartswith",
]
}


Expand Down
8 changes: 6 additions & 2 deletions graphene_mongo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
from .fields import MongoengineConnectionField
from .fields_async import AsyncMongoengineConnectionField

from .types import MongoengineObjectType, MongoengineInputType, MongoengineInterfaceType
from .types_async import AsyncMongoengineObjectType

__version__ = "0.1.1"

__all__ = [
"__version__",
"MongoengineObjectType",
"AsyncMongoengineObjectType",
"MongoengineInputType",
"MongoengineInterfaceType",
"MongoengineConnectionField"
]
"MongoengineConnectionField",
"AsyncMongoengineConnectionField",
]
Loading

0 comments on commit 2a094fc

Please sign in to comment.