Last active
January 12, 2024 05:34
-
-
Save bjunc/293748e6f9aa193a0095e6bcce86dbbf to your computer and use it in GitHub Desktop.
application/graphql vs application/json using axios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let input = { first_name: 'Foo', last_name: 'Bar' }; | |
// application/graphql example | |
/* eslint-disable no-unused-vars */ | |
let configGraphQL = { | |
url: '/graphql', | |
method: 'post', | |
headers: { 'Content-Type': 'application/graphql' }, | |
data: `mutation { user(id: 1, input: ${ JSON.stringify(input) }){ full_name } }` | |
}; | |
// application/json example | |
/* eslint-disable no-unused-vars */ | |
let configJson = { | |
url: '/graphql', | |
method: 'post', | |
data: { | |
query: `mutation { user(id: 1, input: ${ JSON.stringify(input) }){ full_name } }` | |
} | |
}; | |
// swap bewteen configGraphQL and configJson (same response) | |
axios(configJson).then(response => { | |
console.log('graphql response:', response.data); | |
}).catch(err => { | |
console.log('graphql error:', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment