Skip to content

Commit 14af560

Browse files
committed
Merge branch 'master' of github.com:expedock/strawberry-sqlalchemy-mapper
2 parents b545d02 + 0a02aba commit 14af560

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

README.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ Strawberry-sqlalchemy-mapper is the simplest way to implement autogenerated stra
66

77
- Instead of manually listing every column and relationship in a SQLAlchemy model, strawberry-sqlalchemy-mapper
88
lets you decorate a class declaration and it will automatically generate the necessary strawberry fields
9-
for all columns and relationships in the given model.
9+
for all columns and relationships (subject to the limitations below) in the given model.
1010

1111
- Native support for most of SQLAlchemy's most common types.
12-
1312
- Extensible to arbitrary custom SQLAlchemy types.
14-
13+
- Automatic batching of queries, avoiding N+1 queries when getting relationships
1514
- Support for SQLAlchemy >=1.4.x
16-
1715
- Lightweight and fast.
1816

1917
## Getting Started
@@ -31,6 +29,7 @@ class Employee(Base):
3129
__tablename__ = 'employee'
3230
id = Column(UUID, primary_key=True)
3331
name = Column(String, nullable=False)
32+
password_hash = Column(String, nullable=False)
3433
```
3534

3635
Next, decorate a type with `strawberry_sqlalchemy_mapper.type()`
@@ -43,13 +42,71 @@ and hybrid properties. For example:
4342
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
4443
@strawberry_sqlalchemy_mapper.type(models.Employee)
4544
class Employee:
45+
__exclude__ = ["password_hash"]
46+
47+
48+
# context is expected to have an instance of StrawberrySQLAlchemyLoader
49+
class CustomGraphQLView(GraphQLView):
50+
def get_context(self):
51+
return {
52+
"sqlalchemy_loader": StrawberrySQLAlchemyLoader(),
53+
}
54+
55+
# call finalize() before using the schema:
56+
strawberry_sqlalchemy_mapper.finalize()
57+
# only needed if you have polymorphic types
58+
additional_types = list(strawberry_sqlalchemy_mapper.mapped_types.values())
59+
schema = strawberry.Schema(
60+
query=Query,
61+
mutation=Mutation,
62+
extensions=extensions,
63+
types=additional_types,
64+
)
65+
```
66+
67+
Roots of polymorphic hierarchies are also expected to be registered via
68+
`strawberry_sqlalchemy_mapper.interface()`, and its concrete type and
69+
its descendants are expected to inherit from the interface:
70+
71+
```python
72+
class Book(Model):
73+
id = Column(UUID, primary_key=True)
74+
75+
class Novel(Book):
76+
pass
77+
78+
class ShortStory(Book):
79+
pass
80+
81+
82+
# in another file
83+
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
84+
85+
@strawberry_sqlalchemy_mapper.interface(models.Book)
86+
class BookInterface:
87+
pass
88+
89+
@strawberry_sqlalchemy_mapper.type(models.Book)
90+
class Book:
91+
pass
92+
93+
@strawberry_sqlalchemy_mapper.type(models.Novel)
94+
class Novel:
95+
pass
96+
97+
@strawberry_sqlalchemy_mapper.type(models.ShortStory)
98+
class ShortStory:
4699
pass
47100
```
48101

49102
Several examples to help you get started are provided in the `examples` directory.
50103

51104
## Limitations
52105

106+
SQLAlchemy Models -> Strawberry Types and Interfaces are expected to have a consistent
107+
(customizable) naming convention. These can be configured by passing `model_to_type_name`
108+
and `model_to_interface_name` when constructing the mapper.
109+
53110
Natively supports the following SQLAlchemy types:
54111

55112
```python

0 commit comments

Comments
 (0)