Graphql-tag: Could .toString return the graphql string back?

Created on 16 Jan 2018  Â·  19Comments  Â·  Source: apollographql/graphql-tag

So we could use this as a formatting tool as well

Most helpful comment

@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.

All 19 comments

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.

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;
}

@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

@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.

@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.

@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

Thank you! This is what we’re using under the hood in this library anyway
On Fri, Jan 26, 2018 at 10:31 AM Cyril Auburtin notifications@github.com
wrote:

Closed #144 https://github.com/apollographql/graphql-tag/issues/144.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/apollographql/graphql-tag/issues/144#event-1444119173,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABERRk2sUwDC_RvZwZi3Q0BIeG2pTpKrks5tOhn8gaJpZM4RfoAj
.

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

We previously proxied that function and removed it in lieu of using the
graphql-js library directly (mostly because we moved graphql-js to a
peerDependency). I think documentation would be a good solution here, what
do you think?
On Fri, Jan 26, 2018 at 10:40 AM Mark Petty notifications@github.com
wrote:

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

—
You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/apollographql/graphql-tag/issues/144#issuecomment-360868642,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABERRvmIyw3bthzWty7G8-F-FifyTIU8ks5tOhwtgaJpZM4RfoAj
.

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.

I see, that’s a good point. Let me sit on it a little and revisit why we
removed the proxy in the past, but this use case seems pretty
straightforward.
On Fri, Jan 26, 2018 at 10:50 AM Mark Petty notifications@github.com
wrote:

Feels really logical to me to do:

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

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.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/apollographql/graphql-tag/issues/144#issuecomment-360871243,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABERRoP1OCw_scVEPeeTVFSiAt6NhEOtks5tOh6SgaJpZM4RfoAj
.

We already do support interpolation, that’s how fragments are interpolated
into queries.
On Fri, Jan 26, 2018 at 11:01 AM Mark Petty notifications@github.com
wrote:

Actually, an additional benefit would be to be able to import gql files
and use them as AST documents or strings.

i.e.

import query from "./query.gql"
const composedQuery = ${query} ...More stuff

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/apollographql/graphql-tag/issues/144#issuecomment-360873999,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABERRkxJdWo89PRNfbD0ytfZeQUyCfNLks5tOiDzgaJpZM4RfoAj
.

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.

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.

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.

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.

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

You can get formatted query by using print function:

import { print } from 'graphql';

const query = gql`
  query ...
`;

console.log(print(query));

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

Was this page helpful?
0 / 5 - 0 ratings