Comments (5)
I have the same problem :/
I "solved" with a horrible solution using override in method didReceiveResponse
. I returned a Promise instead of TResult (using typescript) and in this promise, I got the content and return request and result
didReceiveResponse<TResult = any>(response: any, request: Request): Promise<any> {
const parent = super.didReceiveResponse.bind(this);
return new Promise(async (resolve) => {
const body = await parent<TResult>(response, request);
return resolve({
body,
req: request,
})
});
}
But there are several problem in this solution :/
Isnt possíble to override a syncronous method with an asyncronous method (this explains why I returned a Promise, instead of use an async override)
The generics for methods get, post, put, patch and delete have no sense anymore, because, in these contracts, the return is the generics.
const { req, body } = await this.get<MyType>('/uri'); // Error
// I changed to
const { req, body } = await this.get('/uri');
return body as MyType;
Too many problems, but works for me
I'm waiting for a better solution
from datasource-rest.
I refactored this and made a better solution. I created an interface like Axios response and overloaded the methods get, put, delete, patch and post.
// Interface with response, like axios
interface ResponseInterface<T = any> {
data: T;
status: number;
headers: any;
request?: any;
}
class BaseAPIDataSource extends RESTDataSource {
didReceiveResponse(response: any, request: Request): Promise<any> {
// I'm getting some metrics here to prometheus
return this.formatResponse(response, request);
}
async formatResponse(response: any, request: Request): Promise<ResponseInterface> {
if (response.status >= 500) {
await super.didReceiveResponse(response, request);
}
const data = await this.parseBody(response);
return {
data,
status: response.status,
headers: response.headers,
request,
};
}
// Now I'm overloading the parent methods only changing the type response
protected async get<TResult = any, R = ResponseInterface<TResult>>(
path: string,
params?: URLSearchParamsInit,
init?: RequestInit,
): Promise<R> {
return super.get(path, params, init);
}
protected async put<TResult = any, R = ResponseInterface<TResult>>(
path: string,
body?: Body,
init?: RequestInit,
): Promise<R> {
return super.put(path, body, init);
}
protected async post<TResult = any, R = ResponseInterface<TResult>>(
path: string,
body?: Body,
init?: RequestInit,
): Promise<R> {
return super.post(path, body, init);
}
protected async patch<TResult = any, R = ResponseInterface<TResult>>(
path: string,
body?: Body,
init?: RequestInit,
): Promise<R> {
return super.patch(path, body, init);
}
protected async delete<TResult = any, R = ResponseInterface<TResult>>(
path: string,
params?: URLSearchParamsInit,
init?: RequestInit,
): Promise<R> {
return super.delete(path, params, init);
}
}
The usage now is like Axios and the type validation works
const { data, headers, status } = await this.get<MyCustomType>('/my/path', { page: 1 });
console.log(data, headers, status);
from datasource-rest.
Does overriding parseBody (which is protected) help here?
class MyAPI extends RESTDataSource {
protected parseBody(response: Response): Promise<object | string> {
return super.parseBody(response).then(result => {
return augmentResultWithHeaders(result, response.headers);
});
}
}
Or is that tough because parseBody doesn't know which particular API it is implementing?
from datasource-rest.
I can highly recommend to switch to your own DataSource and use axios instead of using the RESTDataSource. Its giving you more flexibility in the long run.
from datasource-rest.
If you need a more flexible data source you could try https://github.com/StarpTech/apollo-datasource-http. It's also listed here https://www.apollographql.com/docs/apollo-server/data/data-sources/
from datasource-rest.
Related Issues (20)
- RESTDataSource no longer have access to all datasources through context HOT 2
- Document `contextValue` recipe for circular references in context object
- Posting FormData as body (for file uploads) not possible anymore HOT 7
- Forward all cache options to key value cache HOT 6
- Unable to connect to Localhost server HOT 1
- @apollo/datasource-rest willSendRequest param type missing `agent` HOT 7
- [apollo-datasource-rest] Handling 404 errors HOT 2
- Feature request - be able to tell if API was resolved by memory, cache, or source HOT 3
- RESTDataSource - body based cacheKeyFor POST request HOT 3
- Feature request: More control over readResponseAndWriteToCache method HOT 4
- Why the introduction of response.clone() in HTTPCache class? HOT 4
- Refine `Content-Type` negotiation
- Add url as an argument to the RESTDataSource.didEncounterError() function
- Make errorFromResponse parameters optional
- Parse blob data HOT 1
- RequestDeduplicationPolicy is not exported in datasource-rest index.ts
- Fix CacheOptions Related Types HOT 1
- Typescript project build failing due to missing type declarations HOT 1
- Unable to prevent reading from the Cache HOT 3
- Question about cacheOptions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datasource-rest.