0

I need to make a request from my node server, to a graphQL API. The graphQL API I am using takes the query as the URL's query params, not in the body of the request. It also does not accept POST requests. I am having an issue stringifying something like

const query = {
    student(id: studentId) {
      personal {
        address
        grade
        etc {morestuff}
      }
    }
  }

how do I stringify this to send it as queryParams? I have tried the querystring module querystring(query) but it returns something like student= and that's it. I have tried JSON.stringify and was also unsuccessful. I need the above, to look like:

query=%7B%0A%20%20%20%20student(id%3A%20studentId)%20%7B%0A%20%20%20%20%20%20personal%20%7B%0A%20%20%20%20%20%20%20%20address%0A%20%20%20%20%20%20%20%20grade%0A%20%20%20%20%20%20%20%20etc%20%7Bmorestuff%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D

(I used an online encoder/decoder for that!)

I know that there are GraphQL client libs out there but I am unsure if they are to be used server-to-server, I see them used from browser/clients. If I should be using a client lib, can you recommend one (a lightweight preferably) that supports GET requests (the last one I tried did not - it was called Prisma)

Any help is appreciated :)

1 Answer 1

1

this works for me, creating a function:

function encode(query) {
  return encodeURIComponent(query).replace(/'/g, "%27").replace(/"/g, "%22");
}

and passing:

const query = {
    student(id: studentId) {
      personal {
        address
        grade
        etc {morestuff}
      }
    }
  }

returns desired results. Hope this helps someone.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.