How to forward extension from one server to middleware server
Asked Answered
M

1

10

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.

Modulate answered 22/12, 2017 at 7:19 Comment(0)
B
2

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
    };
}
Byrnes answered 5/1, 2018 at 2:42 Comment(3)
The : and => aren't necessary. The original example code is using the object literal shorthand for method definitions.Rupertruperta
@Rupertruperta I just ran across that in the documentation, thanks for pointing that out.Byrnes
@Byrnes I see the request but apiHeaders: [], additionalInfo: {}, are empty there.Modulate

© 2022 - 2024 — McMap. All rights reserved.