sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

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

Open khairm opened this issue 3 years ago • 1 comments

First Check

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

Commit to Help

  • [X] 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

https://github.com/strawberry-graphql/strawberry/discussions/1183

Might be some code/additional context here.

khairm avatar Dec 16 '22 20:12 khairm

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⬤

ljluestc avatar Sep 03 '23 21:09 ljluestc