sqlmodel-graphql 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,312 @@
1
+ Metadata-Version: 2.4
2
+ Name: sqlmodel-graphql
3
+ Version: 0.1.0
4
+ Summary: GraphQL SDL generation and query optimization for SQLModel
5
+ Project-URL: Homepage, https://github.com/allmonday/sqlmodel-graphql
6
+ Project-URL: Repository, https://github.com/allmonday/sqlmodel-graphql
7
+ Author-email: tangkikodo <allmonday@126.com>
8
+ License: MIT
9
+ License-File: LICENSE
10
+ Keywords: fastapi,graphql,sqlalchemy,sqlmodel
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: License :: OSI Approved :: MIT License
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Requires-Python: >=3.10
19
+ Requires-Dist: aiosqlite>=0.22.1
20
+ Requires-Dist: graphql-core>=3.2.0
21
+ Requires-Dist: greenlet>=3.3.2
22
+ Requires-Dist: sqlmodel>=0.0.14
23
+ Provides-Extra: demo
24
+ Requires-Dist: aiosqlite>=0.19.0; extra == 'demo'
25
+ Requires-Dist: fastapi>=0.100.0; extra == 'demo'
26
+ Requires-Dist: uvicorn>=0.23.0; extra == 'demo'
27
+ Provides-Extra: dev
28
+ Requires-Dist: aiosqlite>=0.19.0; extra == 'dev'
29
+ Requires-Dist: mypy>=1.0.0; extra == 'dev'
30
+ Requires-Dist: pytest-asyncio>=0.21.0; extra == 'dev'
31
+ Requires-Dist: pytest>=7.0.0; extra == 'dev'
32
+ Requires-Dist: ruff>=0.1.0; extra == 'dev'
33
+ Description-Content-Type: text/markdown
34
+
35
+ # SQLModel GraphQL
36
+
37
+ GraphQL SDL generation and query optimization for SQLModel.
38
+
39
+ ## Features
40
+
41
+ - **Automatic SDL Generation**: Generate GraphQL schema from SQLModel classes
42
+ - **@query/@mutation Decorators**: Mark methods as GraphQL operations
43
+ - **Query Optimization**: Parse GraphQL queries to generate optimized SQLAlchemy queries
44
+ - **N+1 Prevention**: Automatic `selectinload` and `load_only` generation
45
+
46
+ ## Installation
47
+
48
+ ```bash
49
+ pip install sqlmodel-graphql
50
+ ```
51
+
52
+ Or with uv:
53
+
54
+ ```bash
55
+ uv add sqlmodel-graphql
56
+ ```
57
+
58
+ ## Quick Start
59
+
60
+ ### 1. Define Your Models
61
+
62
+ ```python
63
+ from typing import Optional
64
+ from sqlmodel import SQLModel, Field, Relationship, select
65
+ from sqlmodel_graphql import query, mutation, QueryMeta
66
+
67
+ class User(SQLModel, table=True):
68
+ id: Optional[int] = Field(default=None, primary_key=True)
69
+ name: str
70
+ email: str
71
+ posts: list["Post"] = Relationship(back_populates="author")
72
+
73
+ @query(name='users')
74
+ async def get_all(cls, limit: int = 10, query_meta: Optional[QueryMeta] = None) -> list['User']:
75
+ """Get all users with optional query optimization."""
76
+ from demo.database import async_session
77
+
78
+ async with async_session() as session:
79
+ stmt = select(cls).limit(limit)
80
+ if query_meta:
81
+ # Apply optimization: only load requested fields and relationships
82
+ stmt = stmt.options(*query_meta.to_options(cls))
83
+ result = await session.exec(stmt)
84
+ return list(result.all())
85
+
86
+ @query(name='user')
87
+ async def get_by_id(cls, id: int, query_meta: Optional[QueryMeta] = None) -> Optional['User']:
88
+ return await fetch_user(id, query_meta)
89
+
90
+ @mutation(name='createUser')
91
+ async def create(cls, name: str, email: str) -> 'User':
92
+ return await create_user(name, email)
93
+
94
+ class Post(SQLModel, table=True):
95
+ id: Optional[int] = Field(default=None, primary_key=True)
96
+ title: str
97
+ content: str = ""
98
+ author_id: int = Field(foreign_key="user.id")
99
+ author: User = Relationship(back_populates="posts")
100
+ ```
101
+
102
+ ### 2. Generate GraphQL SDL
103
+
104
+ ```python
105
+ from sqlmodel_graphql import SDLGenerator
106
+
107
+ generator = SDLGenerator([User, Post])
108
+ sdl = generator.generate()
109
+ print(sdl)
110
+ ```
111
+
112
+ Output:
113
+
114
+ ```graphql
115
+ type User {
116
+ id: Int
117
+ name: String!
118
+ email: String!
119
+ posts: [Post!]!
120
+ }
121
+
122
+ type Post {
123
+ id: Int
124
+ title: String!
125
+ content: String!
126
+ author_id: Int!
127
+ author: User!
128
+ }
129
+
130
+ type Query {
131
+ users(limit: Int): [User!]!
132
+ user(id: Int!): User
133
+ }
134
+
135
+ type Mutation {
136
+ createUser(name: String!, email: String!): User!
137
+ }
138
+ ```
139
+
140
+ ### 3. Execute Queries with GraphQLHandler
141
+
142
+ ```python
143
+ from sqlmodel_graphql import GraphQLHandler
144
+
145
+ # Option 1: Auto-discover all entities (recommended)
146
+ handler = GraphQLHandler()
147
+
148
+ # Option 2: Use a custom base class
149
+ class Base(SQLModel):
150
+ pass
151
+
152
+ class User(Base, table=True):
153
+ ...
154
+
155
+ handler = GraphQLHandler(base=Base)
156
+
157
+ # Option 3: Explicitly specify entities
158
+ handler = GraphQLHandler(entities=[User, Post])
159
+
160
+ # Execute a GraphQL query
161
+ result = await handler.execute("""
162
+ {
163
+ users(limit: 5) {
164
+ id
165
+ name
166
+ posts {
167
+ title
168
+ comments {
169
+ content
170
+ author {
171
+ name
172
+ }
173
+ }
174
+ }
175
+ }
176
+ }
177
+ """)
178
+
179
+ # Result includes nested relationships automatically:
180
+ # {
181
+ # "data": {
182
+ # "users": [
183
+ # {
184
+ # "id": 1,
185
+ # "name": "Alice",
186
+ # "posts": [
187
+ # {
188
+ # "title": "Hello World",
189
+ # "comments": [
190
+ # {"content": "Great post!", "author": {"name": "Bob"}}
191
+ # ]
192
+ # }
193
+ # ]
194
+ # }
195
+ # ]
196
+ # }
197
+ # }
198
+ ```
199
+
200
+ ## How It Works
201
+
202
+ ```
203
+ GraphQL Query QueryMeta
204
+ ───────────── ─────────
205
+ { users { QueryMeta(
206
+ id fields=[FieldSelection('id'), FieldSelection('name')],
207
+ name relationships={
208
+ posts { 'posts': RelationshipSelection(
209
+ title fields=[FieldSelection('title')]
210
+ } }
211
+ } }
212
+ )
213
+
214
+ query_meta.to_options(User)
215
+
216
+ select(User).options(
217
+ load_only(User.id, User.name),
218
+ selectinload(User.posts).options(load_only(Post.title))
219
+ )
220
+ ```
221
+
222
+ ### Query Optimization Flow
223
+
224
+ 1. **GraphQLHandler** receives the query
225
+ 2. **QueryParser** parses the selection set into **QueryMeta**
226
+ 3. **QueryMeta** is injected into your `@query` method as `query_meta` parameter
227
+ 4. **query_meta.to_options(entity)** generates SQLAlchemy options:
228
+ - `load_only()` for requested scalar fields
229
+ - `selectinload()` for requested relationships
230
+ 5. Database query only fetches what's needed, preventing N+1 problems
231
+
232
+ ## API Reference
233
+
234
+ ### `@query(name=None, description=None)`
235
+
236
+ Mark a method as a GraphQL query.
237
+
238
+ ```python
239
+ @query(name='users', description='Get all users')
240
+ async def get_all(cls, limit: int = 10, query_meta: Optional[QueryMeta] = None) -> list['User']:
241
+ ...
242
+ ```
243
+
244
+ ### `@mutation(name=None, description=None)`
245
+
246
+ Mark a method as a GraphQL mutation.
247
+
248
+ ```python
249
+ @mutation(name='createUser')
250
+ async def create(cls, name: str, email: str) -> 'User':
251
+ ...
252
+ ```
253
+
254
+ ### `SDLGenerator(entities)`
255
+
256
+ Generate GraphQL SDL from SQLModel classes.
257
+
258
+ ```python
259
+ generator = SDLGenerator([User, Post])
260
+ sdl = generator.generate()
261
+ ```
262
+
263
+ ### `GraphQLHandler(entities=None, base=None)`
264
+
265
+ Execute GraphQL queries against SQLModel entities with auto-discovery support.
266
+
267
+ ```python
268
+ # Auto-discover from SQLModel (default)
269
+ handler = GraphQLHandler()
270
+
271
+ # Use custom base class
272
+ handler = GraphQLHandler(base=MyBase)
273
+
274
+ # Explicit entities
275
+ handler = GraphQLHandler(entities=[User, Post])
276
+
277
+ result = await handler.execute("{ users { id name } }")
278
+ ```
279
+
280
+ **Auto-Discovery Features:**
281
+ - Automatically finds all SQLModel subclasses with `@query/@mutation` decorators
282
+ - Includes all related entities through Relationship fields
283
+ - Supports custom base classes for better organization
284
+ - Recursive discovery of nested relationships
285
+
286
+ ### `QueryParser()`
287
+
288
+ Parse GraphQL queries to QueryMeta.
289
+
290
+ ```python
291
+ parser = QueryParser()
292
+ metas = parser.parse("{ users { id name } }")
293
+ # metas['users'] -> QueryMeta(fields=[...], relationships={...})
294
+ ```
295
+
296
+ ### `QueryMeta`
297
+
298
+ Metadata extracted from GraphQL selection set.
299
+
300
+ ```python
301
+ @dataclass
302
+ class QueryMeta:
303
+ fields: list[FieldSelection]
304
+ relationships: dict[str, RelationshipSelection]
305
+
306
+ def to_options(self, entity: type[SQLModel]) -> list[Any]:
307
+ """Convert to SQLAlchemy options for query optimization."""
308
+ ```
309
+
310
+ ## License
311
+
312
+ MIT License
@@ -0,0 +1,12 @@
1
+ sqlmodel_graphql/__init__.py,sha256=KnCsvqbtz135cOKactcb1PplY-w5ARKVGD7B5oWPWBo,1667
2
+ sqlmodel_graphql/decorator.py,sha256=z8mwzNfbYBnoHFM_pkD3tsjdJFORwpf8LwoekZg6C1g,4458
3
+ sqlmodel_graphql/handler.py,sha256=ZCs8nYqoAlKpdMlVBgVxe-_vnLFOTGtAfk9M7wWWHG0,14364
4
+ sqlmodel_graphql/introspection.py,sha256=MNCokC28WD5vcvIXYlq5IpVejpy6dEyWznZdy12C58U,13835
5
+ sqlmodel_graphql/query_parser.py,sha256=SLC03j-k1YL41-8z6gYgvCQQir7uGwG0F6cpsqSl8K0,3148
6
+ sqlmodel_graphql/sdl_generator.py,sha256=QQzCAl2g-WRU5XV1Y0dne22kwReH2iURFM6ShXTJ008,9105
7
+ sqlmodel_graphql/type_converter.py,sha256=xuT2-BZ_kOt9EbosYZLYdk5_beU-CBRXAL1AVb_0-zU,5492
8
+ sqlmodel_graphql/types.py,sha256=Xtus2Xws8wCKT8rMZsElzKtKY-R3LEY-_UmtuBGNMOM,5015
9
+ sqlmodel_graphql-0.1.0.dist-info/METADATA,sha256=2MYdAZB7RqlYnenmV_KalOAqHZohTfaGE1XN-KVcheQ,7983
10
+ sqlmodel_graphql-0.1.0.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
11
+ sqlmodel_graphql-0.1.0.dist-info/licenses/LICENSE,sha256=lNVRR3y_bFVoFKuK2JM8N4sFaj3m-7j29kvL3olFi5Y,1067
12
+ sqlmodel_graphql-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.29.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 tangkikodo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.