This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
[PROPOSAL] Make Backbone work in-code, not from an OpenAPI Spec #22
Labels
feedback wanted
Opinions on addressing is requested
Hey everyone 👋 first off I want to share a thank you to everyone that has checked out the package and shown interest! We'll figure out Dart on the server best practices together! 💙
My Experience With Backbone
While the current iteration of backbone works, there are a few issues I've come across:
My Proposal
I would like to change backbone so that instead of writing an Open API spec and generating code from there, you write the Dart backend code, and it generates the client code and an Open API spec (for documentation). The code you write would look something like this:
The
RequestContext
would still contain a logger, the raw shelf request, userId, and dependency caching that backbone currently has. TheGetItemsResponse
andGetItemsRequest
objects would just be regular Dart objects that can be serialized (have afromJson
andtoJson
method)You would then use the
build_runner
package to generate your code. (dart run build_runner build
). It would generate a client library for calling your functions that would look something like this:Challenges to This Proposal
Objects Shared Between Frontend and Backend
Right now, backbone generates 3 packages:
However, if you're defining the shared objects by hand in Dart, then you would probably put them in your backend package. Thus, for the frontend to use them, it would have to depend on the backend in its
pubspec
, which I don't love. Of course you could put your functions objects in their own package when you write them, but I don't think backbone could enforce that.Mason
Right now, backbone uses Mason internally to generate the source code. It seems natural to use the Mason CLI to build your code (ie
mason make backbone
), and I would love to make it that easy. However, that would be tough because the configuration is stored as plain Dart code. Anyone who has ideas/experience merging thebuild_runner
package andmason
together, please share!RPC vs REST
This point gets a bit nerdy, so be prepared 🤓 😆
Background
Two of the bigger theories for designing a web API are Remote Procedure Call (RPC) and Representational State Transfer (REST). As I understand it, from a high level:
getItems
). They need to be documented because you'll need to know what methods are available to you and how to use them. Also, each function is generally specific to a single-use case./items
points to all theItem
s in the system. APOST
call to/items
would add a new item, whereas a 'GET' call to/items/3
would retrieve the item with id 3). In theory, they don't need to be documented because they follow standard HTTP protocols, and each route can be used for different use-cases because when you retrieve an entity, you get the entire thing.Here are some resources I've read/watched to better understand the topic:
The Issue
Right now, backbone uses an Open API spec, which doesn't dictate a style you should use to design your API; it simply allows all the HTTP protocols to be used. You as the developer gets to decide if you follow a REST or RPC pattern. With this change, backbone forces an RPC style API because it takes functions from the backend and exposes them to the frontend. You won't get any control over your routes or HTTP verbs or anything like that. Thus, I think it would be impossible to make REST APIs with backbone, only RPC style APIs.
Feedback Wanted
If you've made it this far, here are some questions I'd love opinions on:
Thank you again to the wonderful Dart community, and I look forward to your ideas 💡 💙 🚀
The text was updated successfully, but these errors were encountered: