Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to forward extension from one server to middleware server

I am using remote schema stitching on my middlware server. I am able to get the schema remotely on middleware server, defined my route like this on middleware server.

app.use('/graphql', graphqlHTTP((request,res) => {
 const startTime = Date.now();
 return {
   schema: remoteSchema
   graphiql: false,
   extensions({ document, variables, operationName, result }) {
     return {
       // here I am not getting extensions which I have on my another server as below.
       console.log(res); // this does not have additional info and response headers
       console.log(result); // this only has response against the query
     }
   };
})); 

I am getting the result of the query in the result but not getting response headers and additional info which is a part of extension which I am adding on my other server where resolvers are there.

{
    "data": {
        "records": {
            "record": [{
                    "id": 1,
                },
                {
                    "id": 2,
                }
            ],
        },
        "additionalInfo": {}
    },
    "extensions": {
        "info": {}
    }
}

What could be the issue? This is how I am adding response headers and additional info on my another server in extensions. I debug below code where extension data is available. This is not being passed to middleware server.

extensions({ document, variables, operationName, result }) {
   result.data.additionalInfo = res.additionalInfo;
   // extension to write api headers in response
   var headerObj = {};
   res.apiHeaders.forEach(element => {
     merge(headerObj, element);
   });
   result.headerObj = headerObj;
   return {
      information: headerObj
   };
}

My application flow is that I am calling middleware route then another server route using remote schema stitching. I want extension which I am adding on another server should be forward to my middleware server in the response.

like image 227
N Sharma Avatar asked Dec 22 '17 07:12

N Sharma


1 Answers

have you console.log() the request? I'm pretty sure anything that you get in the extension function regarding headers that you want to output would be in the request, because it's middleware on the server, the response is something you are going to modify before sending it to the next function or back to the client.

extensions({ document, variables, operationName, result }) {
    // console.log the request object to check the header information from the request.
    console.log(request);
    return {
        // This will fill the information key with all the headers in the request.
        information: reaquest.header
    };
}
like image 151
RickyM Avatar answered Nov 15 '22 05:11

RickyM