Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL post request in axios

Tags:

I have a problem with GraphQL. I want to send axios.post request to my server. I can do it in postman:

{     "query":"mutation{updateUserCity(userID: 2, city:\"test\"){id name age city knowledge{language frameworks}}} " } 

and in graphiql:

mutation {   updateUserCity(userID: 2, city: "test") {     id     name     age     city     knowledge {       language       frameworks     }   } } 

but can't do it in my code:(( here is my code snippet:

const data = await axios.post(API_URL, {   query: mutation updateUserCity(${ id }: Int!, ${ city }: String!) {     updateUserCity(userID: ${ id }, city: ${ city }){       id       name       age       city       knowledge{         language         frameworks       }     }   } }, {     headers: {       'Content-Type': 'application/json'     }   }) 

what's wrong in my code?

like image 388
Tatevik Avatar asked Oct 15 '18 12:10

Tatevik


People also ask

Can we use Axios with GraphQL?

Yes! This is very similar to how you make REST API calls. The only part which you need to worry about is constructing the query object of the GraphQL Query. Now let us look at the same example with Axios.

Can you make POST requests with GraphQL?

GraphQL requests can be sent via HTTP POST or HTTP GET requests. GET requests must be sent in the following format. The query, variables, and operation are sent as URL-encoded query parameters in the URL. In either request method (POST or GET), only query is required.

How do you fetch API in GraphQL?

You can fetch data very simply with the help of the package graphql-request . GraphQL Request is a library that doesn't require you to set up a client or a Provider component. It is essentially a function that just accepts an endpoint and a query.

Can you use GraphQL without Apollo?

Unfortunately, while I'm sure their platform is great, if you're setting up a fresh GraphQL API you should not start with Apollo. It certainly might be useful later, but on day 1 it's a trap, and you'll make your life simpler and easier if you avoid it entirely.


Video Answer


2 Answers

Value of query parameter to be passed in request has to be string and names of variables passed to GraphQL queries should be prefixed by $. You have used string literals for variables in request instead. Also, variables can be passed in post request using variables key.

Changing your code to something like below should get it to working:

const data = await axios.post(API_URL, {   query: `mutation updateUserCity($id: Int!, $city: String!) {     updateUserCity(userID: $id, city: $city){       id       name       age       city       knowledge{         language         frameworks       }     }   }`,   variables: {     id: 2,     city: 'Test'   } }, {     headers: {       'Content-Type': 'application/json'     }   }) 
like image 199
Raeesaa Avatar answered Sep 19 '22 00:09

Raeesaa


I enjoy using the following syntax, which is similar to the accepted answer, but more explicit.

Note that the variables object is nested inside of the data object, and is a sibling of the query object.

const data = await axios({   url: API_URL,   method: 'post',   headers: {     'Content-Type': 'application/json',     // any other headers you require go here   },   data: {     query: `       mutation updateUserCity($id: Int!, $city: String!) {         updateUserCity(userID: $id, city: $city){           id           name           age           city           knowledge{             language             frameworks           }         }       }     `,     variables: {       id: 2,       city: 'Test'     }   } }); 
like image 44
NWRichmond Avatar answered Sep 18 '22 00:09

NWRichmond