Wednesday, September 19, 2018

API Design



https://graphql.org/
https://graphql-java.readthedocs.io/en/latest/index.html
https://github.com/graphql-java/graphql-java-http-example/blob/master/src/main/java/com/graphql/example/http/HttpMain.java


https://www.howtographql.com/graphql-java/0-introduction/

https://puncsky.com/hacking-the-software-engineer-interview#data-stores-todo

Public API Choices: JSON RPC vs. GraphQL vs. REST vs. gRPC 

Public API Choices

In summary, to choose a tool for the public API, API gateway, or BFF (Backend For Frontend) gateway, I prefer GraphQL for its features like tailing results, batching nested queries, performance tracing, and explicit caching.
JSON RPCGraphQLRESTgRPC
UsecasesEtherumGithub V2, Airbnb, Facebook BFF / API GatewaySwaggerHigh performance, Google, internal endpoints
Single Endpoint
Type System✅ as weak as JSON 
No uint64
✅ 
No uint64
✅ w/ Swagger 
No uint64
✅ 
has uint64
Tailored Results
Batch nested queries
VersioningSchema ExtensionYes, w/ v1/v2 route sField Numbers in protobuf
Error HandlingStructuredStructuredHTTP Status CodeStructured
Cross-platform
Playground UIGraphQL BinSwagger
Performance tracing?Apollo plugin??
cachingNo or HTTP cache controlApollo pluginHTTP cache controlNative support not yet. but still yes w/ HTTP cache control
ProblemLack of community support and toolchainBarrister IDL42.51 kb client-side bundle sizeUnstructured with multiple endpoints. awful portability.Grpc-web dev in progress140kb JS bundleNot all places support HTTP2

https://githubengineering.com/the-github-graphql-api/
GraphQL is a querying language developed by Facebook over the course of several years. In essence, you construct your request by defining the resources you want. You send this via a POST to a server, and the response matches the format of your request.


There are several other features of GraphQL that we hope to make available to clients, such as:
  • The ability to batch requests, where you can define dependencies between two separate queries and fetch data efficiently.
  • The ability to create subscriptions, where your client can receive new data when it becomes available.
  • The ability to defer data, where you choose to mark a part of your response as time-insensitive.


REST, RPC, GraphQL, WebHooks, and WebSockets are some of the most popular standards today

Update
Use PUT for replacing a resource and PATCH for partial updates for existing resources.

Create
Use POST for creating new resources.

codes in the 4XX range indicate a client-side error (like a missing required parameter or too many requests). Codes in the 5XX range indicate server-side errors.

SHOWING RELATIONSHIPS
A resource that exists only within another resource can be better represented as a subresource instead of a top-level resource in the URL. This makes the relationship clear for the developers using the API.

For instance, the GitHub API uses subresources to represent relationships in various APIs:

POST /repos/:owner/:repo/issues
Create an issue.
GET /repos/:owner/:repo/issues/:number
Retrieve an issue.
GET /repos/:owner/:repo/issues
List all issues.
PATCH /repos/:owner/:repo/issues/:number
Edit an issue.

NON-CRUD OPERATIONS
Render an action as part of a field of a resource. 
Treat an action like a subresource. The GitHub API uses this pattern for locking and unlocking an issue. PUT /repos/:owner/:repo/issues/:number/lock locks an issue.

Some operations, such as search, are even more difficult to fit in the REST paradigm. A typical practice in that case is to use just the action verb in the API URL. GET /search/code?q=:query: finds files in GitHub matching the given query.

RPC
a client executes a block of code on another server. Whereas REST is about resources, RPC is about actions. Clients typically pass a method name and arguments to a server and receive back JSON or XML.

The endpoints contain the name of the operation to be executed.

API calls are made with the HTTP verb that is most appropriate: GET for read-only requests and POST for others.
RPC style works great for APIs that expose a variety of actions that might have more nuances and complications than can be encapsulated with CRUD or for which there are side effects unrelated to the “resource” at hand. RPC-style APIs also accommodate complicated resource models or actions upon multiple types of resources.

GET /users.get?id=<id>

GraphQL allows clients to define the structure of the data required, and the server returns exactly that structure


a WebHook is simply a URL where API providers send a POST request when something happens

Designing for Real-Life Use Cases
Work Toward Consistency
You want your API to be intuitively consistent. That should be reflected in your endpoint names, input parameters, and output responses. Developers should be able to guess parts of your API even without reading the documentation. Unless you are making a significant version bump or large release, it’s best to work toward consistency when designing new aspects of an existing API.

Make Troubleshooting Easy
returning meaningful errors as well as by building tooling.
Logging on HTTP statuses, errors and their frequencies, and other request metadata is valuable to have, for both internal and external use

Building a versioning system is easier if it’s baked into the design at an early stage


Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts