Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
with:
python-version: ${{ matrix.python-version }}
enable-cache: true
cache-dependency-glob: "pyproject.toml"
cache-dependency-glob: "uv.lock"
- name: Start MongoDB ${{ matrix.mongodb-version }}
uses: supercharge/mongodb-github-action@1.12.1
with:
Expand All @@ -60,6 +60,6 @@ jobs:
with:
python-version: ${{ env.MAIN_PYTHON_VERSION }}
enable-cache: true
cache-dependency-glob: "pyproject.toml"
cache-dependency-glob: "uv.lock"
- name: Build wheel
run: uv build
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.11.10
rev: v0.15.20
hooks:
- id: ruff
args: [--fix]
- id: ruff-check
args: [ --fix ]
- id: ruff-format
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ clean:
@find . -name "*.swp" -delete
@find . -name "__pycache__" -delete

setup:
@uv sync --all-extras --all-groups

lint:
@uv run ruff check graphene_mongo
@uv run ruff format . --check
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@

html_context = {
"rtd_versions_url": "https://readthedocs.org/projects/graphene-mongo/versions/",
}
}
2 changes: 1 addition & 1 deletion examples/falcon_mongoengine/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ async def on_post(self, req, resp):
operation_name=body.get("operationName"),
)
errors = [{"message": str(e)} for e in result.errors] if result.errors else None
resp.media = {"data": result.data, "errors": errors}
resp.media = {"data": result.data, "errors": errors}
4 changes: 2 additions & 2 deletions examples/falcon_mongoengine/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
class MongoLifespan:
async def process_startup(self, scope, event):
mongoengine.connect("bookmarks_db")
await mongoengine.async_connect("bookmarks_db")
mongoengine.async_connect("bookmarks_db")

async def process_shutdown(self, scope, event):
mongoengine.disconnect()


app = falcon.asgi.App(middleware=[MongoLifespan()])
app.add_route("/graphql", GraphQLResource())
app.add_route("/graphql", GraphQLResource())
6 changes: 3 additions & 3 deletions examples/fastapi_mongoengine/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
@asynccontextmanager
async def lifespan(app: FastAPI):
mongoengine.connect("library_db")
await mongoengine.async_connect("library_db")
mongoengine.async_connect("library_db")
init_db()
yield
mongoengine.disconnect()
await mongoengine.disconnect()


app = FastAPI(title="Library GraphQL API", lifespan=lifespan)
Expand All @@ -52,4 +52,4 @@ async def graphql(request: Request):
operation_name=body.get("operationName"),
)
errors = [{"message": str(e)} for e in result.errors] if result.errors else None
return JSONResponse({"data": result.data, "errors": errors})
return JSONResponse({"data": result.data, "errors": errors})
3 changes: 1 addition & 2 deletions examples/fastapi_mongoengine/database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from models import Author, Book


Expand Down Expand Up @@ -37,4 +36,4 @@ def init_db():
genre="Philosophical Fiction",
author=kafka,
tags=["classic", "absurdism"],
).save()
).save()
2 changes: 1 addition & 1 deletion examples/fastapi_mongoengine/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class Book(mongoengine.Document):
author = mongoengine.ReferenceField(Author, reverse_delete_rule=mongoengine.NULLIFY)
tags = mongoengine.ListField(mongoengine.StringField())

meta = {"collection": "books"}
meta = {"collection": "books"}
4 changes: 3 additions & 1 deletion examples/fastapi_mongoengine/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Arguments:

async def mutate(self, info, title, published_year=None, genre=None, author_id=None, tags=None):
from graphql_relay import from_global_id

author = None
if author_id:
author = await AuthorModel.aobjects.get(pk=from_global_id(author_id)[1])
Expand All @@ -62,6 +63,7 @@ class Arguments:

async def mutate(self, info, id):
from graphql_relay import from_global_id

try:
book = await BookModel.aobjects.get(pk=from_global_id(id)[1])
await book.adelete()
Expand All @@ -81,4 +83,4 @@ class Query(graphene.ObjectType):
authors = AsyncMongoengineConnectionField(AuthorType)


schema = graphene.Schema(query=Query, mutation=Mutation, types=[AuthorType, BookType])
schema = graphene.Schema(query=Query, mutation=Mutation, types=[AuthorType, BookType])
5 changes: 3 additions & 2 deletions examples/fastapi_mongoengine/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import os

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

