Skip to content

Commit 596a72b

Browse files
committed
Add SQLAlchemyList and SQLAlchemyMutation types
This brings default support for fields filtering and ordering on queries and mutations.
1 parent ecd9a91 commit 596a72b

File tree

7 files changed

+588
-27
lines changed

7 files changed

+588
-27
lines changed

graphene_sqlalchemy/mutations.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from graphene_sqlalchemy.types import SQLAlchemyMutation
2+
3+
4+
def create(of_type):
5+
class CreateModel(SQLAlchemyMutation):
6+
class Meta:
7+
model = of_type._meta.model
8+
create = True
9+
Output = of_type
10+
return CreateModel.Field()
11+
12+
13+
def update(of_type):
14+
class UpdateModel(SQLAlchemyMutation):
15+
class Meta:
16+
model = of_type._meta.model
17+
Output = of_type
18+
return UpdateModel.Field()
19+
20+
21+
def delete(of_type):
22+
class DeleteModel(SQLAlchemyMutation):
23+
class Meta:
24+
model = of_type._meta.model
25+
delete = True
26+
Output = of_type
27+
return DeleteModel.Field()

graphene_sqlalchemy/tests/conftest.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pytest
2+
from sqlalchemy import create_engine
3+
4+
5+
@pytest.fixture(scope='session')
6+
def db():
7+
return create_engine('sqlite:///test_sqlalchemy.sqlite3')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import graphene
2+
import pytest
3+
from sqlalchemy.orm import scoped_session, sessionmaker
4+
5+
from graphene_sqlalchemy import SQLAlchemyObjectType
6+
from graphene_sqlalchemy.mutations import create
7+
from graphene_sqlalchemy.registry import reset_global_registry
8+
from graphene_sqlalchemy.tests.models import Base, Reporter
9+
10+
11+
@pytest.yield_fixture(scope='function')
12+
def session(db):
13+
reset_global_registry()
14+
connection = db.engine.connect()
15+
transaction = connection.begin()
16+
Base.metadata.create_all(connection)
17+
18+
# options = dict(bind=connection, binds={})
19+
session_factory = sessionmaker(bind=connection)
20+
session = scoped_session(session_factory)
21+
22+
yield session
23+
24+
# Finalize test here
25+
transaction.rollback()
26+
connection.close()
27+
session.remove()
28+
29+
30+
def test_should_create_with_create_fields(session):
31+
class ReporterType(SQLAlchemyObjectType):
32+
class Meta:
33+
model = Reporter
34+
35+
class Query(graphene.ObjectType):
36+
reporters = graphene.List(ReporterType)
37+
38+
def resolve_reporters(self, *args, **kwargs):
39+
return session.query(Reporter)
40+
41+
class Mutation(graphene.ObjectType):
42+
createReporter = create(ReporterType)
43+
44+
query = '''
45+
query ReporterQuery {
46+
reporters {
47+
firstName,
48+
lastName,
49+
email
50+
}
51+
}
52+
'''
53+
expected = {
54+
'reporters': []
55+
}
56+
schema = graphene.Schema(query=Query, mutation=Mutation)
57+
result = schema.execute(query)
58+
assert not result.errors
59+
assert result.data == expected
60+
61+
query = '''
62+
mutation createReporter{
63+
createReporter (firstName: "ABC", lastName: "def") {
64+
firstName,
65+
lastName,
66+
email
67+
}
68+
}
69+
'''
70+
expected = {
71+
'createReporter': {
72+
'firstName': 'ABC',
73+
'lastName': 'def',
74+
'email': None,
75+
}
76+
}
77+
schema = graphene.Schema(query=Query, mutation=Mutation)
78+
result = schema.execute(query, context_value={'session': session})
79+
assert not result.errors
80+
assert result.data == expected

0 commit comments

Comments
 (0)