-
Notifications
You must be signed in to change notification settings - Fork 177
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
Comments
I also want the original string back becuase I need to prefix with |
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 So based upon @jonaskello's slug:
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 ***@***.***> wrote:
Closed #144 <#144>.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#144 (comment)>,
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 |
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 ***@***.***> 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
<#144 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABERRvmIyw3bthzWty7G8-F-FifyTIU8ks5tOhwtgaJpZM4RfoAj>
.
|
Feels really logical to me to do:
Now that we have imports inside |
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 ***@***.***> 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
<#144 (comment)>,
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 ***@***.***> 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
<#144 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABERRkxJdWo89PRNfbD0ytfZeQUyCfNLks5tOiDzgaJpZM4RfoAj>
.
|
It would be awesome is |
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
Eventually figured out I had to do |
Any update on this? Having |
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 |
If you need the docs: https://graphql.org/graphql-js/language/#print |
So we could use this as a formatting tool as well
The text was updated successfully, but these errors were encountered: