Skip to content

Latest commit

 

History

History
13 lines (7 loc) · 1.36 KB

api-layer.md

File metadata and controls

13 lines (7 loc) · 1.36 KB

📡 API Layer

Use a single instance of the API client

No matter if your application is consuming RESTful or GraphQL API, have a single instance of the API client that's been pre-configured and reused throughout the application. E.g have a single API client (axios / graphql-request / apollo-client) instance with pre-defined configuration.

API Client Example Code

Define and export request declarations

Instead of declaring API requests on the go, have them defined and exported separately. If it's a RESTful API a declaration would be a fetcher function that calls an endpoint. On the other hand, requests for GraphQL APIs are declared via queries and mutations that could be consumed by data fetching libraries such as react-query, apollo-client, urql, etc. This makes it easier to track which endpoints are defined and available in the application. You can also type the responses and infer them further for a good type safety of the data. You can also define and export corresponding API hooks from there.

API Request Declaration Example Code