import mongoengine
Expand All @@ -15,11 +16,11 @@
async def setup():
gridfs.enable_gridfs_integration()
mongoengine.connect("library-test", host="mongomock://localhost")
await mongoengine.async_connect("library-test", host="mongomock://localhost")
mongoengine.async_connect("library-test", host="mongomock://localhost")
init_db()


@pytest.fixture
async def client():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as c:
yield c
yield c
50 changes: 28 additions & 22 deletions examples/fastapi_mongoengine/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

@pytest.mark.asyncio
async def test_query_all_books(client):
response = await client.post("/graphql", json={
"query": """
response = await client.post(
"/graphql",
json={
"query": """
query {
books {
edges {
Expand All @@ -17,7 +19,8 @@ async def test_query_all_books(client):
}
}
"""
})
},
)
assert response.status_code == 200
data = response.json()
assert not data["errors"]
Expand All @@ -28,9 +31,9 @@ async def test_query_all_books(client):

@pytest.mark.asyncio
async def test_query_books_paginated(client):
response = await client.post("/graphql", json={
"query": "{ books(first: 2) { edges { node { title } } } }"
})
response = await client.post(
"/graphql", json={"query": "{ books(first: 2) { edges { node { title } } } }"}
)
assert response.status_code == 200
data = response.json()
assert not data["errors"]
Expand All @@ -39,9 +42,9 @@ async def test_query_books_paginated(client):

@pytest.mark.asyncio
async def test_query_books_filter_genre(client):
response = await client.post("/graphql", json={
"query": '{ books(genre: "Dystopian") { edges { node { title } } } }'
})
response = await client.post(
"/graphql", json={"query": '{ books(genre: "Dystopian") { edges { node { title } } } }'}
)
assert response.status_code == 200
data = response.json()
assert not data["errors"]
Expand All @@ -53,9 +56,9 @@ async def test_query_books_filter_genre(client):

@pytest.mark.asyncio
async def test_query_authors(client):
response = await client.post("/graphql", json={
"query": "{ authors { edges { node { name nationality } } } }"
})
response = await client.post(
"/graphql", json={"query": "{ authors { edges { node { name nationality } } } }"}
)
assert response.status_code == 200
data = response.json()
assert not data["errors"]
Expand All @@ -67,29 +70,32 @@ async def test_query_authors(client):
@pytest.mark.asyncio
async def test_create_and_delete_book(client):
# Create
response = await client.post("/graphql", json={
"query": """
response = await client.post(
"/graphql",
json={
"query": """
mutation {
createBook(title: "Test Book", genre: "Fiction", publishedYear: 2024) {
book { title genre }
}
}
"""
})
},
)
assert response.status_code == 200
data = response.json()
assert not data["errors"]
assert data["data"]["createBook"]["book"]["title"] == "Test Book"

# Fetch ID for delete
response = await client.post("/graphql", json={
"query": '{ books(title: "Test Book") { edges { node { id title } } } }'
})
response = await client.post(
"/graphql", json={"query": '{ books(title: "Test Book") { edges { node { id title } } } }'}
)
book_id = response.json()["data"]["books"]["edges"][0]["node"]["id"]

# Delete
response = await client.post("/graphql", json={
"query": f'mutation {{ deleteBook(id: "{book_id}") {{ success }} }}'
})
response = await client.post(
"/graphql", json={"query": f'mutation {{ deleteBook(id: "{book_id}") {{ success }} }}'}
)
assert response.status_code == 200
assert response.json()["data"]["deleteBook"]["success"] is True
assert response.json()["data"]["deleteBook"]["success"] is True
2 changes: 1 addition & 1 deletion examples/flask_mongoengine/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ async def graphql_view():

if __name__ == "__main__":
init_db()
app.run()
app.run()
2 changes: 1 addition & 1 deletion graphene_mongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"AsyncMongoengineConnectionField",
"get_query_fields",
"get_select_related_paths",
]
]
4 changes: 1 addition & 3 deletions graphene_mongo/asynchronous/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,7 @@ async def default_resolver(self, _root, info, required_fields=None, resolved=Non
items = await _base_query.limit(limit)
has_next_page = (
(
len(
await _base_query.skip(skip + limit).only("id").limit(1).to_list()
)
len(await _base_query.skip(skip + limit).only("id").limit(1).to_list())
!= 0
)
if requires_page_info
Expand Down
Loading
Loading