@@ -6,14 +6,12 @@ Strawberry-sqlalchemy-mapper is the simplest way to implement autogenerated stra
6
6
7
7
- Instead of manually listing every column and relationship in a SQLAlchemy model, strawberry-sqlalchemy-mapper
8
8
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.
10
10
11
11
- Native support for most of SQLAlchemy's most common types.
12
-
13
12
- Extensible to arbitrary custom SQLAlchemy types.
14
-
13
+ - Automatic batching of queries, avoiding N+1 queries when getting relationships
15
14
- Support for SQLAlchemy >=1.4.x
16
-
17
15
- Lightweight and fast.
18
16
19
17
## Getting Started
@@ -31,6 +29,7 @@ class Employee(Base):
31
29
__tablename__ = ' employee'
32
30
id = Column(UUID , primary_key = True )
33
31
name = Column(String, nullable = False )
32
+ password_hash = Column(String, nullable = False )
34
33
```
35
34
36
35
Next, decorate a type with ` strawberry_sqlalchemy_mapper.type() `
@@ -43,13 +42,71 @@ and hybrid properties. For example:
43
42
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
44
43
@strawberry_sqlalchemy_mapper.type (models.Employee)
45
44
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 :
46
99
pass
47
100
```
48
101
49
102
Several examples to help you get started are provided in the ` examples ` directory.
50
103
51
104
## Limitations
52
105
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
+
53
110
Natively supports the following SQLAlchemy types:
54
111
55
112
``` python
0 commit comments