I have a problem and I need to transform a query graphql to a json object. That is, I get a query in the following way and I would like to have a json of that query. How could I do it? I've been searching and I haven't found a way.
Thank you.
query {
Patient(id:4){
id
birthDate {
year,
day
}
name {
text
}
}
}
Any GraphQL document, whether it's a query or a schema, can be represented as an AST object. This is the standard way to represent a GraphQL document as an object. You can parse a document string into an object using the core library:
const { parse } = require('graphql')
const object = parse(`
query {
# ...
}
`)
The object can be converted back into a string as well:
const { print } = require('graphql')
const string = print(object)
GraphQL does not define a JSON standard for queries.
If you want to express a graphQL in JSON, you can define your own structure and write your own serializers. but it will not be supported by any graphQL service
EDIT: I found an NPM package that does that. It is not a standard whatsoever, but if you really need it, you can use this package: https://www.npmjs.com/package/json-to-graphql-query
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With