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

[graphql-tag/loader] Allow importing query strings directly #250

Open
TLATER opened this issue Apr 20, 2019 · 4 comments
Open

[graphql-tag/loader] Allow importing query strings directly #250

TLATER opened this issue Apr 20, 2019 · 4 comments

Comments

@TLATER
Copy link

TLATER commented Apr 20, 2019

Hi, I'm using graphql-tag/loader to import .graphql files for compile time checks and general compilation benefits.

I do this mostly because it makes development a bit easier; I like to keep my runtime dependencies rather minimal, so I'm handling the actual requests myself (though the same problem applies to graphql.js or graphql-request).

To use the queries, I then need to use graphql.print at run time to convert my AST into query string - but my files were originally query strings, and this should be possible at compile time!

I'm now unnecessarily importing graphql into my "binary" and spend runtime turning an AST back into its source code. This strikes me as extremely inefficient.

Similar to #206, it would be nice if the webpack loader could export queries directly, so that we can use the graphql-tag loader for compilation without affecting runtime performance.

@dobesv
Copy link
Contributor

dobesv commented May 3, 2019

I think this is available as loc.source.body.

e.g.

console.log(require('./src/test.graphql').loc.source.body)

@TLATER
Copy link
Author

TLATER commented May 15, 2019

This almost works, but it won't work if you specify more than one query/fragment in a file and try to access only one. That is, this won't work:

import queries from './queries.graphql';

fetch(API_ENDPOINT, {
    method: 'POST',
    body: JSON.stringify({
        query: queries.foo.loc.source.body
    })
});

It will simply query with the full body of the queries.graphql file, completely unprocessed, not selecting a specific query/resolving fragments.

@dobesv
Copy link
Contributor

dobesv commented May 16, 2019

Well ... sure. But you were saying earlier that "my files were originally query strings, and this should be possible at compile time" which I interpreted to mean you wanted the original string. I guess what you're saying is that you want it to actually generate a new query document for each query and embed it into the generated js code. Some people might object as for anyone who has no use for the source, each document would have to include a copy of all the fragments it uses plus the query itself. Which could mean duplication between these different copies of the source.

One thing that might be easier than you think is to just make a fork of graphql-tag/loader that exports exactly the strings you want, and use that in webpack instead of using the one from this package. The loader is actually not super complicated.

@balazsorban44
Copy link

https://github.com/nhi/graphql-operations-string-loader I created a loader that handles multiple operations per file, and outputs them as named exports in a string format

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

3 participants