how to attach request and response objects to apollo server micro context object - apollo-server

I was wondering how I might be able to add the request and response objects to the apollo-server-micro's context. When I try the following, I get an error message saying that the resolver needs to return an object:
new ApolloServer({
context: ({req,res}) => ({req,res})
})
Thank you

Related

How to provide own follow redirect strategy?

I'm interacting with a webservice which on POST request answers with 302 containing address to created resource in the location header. To access the created resource I've to make a GET request to the provided location.
I want reactor.netty.http.client.HttpClient to handle the redirect flow for me.
This is my configuration:
import reactor.netty.http.client.HttpClient;
...
...
var nettyHttpClient = HttpClient.create()
.compress(true)
.followRedirect(true);
With the above configuration, the client will use same HTTP method for the redirected request as it did for the first request.
Given my use-case, is there a way to provide my own redirect strategy to the client for 3xx responses?
You could handle raw response and handle http status 302 with custom logic
var response = nettyHttpClient.post()
.uri("/test")
.response()
.flatMap(res -> {
if (res.status().equals(HttpResponseStatus.FOUND)) {
return nettyHttpClient.get()
.uri(res.responseHeaders().get(HttpHeaders.LOCATION))
.response();
}
return Mono.just(res);
});

How to get uri from apollo client object

Context: I'm working with micro front-end, and I want to access the uri of the apollo client. I'm currently passing the whole client object and I don't want to pass also the uri each time.
I defined my ApolloClient object like this:
new ApolloClient({
...
link: ApolloLink.from([errorLink, new HttpLink({ uri: process.env.BACKEND_URL })]),
});
How can I access the uri from the client object ?
const client = useApolloClient();
const uri = client.???
the uri is a field on the object, so you should be able use the Dot property accessor: object.property to obtain the URI. You can learn more about the HTTP Link constructor in the Apollo Docs

Capture raw axios request from AWS Lambda

I have code that calls a vendor API to do a formdata upload of a file by axios from inside an AWS Lambda. The call returns a 400 error. If I run the code locally using the same node version v14 it works. I want to capture both raw requests and compare them for differences. How do I capture both raw requests? I've tried using ngrok and pipedream but they don't show the raw but decode the request and the file.
let response = null;
try {
const newFile = fs.createReadStream(doc);
const formData = new FormData();
formData.append("file", newFile);
formData.append("url", url);
const headers = {
Authorization: "Bearer " + token,
...formData.getHeaders(),
};
console.log("Headers: ", headers);
response = await axios.post(`${APIBASE}/file/FileUpload`, formData, {
headers,
});
console.log("file upload response", response);
} catch (err) {
console.log("fileupload error at API", err);
}
You might be able to just use a custom request interceptor and interrogate at the requests that way.
https://axios-http.com/docs/interceptors
You're not able to capture the request on the network level, as this is totally controlled by AWS. Maybe there's a way to do this when running in a VPC, but I don't think so.
You could simply use a tool such as axios debug logger to print out all of the request and response contents (including headers etc) before the request is made/after the response has arrived. This might provide some more information as to where things are going wrong.
As to the cause of the problem, it is difficult to help you there since you haven't shared the error message nor do we know anything about the API you're trying to call.
There are multiple ways to debug
axios debug logger .
AWS cloud watch where you can see all the logs. you can capture the request
and response.
Use postman to call the prod lambda endpoint and verify the response.

How to consume other GraphQL API from Apollo Server implementation?

I have an Apollo Server implementation in which consume other REST APIs, I need to know, how can I use another GraphQL API inside a resolver?
I expect an Apollo Server implementation that works as an API Gateway to consume other APIs (REST or GraphQL)
A GraphQL request is made like most other REST calls, with POST with application/json header. It still hits an endpoint, passes data, and provides a body with the query. You don't need a fancy client like Apollo, but you will need to know the implementation;
A query parameter also needs to be passed as a string.
Here is an example call using axios in javascript:
const data = await axios.post(GRAPHQL_URL, {
query: `
getUser(userID: 1234){
id
name
age
}
`,
}, {
headers: {
'Content-Type': 'application/json'
}
})

Spring + Angular: How to parse ResponseEntity in angular?

I'm using Spring Boot to create an API that needs to be consumed in Angular 4. Spring and Angular are on different ports.
The problem is that Spring's ResponseEntity raises an error in Angular.
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ResponseEntity getFlow(#PathVariable int id) {
Flow flow = flowService.findById(id);
return new ResponseEntity(flow, HttpStatus.FOUND);
}
Now, I can perfectly use Postman to test the API and it works.
But when I make a request from Angular, it returns an error:
Strangely, it returns an error alongside the requested object.
Now, the cause of the problem is that the Spring Boot application returns a ResponseEntity and not a normal object (like String), and Angular doesn't know how to interpret it. If the controller returns just a Flow object, it works.
How can it be solved using ResponseEntity? Or, how else can I send the object alongside the HTTP status code?
Also, in #RequestMapping put produces = "application/json", and in get request in angular, add http options :
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json',
'Content-Type': 'application/json'
})
};
So your get request looks like this:
this.http.get(url, httpOptions)
As per the document mentioned here
https://docs.angularjs.org/api/ng/service/$http
A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Any response status code outside of that range is considered an error status and will result in the error callback being called. Also, status codes less than -1 are normalized to zero. -1 usually means the request was aborted, e.g. using a config.timeout. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the outcome (success or error) will be determined by the final response status code.
As you are sending an instance of ResponseEntity(HttpStatus.Found) whose Http status code is 302 which doesnt fall under the success range thats why error callback is called.
Try returning the content like this
return new ResponseEntity(flow, HttpStatus.OK);

Resources