Skip to content

Developer always compare JSON-RPC with GraphQL, why not combine them together? #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
allmonday opened this issue Jul 25, 2024 · 0 comments

Comments

@allmonday
Copy link
Owner

allmonday commented Jul 25, 2024

From my point of view:

I replaced RESTful with RPC to illustrate my point better.

GraphQL plays pretty well on composing data with resolver and dataloader.

RPC (better with openapi) has a mature software ecosystem such as rate control, caching, monitoring etc.

and it has capable of generating clients from openapi.json

As a python / fastapi developer I got an idea one day that we can apply resolver and dataloader pattern into any class transforming library of a language which support typings

for example: pydantic.

so that we can declare complicated, deep, nest schema, and let resolver / dataloader handle the fetching process. (no worry about N+1 query).

and let RPC (openapi) ecosystem to generate client with types and method to clients.

I've setup a demo repo for this whole process, it includes:

  1. define basic schema
  2. compose complicated schema
  3. define resolver and dataloader, if a loader return value meets the basic schema, then it can be inherited and extended.
  4. exposing api to clients.
# 1. define base schemas
class Comment(BaseModel):
    id: int
    content: str
    user_id: int

class Blog(BaseModel):
    id: int
    title: str

class User(BaseModel):
    id: int
    name: str

# 2. inherit and extend from base schemas
class MyBlogSite(BaseModel):
    name: str
    blogs: list[MyBlog] = []
    async def resolve_blogs(self):
        return await get_blogs()

    comment_count: int = 0
    def post_comment_count(self):
        return sum([b.comment_count for b in self.blogs])

class MyBlog(Blog):
    comments: list[MyComment] = []
    def resolve_comments(self, loader=LoaderDepend(blog_to_comments_loader)):
        return loader.load(self.id)

    comment_count: int = 0
    def post_comment_count(self):
        return len(self.comments)

class MyComment(Comment):
    user: Optional[User] = None
    def resolve_user(self, loader=LoaderDepend(user_loader)):
        return loader.load(self.user_id)

the whole can be progressive:

image

repo: https://github.com/allmonday/pydantic-resolve-demo

this could be a simple alternative choice for API integration.

@allmonday allmonday changed the title Developer always compares RESTful and GraphQL, why not combine them together? Developer always compares RESTful (or RPC) and GraphQL, why not combine them together? Jul 26, 2024
@allmonday allmonday changed the title Developer always compares RESTful (or RPC) and GraphQL, why not combine them together? Developer always compares RESTful, RPC and GraphQL, why not combine them together? Jul 26, 2024
@allmonday allmonday changed the title Developer always compares RESTful, RPC and GraphQL, why not combine them together? Developer always compare RPC and GraphQL, why not combine them together? Jul 26, 2024
@allmonday allmonday changed the title Developer always compare RPC and GraphQL, why not combine them together? Developer always compare JSON-RPC with GraphQL, why not combine them together? Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant