|
1 | 1 | ---
|
2 |
| -title: Compiling queries with Babel |
| 2 | +title: Compiling queries with Transpilers |
3 | 3 | ---
|
4 | 4 |
|
5 | 5 | If you prefer co-locating your GraphQL queries in your Javascript files, you typically use the [graphql-tag](https://github.com/apollographql/graphql-tag) library to write them. That requires to process the query strings into a GraphQL AST, which will add to the startup time of your application, especially if you have many queries.
|
6 | 6 |
|
7 |
| -To avoid this runtime overhead, you can precompile your queries created with `graphql-tag` using [Babel](http://babeljs.io/). Here are two ways you can do this: |
| 7 | +To avoid this runtime overhead, you can precompile your queries created with `graphql-tag` using transpilers like [Babel](http://babeljs.io/) and [SWC](https://swc.rs). Here are possible ways you can do this: |
8 | 8 |
|
9 | 9 | 1. Using [babel-plugin-graphql-tag](#using-babel-plugin-graphql-tag)
|
10 |
| -2. Using [graphql-tag.macro](#using-graphql-tagmacro) |
11 |
| -1. Using [ts-transform-graphql-tag](#using-ts-transform-graphql-tag) for TypeScript |
| 10 | +2. Using [graphql-tag-swc-plugin](#using-graphql-tag-swc-plugin) |
| 11 | +3. Using [graphql-tag.macro](#using-graphql-tagmacro) |
| 12 | +4. Using [ts-transform-graphql-tag](#using-ts-transform-graphql-tag) for TypeScript |
12 | 13 |
|
13 | 14 | If you prefer to keep your GraphQL code in separate files (`.graphql` or `.gql`) you can use [babel-plugin-import-graphql](https://github.com/detrohutt/babel-plugin-import-graphql). This plugin still uses `graphql-tag` under the hood, but transparently. You simply `import` your operations/fragments as if each were an export from your GraphQL file. This carries the same precompilation benefits as the above approaches.
|
14 | 15 |
|
@@ -38,6 +39,41 @@ Then add the plugin in your `.babelrc` configuration file:
|
38 | 39 |
|
39 | 40 | And that's it! All the usages of `import gql from 'graphql-tag'` will be removed, and the calls to `gql` will be replaced by the compiled version.
|
40 | 41 |
|
| 42 | +## Using graphql-tag-swc-plugin |
| 43 | + |
| 44 | +This plugin is the same as [babel-plugin-graphql-tag](#using-babel-plugin-graphql-tag) but for [swc](https://swc.rs/), a fast rust based babel alternative. |
| 45 | + |
| 46 | +Install the plugin in your dev dependencies: |
| 47 | + |
| 48 | +``` |
| 49 | +# with npm |
| 50 | +npm install --save-dev graphql-tag-swc-plugin |
| 51 | +
|
| 52 | +# or with yarn |
| 53 | +yarn add --dev graphql-tag-swc-plugin |
| 54 | +``` |
| 55 | + |
| 56 | +Then add the plugin in your `.swcrc` configuration file: |
| 57 | + |
| 58 | +``` |
| 59 | +{ |
| 60 | + jsc: { |
| 61 | + experimental: { |
| 62 | + plugins: [ |
| 63 | + ["graphql-tag-swc-plugin", |
| 64 | + { |
| 65 | + importSources: ["@apollo/client", "graphql-tag"], |
| 66 | + gqlTagIdentifiers: ["gql"] |
| 67 | + }, |
| 68 | + ], |
| 69 | + ], |
| 70 | + }, |
| 71 | + }, |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +For more information on configuration and features, please checkout the [`graphql-tag-swc-plugin`](https://github.com/rishabh3112/graphql-tag-swc-plugin) documentation. |
| 76 | + |
41 | 77 | ## Using graphql-tag.macro
|
42 | 78 |
|
43 | 79 | This approach is a bit more explicit, since you change all your usages of `graphql-tag` for `graphql-tag.macro`, which exports a `gql` function that you can use the same way as the original one. This macro requires the [babel-macros](https://github.com/kentcdodds/babel-macros) plugin, which will do the same as the previous approach but only on the calls that come from the macro import, leaving regular calls to the `graphql-tag` library untouched.
|
|
0 commit comments