0

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • Why isn’t the data in the message body? If you are using express gateway, can you post the relevant parts of gateway.config.yml? – James McLeod Jun 03 '21 at 07:08
  • @JamesMcLeod , I am using proxy-policy in express-gateway where I get the request with parameters in JSON format as mentioned above in the body . Now I am proxying it to Graphql server , In Graphql I am fetching the values from proxy-policy in express gateway and getting data from 2 services using those parameters as mentioned above and sending back combined response to proxy-policy in express-gateway. – Ishu Garg Jun 03 '21 at 08:29
  • What error are getting from graphql? – James McLeod Jun 03 '21 at 13:14
  • I am getting error in graphql that keys cant be string as I am calling graphql with this : http://localhost:1000/graphql/?query={getproducts("p1":"p1","p2":"p2","p3":"p3","p4":"p4","p5":"p5")} – Ishu Garg Jun 04 '21 at 04:07
  • That is correctly-formed JSON; if graphql is rejecting it, it is not really looking for strict JSON. – James McLeod Jun 06 '21 at 17:11

0 Answers0