tiangolo / sqlmodel

SQL databases in Python, designed for simplicity, compatibility, and robustness.

Home Page:https://sqlmodel.tiangolo.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Integration with strawberry (graphql queries) and pydastic (elastic search).

khairm opened this issue · comments

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

-

Description

As per title. Would be great if we had one model that could work with pydastic and strawberry queries. Maybe I'm naïve....

Wanted Solution

I would want execute strawberry and pydastic queries/insertions on a SQLModel.

Wanted Code

-

Alternatives

Asking the same in the the strawberry and pydastic repos.
Creating a new project to incorporate all 3....

Operating System

Windows

Operating System Details

Windows 11 home

SQLModel Version

0.0.8

Python Version

3.10

Additional Context

strawberry-graphql/strawberry#1183

Might be some code/additional context here.

from sqlmodel import SQLModel, Field
from strawberry import schema, ID

Define your SQLModel

class Item(SQLModel, table=True):
id: int = Field(default=None, primary_key=True)
name: str

Define Pydantic model for validation

class ItemPydantic(BaseModel):
name: str

Define Strawberry GraphQL schema

@Schema
class ItemSchema:
id: ID
name: str

Define Strawberry GraphQL query

@strawberry.type
class Query:
@strawberry.field
def get_item(self, id: int) -> ItemSchema:
item = get_item_by_id(id) # Implement this function
return ItemSchema(id=item.id, name=item.name)

Define Strawberry GraphQL mutation

@strawberry.type
class Mutation:
@strawberry.mutation
def create_item(self, name: str) -> ItemSchema:
item = create_item(name) # Implement this function
return ItemSchema(id=item.id, name=item.name)

Create Strawberry GraphQL⬤