Skip to content

Commit 5f5326b

Browse files
authored
docs: add graphql-tag-swc-plugin to performance docs (#11884)
1 parent 3d164ea commit 5f5326b

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

docs/source/performance/babel.md

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
2-
title: Compiling queries with Babel
2+
title: Compiling queries with Transpilers
33
---
44

55
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.
66

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:
88

99
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
1213

1314
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.
1415

@@ -38,6 +39,41 @@ Then add the plugin in your `.babelrc` configuration file:
3839

3940
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.
4041

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+
4177
## Using graphql-tag.macro
4278

4379
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

Comments
 (0)