question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Post request Mutation does not work in axios

See original GitHub issue

Hi, I have a post request mutation done using axios, I am quite sure it is correct, as it works in graphiql. My queries are alright, just the mutations do not work.

errors

POST http://127.0.0.1:5000/graphql 400 (Bad Request)
dispatchXhrRequest @ bundle.js:903
xhrAdapter @ bundle.js:740
dispatchRequest @ bundle.js:1261

Am i missing something? thanks

axios.post("#{graphqlUrl}",{
      query: "mutation {
        createDevice(input: $input)
      }"
      variables:
        input: device
    })
... in schema
type Mutation {
  createDevice(input: CreateDeviceInput): Device
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:5
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
IvanGoncharovcommented, Mar 22, 2018

@Paddy-Hamilton @jeanpaulversace You can always validate your query and variables inside GraphiQL: image

@benjaminhon Can I close this issue?

2reactions
benjaminhoncommented, Mar 31, 2017

@jeanpaulversace if you are using axios, its the ‘data’ property and not the ‘body’ property

here is how i do my axios request

this is my helper function

request = ({query, variables}) ->
  axios({
    method:   'post'
    url:      "#{graphqlUrl}"
    headers:  {'Content-Type': 'application/json'}
    data:     JSON.stringify({query, variables})
  })

one case how i use it

create: (device) -> (dispatch) ->
    variables = {input: device}
    query = "
      mutation ($input: CreateDeviceInput!) {
        createDevice(input: $input) { id }
      }
    "
    request({query, variables})
    .then ->
      dispatch actions.fetch()

one my back end server

cors        = require 'cors'
graphqlHTTP = require 'express-graphql'
app.use('/graphql', cors(), graphqlHTTP({schema, graphiql: true}))
Read more comments on GitHub >

github_iconTop Results From Across the Web

Post mutation to graphql with axios - Stack Overflow
Here's the working request for reference. axios .post(config.apiendpoint, { query: ` mutation addSkill($id:String!, $name:String!, ...
Read more >
Node.js mutation example using Axios - monday Community
Hi @kevinmarchese. Here's a sample request to create an item in a board: const axios = require('axios') const body = { query: `...
Read more >
How to do GraphQL queries and mutations with Axios - Medium
If your query requires variables as arguments use the template literal syntax demonstrated in the mutation below.
Read more >
axios get cookie value from response - You.com - You.com
So the answer should be as simple as specifying the value of the Origin request header in the server.use.cors.origin value. Open side panel....
Read more >
Mutations | TanStack Query Docs
Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects. ... This will not work in React 16 and earlier....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found