Coder Social home page Coder Social logo

Comments (22)

detrohutt avatar detrohutt commented on August 29, 2024 6

I'm using nextjs and can't use the webpack loader so I made a plugin here by modifying babel-plugin-inline-import. It doesn't support importing fragments yet but it was enough to get me started moving my queries into .graphql files.

Update: It now supports fragments

@stubailo @fubhy Let me know if there's anything I could do to make this more useful. Hopefully this doesn't completely overlap with the functionality of persistgraphql as I'm not really familiar with that.

from graphql-tag.

jtmthf avatar jtmthf commented on August 29, 2024 4

I started working on this here jtmthf/babel-plugin-graphql-tag. It's not working just yet though. Do have support for external fragments which is nice.

from graphql-tag.

stubailo avatar stubailo commented on August 29, 2024 2

Yeah - basically the result doesn't include any information about aliases, fragments, or arguments, and we need that information to cache properly. For example:

query { a }

query { a: b }

Because of aliases, the two above queries will have identically shaped results and the only way to differentiate is to see what the field in the query was called.

from graphql-tag.

stubailo avatar stubailo commented on August 29, 2024 2

@thangngoc89 btw you can also use persisted queries with the queries inside the app code, it's just not documented yet. Could you open an issue there to document it?

However it's definitely unfortunate that Next.js doesn't support this by bundling all of the code in the same way, that seems like it would be pretty important for a framework that has easy SSR as one of the main values.

Regardless of all these things having a babel plugin would be sweet. Someone should get started on it!

from graphql-tag.

stubailo avatar stubailo commented on August 29, 2024 2

@thangngoc89 actually it works already, it's just not documented! That issue is when you're using something other than gql. Here are the options you need to pass into the CLI: https://github.com/apollographql/persistgraphql/blob/master/src/ExtractGQL.ts#L62-L66

So I guess it would be --inJsCode=true --extension='js' or similar. I haven't actually run it myself, only write unit tests πŸ˜„

from graphql-tag.

jnwng avatar jnwng commented on August 29, 2024 2

i think a combination of https://www.npmjs.com/package/babel-plugin-inline-import-graphql-ast and https://github.com/gajus/babel-plugin-graphql-tag should resolve importing graphql fragments in next.js / react-native and pre-compiling queries to resolve the need for the graphql-tag runtime, respectively. thank you to @detrohutt and @gajus for your contributions!

from graphql-tag.

thangngoc89 avatar thangngoc89 commented on August 29, 2024 1

I'm building an app with ssr and next.js . Next.js doesn't allow webpack loader due to the complexity of ssr. Having a babael plugin would help me use persisted queries.

from graphql-tag.

jtmthf avatar jtmthf commented on August 29, 2024 1

@thangngoc89 it's possible, but it's pretty rare for a Babel plugin load an external file. To not complicate this plugin too much. Loading from an external file could be done from a separate plugin that scans for imports to (.gql|.graphql) files and then replaces the import statement with the template literal that could then be picked up by babel-plugin-graphql-tag

from graphql-tag.

chaffeqa avatar chaffeqa commented on August 29, 2024 1

I agree with @detrohutt, this also solves for users using Expo (since they dont expose their webpack, but rather their babelrc.

I also agree with moving this to the babel layer makes sense because I'd think in time, just like import blah.json should resolve, import blah.graphql could as well

from graphql-tag.

stubailo avatar stubailo commented on August 29, 2024

Do we think this is actually a good idea? Is there real data that shows it's better to compile the queries ahead of time for a medium-sized app with 10+ queries? It feels like the AST could get pretty large.

Anyway, I think it would be cool to add a Babel plugin in here, and I can't work on it personally but would be happy to help.

from graphql-tag.

jtmthf avatar jtmthf commented on August 29, 2024

Presumably, this would have the same runtime impact as the webpack loader. Just with the queries being written inline instead of in an external file. The benefit I could see is for apps with no SSR. Having the queries pre-parsed could cut down on the time until the first query is sent. The downside being a bigger bundle, which could potentially increase the time to first query and paint.

I'll make a quick port of GitHunt-React using the loader and no SSR and see what impact that has on performance. Then mess around with both network and cpu throttling as well to see how different scenarios affect the performance. If most scenarios have the same or worse performance then this won't be worth doing.

from graphql-tag.

wmertens avatar wmertens commented on August 29, 2024

The converted queries will be highly compressible code, and the 40KB minified graphql-tag and possibly parts of graphql (through dead code elimination) might even more than offset it…

from graphql-tag.

jnwng avatar jnwng commented on August 29, 2024

@jtmthf @wmertens do you think this is better addressed by something like persisted queries where the client doesn't have to send the query at all (just an id?)

from graphql-tag.

stubailo avatar stubailo commented on August 29, 2024

Even if you are only sending the ID, the client still needs to know the original query so persisted queries wouldn't significantly reduce the bundle size.

from graphql-tag.

jnwng avatar jnwng commented on August 29, 2024

@stubailo i don't fully understand, why does the client need to know the original query? is that for caching purposes?

from graphql-tag.

wmertens avatar wmertens commented on August 29, 2024

from graphql-tag.

thangngoc89 avatar thangngoc89 commented on August 29, 2024

@wmertens Next.js will run the source code in Node environment so no webpack loader is allow.

From their docs:

Warning: Adding loaders to support new file types (css, less, svg, etc.) is not recommended because only the client code gets bundled via webpack and thus it won't work on the initial server rendering. Babel plugins are a good alternative because they're applied consistently between server/client rendering (e.g. babel-plugin-inline-react-svg).

from graphql-tag.

thangngoc89 avatar thangngoc89 commented on August 29, 2024

@stubailo oh sweet. Best news ever. I think you opened on PR for this already apollographql/persistgraphql#7

from graphql-tag.

jtmthf avatar jtmthf commented on August 29, 2024

Got it working! I'll add documentation and significantly more tests soon.

Edit:
It appears @boopathi attempted this project awhile back and currently holds the package name on NPM.

from graphql-tag.

thangngoc89 avatar thangngoc89 commented on August 29, 2024

@jtmthf I'll as soon as I get back to the work computer. Any plans for importing a gql file directly?

from graphql-tag.

batjko avatar batjko commented on August 29, 2024

I'm running into this issue when testing my React app with Mocha and the webpack loader recommended in the Apollo docs.

The loader works great for the app, allowing me to extract .graphql files from my code and simply import them.

However, when I want to run Enzyme tests on my components that use graphql() to inject the data from my server, the import of .graphql files doesn't work.

I tried to ignore the import (using the ignore-styles package, that allows you to specifiy extensions to ignore during future imports), but then graphql() complains that it wasn't passed a proper GraphQL DocumentNode, and I have no idea how I could mock / spy it up before rendering the component.

from graphql-tag.

fubhy avatar fubhy commented on August 29, 2024

I believe that https://github.com/apollographql/persistgraphql has hit a wall due to apollographql/persistgraphql#13. I think that the babel plugin you are proposing here could be the solution to this problem.

from graphql-tag.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.