How to link API requests with request parameters to graphql server query running on another PORT

274 Views Asked by At

I have an API-Gateway running on some port. There is an API end point: fetch/products/. Request coming from this API end point with some request parameters needs to be sent to graphql server. How to dynamically send those JSON request parameters to graphql?

Server1.js

const typeDefs = `
  type Query {getproducts(p1: String!,p2: String!,p3: String!,p4: String!,p5:String!
   }
`}

const resolvers = {
  Query: {
    getproducts: async (_, {p1,p2,p3,p4,p5}) => {
         
      const response = await fetch(`http://service1.com/fetch/products/?p1=${p1}&p2=${p2}&p3=${p3}`);
      const data = await response.json();
      const response2 = await fetch(`http://service2.com/fetch/items/?p1=${p1}&p4=${p4}&p5=${p5}`);
      const data = await response2.json();
     return response1+response2;
}}};

const server = new ApolloServer({ typeDefs, resolvers });

server.listen({ port: 1000});  

api-gateway server file

Request from API-Gateway is made from different port:

/fetch/products/?p1=value1&p2=value2&p3=value3&p4=value4&p5=value5&p6=value6

#How to send this request to graphql server in this form:

http://localhost:1000/graphql/?query={getproducts(p1:"p1",p2:"p2",p3:"p3",p4:"p4",p5:"p5")}

When I created object from API-request and stringyfy it then i get keys also in string.

 http://localhost:1000/graphql/?query={getproducts("p1":"p1","p2":"p2","p3":"p3","p4":"p4","p5":"p5")} 
    

How to handle this? Or some other way to handle request coming from API-Gateway to graphql server with request parameter.

0

There are 0 best solutions below