Skip to content

Commit 908c18f

Browse files
committed
Merge pull request #87 from graphql-python/sqlalchemy
Added SQLAlchemy support into graphene 💪
2 parents 0345fa4 + 0f5277c commit 908c18f

31 files changed

+1303
-5
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ install:
2525
if [ "$TEST_TYPE" = build ]; then
2626
pip install --download-cache $HOME/.cache/pip/ pytest pytest-cov coveralls six pytest-django django-filter
2727
pip install --download-cache $HOME/.cache/pip/ -e .[django]
28+
pip install --download-cache $HOME/.cache/pip/ -e .[sqlalchemy]
2829
pip install django==$DJANGO_VERSION
2930
python setup.py develop
3031
elif [ "$TEST_TYPE" = build_website ]; then

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- **Relay:** Graphene has builtin support for Relay
88
- **Django:** Automatic *Django model* mapping to Graphene Types. Check a fully working [Django](http://github.com/graphql-python/swapi-graphene) implementation
99

10+
Graphene also supports *SQLAlchemy*!
1011

1112
*What is supported in this Python version?* **Everything**: Interfaces, ObjectTypes, Scalars, Unions and Relay (Nodes, Connections), in addition to queries, mutations and subscriptions.
1213

@@ -18,8 +19,10 @@ For instaling graphene, just run this command in your shell
1819

1920
```bash
2021
pip install graphene
21-
# Or in case of need Django model support
22+
# In case of need Django model support
2223
pip install graphene[django]
24+
# Or in case of need SQLAlchemy support
25+
pip install graphene[sqlalchemy]
2326
```
2427

2528

@@ -59,6 +62,7 @@ If you want to learn even more, you can also check the following [examples](exam
5962
* **Basic Schema**: [Starwars example](examples/starwars)
6063
* **Relay Schema**: [Starwars Relay example](examples/starwars_relay)
6164
* **Django model mapping**: [Starwars Django example](examples/starwars_django)
65+
* **SQLAlchemy model mapping**: [Flask SQLAlchemy example](examples/flask_sqlalchemy)
6266

6367

6468
## Contributing

README.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ building GraphQL schemas/types fast and easily.
1212
`Django <http://github.com/graphql-python/swapi-graphene>`__
1313
implementation
1414

15+
Graphene also supports *SQLAlchemy*!
16+
1517
*What is supported in this Python version?* **Everything**: Interfaces,
1618
ObjectTypes, Scalars, Unions and Relay (Nodes, Connections), in addition
1719
to queries, mutations and subscriptions.
@@ -27,8 +29,10 @@ For instaling graphene, just run this command in your shell
2729
.. code:: bash
2830
2931
pip install graphene
30-
# Or in case of need Django model support
32+
# In case of need Django model support
3133
pip install graphene[django]
34+
# Or in case of need SQLAlchemy support
35+
pip install graphene[sqlalchemy]
3236
3337
Examples
3438
--------
@@ -70,6 +74,8 @@ If you want to learn even more, you can also check the following
7074
example <examples/starwars_relay>`__
7175
- **Django model mapping**: `Starwars Django
7276
example <examples/starwars_django>`__
77+
- **SQLAlchemy model mapping**: `Flask SQLAlchemy
78+
example <examples/flask_sqlalchemy>`__
7379

7480
Contributing
7581
------------

docs/config.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,10 @@ ga = "UA-12613282-7"
2323
"/docs/django/tutorial/",
2424
"/docs/django/filtering/",
2525
]
26+
27+
[docs.sqlalchemy]
28+
name = "SQLAlchemy"
29+
pages = [
30+
"/docs/sqlalchemy/tutorial/",
31+
"/docs/sqlalchemy/tips/",
32+
]

docs/css/graphiql.css

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
}
9898

9999
#graphiql-container .resultWrap {
100+
position: relative;
100101
display: -webkit-flex;
101102
display: flex;
102103
-webkit-flex-direction: column;
@@ -1010,6 +1011,52 @@ span.CodeMirror-selectedtext { background: none; }
10101011
background-position: right bottom;
10111012
width: 100%; height: 100%;
10121013
}
1014+
#graphiql-container .spinner-container {
1015+
position: absolute;
1016+
top: 50%;
1017+
height: 36px;
1018+
width: 36px;
1019+
left: 50%;
1020+
transform: translate(-50%, -50%);
1021+
z-index: 10;
1022+
}
1023+
1024+
#graphiql-container .spinner {
1025+
vertical-align: middle;
1026+
display: inline-block;
1027+
height: 24px;
1028+
width: 24px;
1029+
position: absolute;
1030+
-webkit-animation: rotation .6s infinite linear;
1031+
-moz-animation: rotation .6s infinite linear;
1032+
-o-animation: rotation .6s infinite linear;
1033+
animation: rotation .6s infinite linear;
1034+
border-left: 6px solid rgba(150, 150, 150, .15);
1035+
border-right: 6px solid rgba(150, 150, 150, .15);
1036+
border-bottom: 6px solid rgba(150, 150, 150, .15);
1037+
border-top: 6px solid rgba(150, 150, 150, .8);
1038+
border-radius: 100%;
1039+
}
1040+
1041+
@-webkit-keyframes rotation {
1042+
from { -webkit-transform: rotate(0deg); }
1043+
to { -webkit-transform: rotate(359deg); }
1044+
}
1045+
1046+
@-moz-keyframes rotation {
1047+
from { -moz-transform: rotate(0deg); }
1048+
to { -moz-transform: rotate(359deg); }
1049+
}
1050+
1051+
@-o-keyframes rotation {
1052+
from { -o-transform: rotate(0deg); }
1053+
to { -o-transform: rotate(359deg); }
1054+
}
1055+
1056+
@keyframes rotation {
1057+
from { transform: rotate(0deg); }
1058+
to { transform: rotate(359deg); }
1059+
}
10131060
.CodeMirror-hints {
10141061
background: white;
10151062
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.45);
@@ -1076,4 +1123,4 @@ li.CodeMirror-hint-active {
10761123
border-bottom: solid 1px #c0c0c0;
10771124
border-top: none;
10781125
margin-bottom: -1px;
1079-
}
1126+
}

docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"es6-promise": "^3.0.2",
1919
"extract-text-webpack-plugin": "^0.9.1",
2020
"gatsby": "^0.7.7",
21-
"graphiql": "^0.4.2",
21+
"graphiql": "^0.4.5",
2222
"graphql": "^0.4.13",
2323
"jeet": "^6.1.2",
2424
"lodash": "^3.10.1",

docs/pages/community.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Django integration:
2121
- **graphql-django-view**: [Source Code][5] - [PyPI package][6]
2222
- **django-graphiql**: [Source Code][7] - [PyPI package][8]
2323

24+
Flask integration:
25+
- **graphql-flask**: [Source Code][9] - [PyPI package][10]
26+
2427
## Other related projects
2528

2629
- [Flask GraphQL Demo](https://github.com/amitsaha/flask-graphql-demo) by [@echorand](https://twitter.com/echorand)
@@ -37,3 +40,5 @@ Django integration:
3740
[6]: https://pypi.python.org/pypi/graphql-django-view
3841
[7]: https://github.com/graphql-python/django-graphiql
3942
[8]: https://pypi.python.org/pypi/django-graphiql
43+
[9]: https://github.com/graphql-python/graphql-flask
44+
[10]: https://pypi.python.org/pypi/graphql-flask

docs/pages/docs/sqlalchemy/tips.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Tips
3+
description: Tips when SQLAlchemy in Graphene
4+
---
5+
6+
# Tips
7+
8+
## Querying
9+
10+
For make querying to the database work, there are two alternatives:
11+
12+
* Expose the db session when you create the `graphene.Schema`:
13+
14+
```python
15+
schema = graphene.Schema(session=session)
16+
```
17+
18+
* Create a query for the models.
19+
20+
```python
21+
Base = declarative_base()
22+
Base.query = db_session.query_property()
23+
24+
class MyModel(Base):
25+
# ...
26+
```
27+
28+
If you don't specify any, the following error will be displayed:
29+
30+
`A query in the model Base or a session in the schema is required for querying.`

0 commit comments

Comments
 (0)