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

Implement toString() to support graphql.js #206

Open
2 of 4 tasks
Pokute opened this issue Aug 22, 2018 · 4 comments
Open
2 of 4 tasks

Implement toString() to support graphql.js #206

Pokute opened this issue Aug 22, 2018 · 4 comments
Labels
feature New addition or enhancement to existing solutions good first issue Issues that are suitable for first-time contributors. graphql.js

Comments

@Pokute
Copy link

Pokute commented Aug 22, 2018

Issue Labels

  • has-reproduction
  • feature
  • [] docs
  • blocking
  • good first issue

/label graphql.js

graphql-tag claims to be a utility for graphql, but doesn't have any indication on how to use it with graphql package. Sure enough the result from graphql-tag might be valid AST, but the I don't think that the specifications really make any mention of queries being in AST format. Graphql doesn't support gql objects as query parameter.

const someQuery = gql`
  query { something { else } }
`

console.log(someQuery.toString()); // "[object Object]"
graphql(schema, someQuery); // Fails.

someQuery.toString() = function() { return this.loc.source; };
console.log(someQuery.toString()); // Proper query string.
graphql(schema, someQuery); // Works.

This should be really easy to implement with the objects returned by the template stringy adding a custom toString() -function to them. Currently the default is always returns "[object Object]".

If above change is unacceptable, there should be documentation how to properly retrieve the query in string form.

@ghost ghost added feature New addition or enhancement to existing solutions good first issue Issues that are suitable for first-time contributors. graphql.js labels Aug 22, 2018
@darren128
Copy link

darren128 commented May 1, 2019

@Pokute See #214

Still trying to get it merged....

@dobesv
Copy link
Contributor

dobesv commented May 3, 2019

I think you can get the input source code from loc.source.body:

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

@balazsorban44
Copy link

https://github.com/nhi/graphql-operations-string-loader

If someone is interested, I created a loader that puts out named exports of each operation from a graphql file as strings.

In case of a single operation per file, it will also alias it to the default export

@rizky
Copy link

rizky commented Jul 4, 2022

I think it would be safer to just use

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

https://stackoverflow.com/questions/63034980/graphqlerror-there-can-be-only-one-fragment-named

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 good first issue Issues that are suitable for first-time contributors. graphql.js
Projects
None yet
Development

No branches or pull requests

5 participants