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

Add ast toString returning query string #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

darren128
Copy link

@darren128 darren128 commented Sep 25, 2018

Pull Request Labels

  • feature

For #206

Example

const query = `{ query { user { firstName lastName } } }`;

const ast = gql(query);

console.log(ast.toString()); // "{ query { user { firstName lastName } } }"

@ghost ghost added the feature New addition or enhancement to existing solutions label Sep 25, 2018
@darren128
Copy link
Author

@jnwng Would you mind reviewing this? This could be a helpful feature and make debugging easier as well.

@gfx
Copy link

gfx commented Oct 30, 2018

This is just what I want.

@jnwng ping

@darren128
Copy link
Author

@stubailo Would you mind reviewing this?

src/index.js Outdated Show resolved Hide resolved
@stubailo
Copy link
Contributor

@darren128 still waiting on a fix!

@darren128
Copy link
Author

@stubailo Fixed and added a test for the use case.

@darren128
Copy link
Author

darren128 commented Dec 13, 2018

Ready for review @stubailo

@rybon
Copy link

rybon commented Dec 24, 2018

It would be nice to add a condense or minify flag to this feature to remove excess whitespace. Perhaps that should be passed to the toString function or applied on the returned value. Since it runs at compile time, we could turn this flag on or off depending on process.env.NODE_ENV === 'production' or something similar. And support it via babel-plugin-graphql-tag.

parsed.toString = function (condense) { return condense ? print(this, { condense: true }) : print(this); };

// ...

const query = `{ query { user { firstName lastName } } }`;

const ast = gql(query);

console.log(ast.toString(true)); // "{query{user{firstName lastName}}}"

Related: graphql/graphql-js#1523
PR: graphql/graphql-js#1628

@gabsprates
Copy link

@rybon, i wrote this right now: https://github.com/gabsprates/minify-graphql-loader

@darren128
Copy link
Author

@stubailo Bumping this for review, thanks!

@olslash
Copy link

olslash commented Mar 28, 2019

would love to see this merged!

@pie6k
Copy link

pie6k commented Apr 9, 2019

Is there any update about it?

@darren128
Copy link
Author

@gfx Bumping this for review, thanks!

@rybon
Copy link

rybon commented May 15, 2019

@jnwng will this PR ever move anywhere?

@jnwng
Copy link
Contributor

jnwng commented May 15, 2019

to confirm, this is a breaking change, correct? are there cases where folks may be relying on the current return value of toString that returns "[object Object]"?

@darren128
Copy link
Author

@jnwng Yes, it would break the current usage of toString. We can call that out in the change log, or are you proposing we implement this under a different method name?

@jnwng
Copy link
Contributor

jnwng commented May 15, 2019

as mentioned in #206 (comment), using loc.source.body gets the equivalent GraphQL string that was sent to this literal.

given the breaking-ness of this change, i'm not sure it merits a major bump and i'm wondering if documentation might be resolve this issue. is it important that we're passing the string through the printer?

@rybon
Copy link

rybon commented May 15, 2019

Would changing the naming in combination with graphql/graphql-js#1523 feature help resolve those concerns?

@rybon
Copy link

rybon commented May 15, 2019

Here's a proposal:

var stripIgnoredCharacters = require('graphql').stripIgnoredCharacters;

// ...

parsed.toStrippedString = function() { return stripIgnoredCharacters(this); };

Requires graphql-js 14.3.0

@rybon
Copy link

rybon commented May 16, 2019

My proposal as a PR: #260

@rybon
Copy link

rybon commented May 18, 2019

If it's any help, I managed to add this feature to babel-plugin-graphql-tag: gajus/babel-plugin-graphql-tag#31

I believe it achieves the same thing as this PR, only difference is the GraphQL string literals are stripped of insignificant characters instead of pretty printed.

@miguelmota
Copy link

As mentioned in this comment, you can do the following to get the graphql string:

require('graphql-tag').default('query { test }').loc.source.body

@intellix
Copy link

Will we get the ability to stripIgnoredCharacters at any point? The PR from @rybon was closed and we're not using Babel in our chain. Perhaps it could be an ApolloClient option to strip whitespace from all GET queries

@wenisy
Copy link

wenisy commented Jan 7, 2020

As mentioned in this comment, you can do the following to get the graphql string:

require('graphql-tag').default('query { test }').loc.source.body

Thanks.
But If I my Graphql is a file and included #import, what should I do?

Like require('graphql-tag').default('../abc.graphql').loc.source.body

//abc.graphql
#import './hello.graphql'

...

@good-idea
Copy link

Chiming in here to say that since loc.source.body is the query before it has been parsed, it isn't helpful for cases where you're relying on some of the nice features of graphql-tag like fragment deduplication. I'd love a way to get a cleaned query string from the AST!

@francoisromain
Copy link

@good-idea How did you handle this finally?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New addition or enhancement to existing solutions waiting on contributor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet