Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could .toString return the graphql string back? #144

Closed
caub opened this issue Jan 16, 2018 · 20 comments
Closed

Could .toString return the graphql string back? #144

caub opened this issue Jan 16, 2018 · 20 comments

Comments

@caub
Copy link

caub commented Jan 16, 2018

So we could use this as a formatting tool as well

@caub caub changed the title Could .toString return the graphql string back Could .toString return the graphql string back? Jan 16, 2018
@jonaskello
Copy link

I also want the original string back becuase I need to prefix with gql to make code generation work, but then I also want to use the original string when building dynamic queries.

@jonaskello
Copy link

I found that you can get the string from the resulting gql document like this:

const myFragment = gql` .....some GQL... `;

console.log(getGqlString(myFragment));

function getGqlString(doc: DocumentNode) {
  return doc.loc && doc.loc.source.body;
}

@caub
Copy link
Author

caub commented Jan 22, 2018

@jonaskello yes, but it just return the original string, not formatted, so not interesting

https://github.com/apollographql/graphql-tag/blob/master/src/index.js#L8 can normalize the query at least

@jonaskello
Copy link

jonaskello commented Jan 22, 2018

@caub Yes that is true, for my case it was enough, but for formatting it is not useful. I looked for something like AstToString in the graphql library but I did not find something like that. I guess you can build your own pretty printer by visiting all nodes of the ast. GraphiQL has a pretty feature so maybe be worth looking into the source code of that.

@develomark
Copy link

@caub you can use graphql/language/printer

So based upon @jonaskello's slug:

import { print } from 'graphql/language/printer'

console.log(print(query)) 

This will return the full string including any fragments etc.

@caub
Copy link
Author

caub commented Jan 26, 2018

@develomark thanks! perfect

const {parse} = require('graphql');
const {print} = require('graphql/language/printer');
console.log(print(parse('{  lolo (first: 20) { ok koo } }')))

Ideally there would be an option to have the pretty query like that or a minimal one (more for http requests) (or maybe it doesn't matter much to save a few chars there)

Closing, thanks again

@caub caub closed this as completed Jan 26, 2018
@jnwng
Copy link
Contributor

jnwng commented Jan 26, 2018 via email

@develomark
Copy link

develomark commented Jan 26, 2018

To be honest, this is not that well documented and I think a .toString() or .printSchema() function would be useful.

@jnwng
Copy link
Contributor

jnwng commented Jan 26, 2018 via email

@develomark
Copy link

develomark commented Jan 26, 2018

Feels really logical to me to do:

import query from "./query.gql"
query.toString()

Now that we have imports inside graphql files, it isn't immediately obvious what's being constructed. One less step has got to be useful.

@jnwng
Copy link
Contributor

jnwng commented Jan 26, 2018 via email

@jnwng
Copy link
Contributor

jnwng commented Jan 26, 2018 via email

@conatus
Copy link

conatus commented Feb 6, 2018

It would be awesome is toString() was supported. I guessed this method would be there, was surprised it wasn't then ended up on this issue - which is great - but it would be better if it was in the mix by default.

@robbyemmert
Copy link

Right now my network request payloads are huge because it's sending the whole AST object. This also makes it much harder to debug network requests. A .toJSON function would solve this problem, because it would automatically convert to a string when serialized into JSON (i.e. sent over the network) just like Dates are serialized automatically.

@robbyemmert
Copy link

In my case, there's no point in parsing the AST on the client only to serialize it to string... It would be great if I can avoid parsing it in the first place.

@Vadorequest
Copy link

When I try to print my query using console.log('builtQuery', print(builtQuery)); I get

Invalid AST Node: { query: { kind: "Document", definitions: [Array] }, variables: { skip: 0, first: 10, orderBy: "id_ASC", where: {} }, parseResponse: [function] }

Eventually figured out I had to do console.debug('builtQuery', print(builtQuery.query)); and it worked perfectly.

@jeliasson
Copy link

Any update on this? Having .toString() would be really helpful. 😊

@dnknitro
Copy link

dnknitro commented Aug 1, 2020

You can get formatted query by using print function:

import { print } from 'graphql';

const query = gql`
  query ...
`;

console.log(print(query));

@balazsorban44
Copy link

https://github.com/nhi/graphql-operations-string-loader Created a webpack loader that reads graphql files (even with multiple operations per file) and exports them as named exports in a string format

@torressam333
Copy link

@caub you can use graphql/language/printer

So based upon @jonaskello's slug:

import { print } from 'graphql/language/printer'

console.log(print(query)) 

This will return the full string including any fragments etc.

If you need the docs: https://graphql.org/graphql-js/language/#print

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests