···
2
+
<h2 align="center">graphql-web-lite</h2>
3
+
<p align="center"><strong>The small sibling of the <code>graphql</code> package, slimmed down for client-side libraries</strong></p>
5
+
<a href="https://npmjs.com/package/graphql-web-lite">
6
+
<img alt="NPM Version" src="https://img.shields.io/npm/v/graphql-web-lite.svg" />
8
+
<a href="https://npmjs.com/package/use-interactions">
9
+
<img alt="License" src="https://img.shields.io/npm/l/graphql-web-lite.svg" />
11
+
<a href="https://bundlephobia.com/result?p=graphql-web-lite">
12
+
<img alt="Minified gzip size" src="https://img.shields.io/bundlephobia/minzip/graphql-web-lite.svg?label=gzip%20size" />
18
+
The `graphql` package serves two purposes in being the reference implementation of the
19
+
[GraphQL specification](https://spec.graphql.org/) and being used as the shared toolkit
20
+
for implementing GraphQL in client-side and server-side JavaScript applications. This
21
+
can cause bloat for client-side apps, where we'd rather choose lower bundlesize impact
24
+
`graphql-web-lite` is an **experimental** library, providing an alias package that can
25
+
be swapped in for the standard `graphql` package in client-side applications.
26
+
It aims to reduce the size of imports that are in common use by GraphQL clients and
27
+
users, while still providing most `graphql` exports that are used in other contexts.
31
+
`graphql-web-lite` mirrors the folder structure of the regular `graphql` package and
32
+
includes mostly the same files and imports as the `graphql` package, minus a couple
33
+
that aren't commonly used for client-side GraphQL.
34
+
This offers us several installation options, depending on how the package is aliased
35
+
to the regular `"graphql"` import.
38
+
<summary><strong>Installation with <code>package.json</code> aliases</strong></summary>
40
+
When your dependencies and `node_modules` folder isn't used by a _GraphQL server_ and
41
+
only used by a _GraphQL clients_, you can use a quick and easy npm installation alias.
42
+
In your `package.json` file you can define your `graphql` installation to use
43
+
`graphql-web-lite` instead, which is supported by both Yarn and npm:
48
+
- "graphql": "^15.5.0"
49
+
+ "graphql": "npm:graphql-web-lite@^15.5.1000"
54
+
Alternatively, you can run an installation command to alias the package:<br />
57
+
npm install --save graphql@npm:graphql-web-lite
59
+
yarn add graphql@npm:graphql-web-lite
62
+
When this isn't suitable — for instance, because the same dependencies are shared
63
+
with an API or server-side GraphQL consumer, or you're working inside a monorepo —
64
+
you can try one of the other aliasing techniques below.
69
+
<summary><strong>Installation with Webpack aliases</strong></summary>
71
+
First, we'll need to install `graphql-web-lite` _alongside_ the regular `graphql`
75
+
npm install --save graphql-web-lite
77
+
yarn add graphql-web-lite
80
+
To alias a package in Webpack, an entry must be added to your Webpack
81
+
configuration's `resolve.alias` section. Depending on your implementation,
82
+
the configuration may already contain some keys inside `resolve.alias`, and
83
+
you'd want to add another entry for `"graphql"`.
86
+
const webpackConfig = {
90
+
graphql: 'graphql-web-lite',
99
+
<summary><strong>Installation with Next.js</strong></summary>
101
+
First, we'll need to install `graphql-web-lite` _alongside_ the regular `graphql`
105
+
npm install --save graphql-web-lite
107
+
yarn add graphql-web-lite
110
+
In your [Next.js configuration file](https://nextjs.org/docs/api-reference/next.config.js/introduction),
111
+
under `next.config.js`, you can add an additional `webpack()` function that is
112
+
able to modify Next's Webpack configuration to add an alias for `graphql`.
116
+
webpack(config, { isServer }) {
118
+
config.resolve.alias = {
119
+
...config.resolve.alias,
120
+
graphql: 'graphql-web-lite',
128
+
Here we're also ensuring that the alias isn't applied on the server-side, in case
129
+
an API route is using `graphql` for a server-side GraphQL schema.
134
+
<summary><strong>Installation with Rollup's alias plugin</strong></summary>
136
+
First, we'll need to install `graphql-web-lite` _alongside_ the regular `graphql`
140
+
npm install --save graphql-web-lite
142
+
yarn add graphql-web-lite
145
+
In Rollup, the easiest way to add a new alias is to use `@rollup/plugin-alias`.
146
+
We'll have to install this plugin and ensure that every import and deep import
147
+
to `graphql` is aliased to `graphql-web-lite`.
149
+
Any Rollup-based build will fail when the import name of the package does not
150
+
match the `name` field in the module's `package.json` file, so this step is
154
+
import alias from '@rollup/plugin-alias';
159
+
entries: [{ find: 'graphql', replacement: 'graphql-web-lite' }],
168
+
<summary><strong>Installation with Vite</strong></summary>
170
+
First, we'll need to install `graphql-web-lite` _alongside_ the regular `graphql`
174
+
npm install --save graphql-web-lite
176
+
yarn add graphql-web-lite
179
+
In your [Vite configuration file](https://vitejs.dev/config/#config-file-resolving)
180
+
you may add a new entry for its `resolve.alias` entries. This entry works the same
181
+
as Rollup's alias plugin.
184
+
import { defineConfig } from 'vite';
186
+
export default defineConfig({
189
+
graphql: 'graphql-web-lite',
195
+
Here we're also ensuring that the alias isn't applied on the server-side, in case
196
+
an API route is using `graphql` for a server-side GraphQL schema.
201
+
<summary><strong>Installation with Parcel</strong></summary>
203
+
First, we'll need to install `graphql-web-lite` _alongside_ the regular `graphql`
207
+
npm install --save graphql-web-lite
209
+
yarn add graphql-web-lite
212
+
In Parcel, we can add an entry in our `package.json` file for an alias. Specifically,
213
+
the `alias` property may contain an object that maps packages to their aliases.
218
+
+ "graphql": "graphql-web-lite"
226
+
<summary><strong>Installation with Jest's module mapping</strong></summary>
228
+
First, we'll need to install `graphql-web-lite` _alongside_ the regular `graphql`
232
+
npm install --save graphql-web-lite
234
+
yarn add graphql-web-lite
237
+
Jest allows any module names to be remapped using regular expression-based
238
+
mappings. In your Jest config you'll need to add an entry for `graphql` that
239
+
remaps all imports and deep imports to `graphql-web-lite`.
243
+
"moduleNameMapper": {
244
+
"graphql(.*)": "graphql-web-lite$1"
251
+
## Impact & Changes
253
+
In short, most utilities, functions, and classes exported by the `graphql`
254
+
package are only used for server-side GraphQL. Some of them have been removed
255
+
in `graphql-web-lite` to discourage its server-side usage, and help bundlers
256
+
exlude them if tree-shaking were to fail.
258
+
Most GraphQL clients rely on importing the parser, printer, visitor, and the
259
+
`GraphQLError` class. These have all been modified to reduce their bundlesize
260
+
impact and to remove any support for
261
+
[GraphQL's schema language and type system](https://spec.graphql.org/June2018/#sec-Type-System).
263
+
Any debugging calls, development-time warnings, and other non-production code
264
+
is also being transpiled away, and `process.env.NODE_ENV` has been compiled
268
+
<summary><strong>Full List of Changes</strong></summary>
270
+
| Export | Changes | Notes |
271
+
| -------------------------- | ----------- | ------------------------------------------------------------------- |
272
+
| `getVisitFn` | _unchanged_ | n/a |
273
+
| `visitInParallel` | _unchanged_ | n/a |
274
+
| `BREAK` | _unchanged_ | n/a |
275
+
| `visit` | _modified_ | works recursively and does not detect invalid AST nodes |
276
+
| `print` | _modified_ | won't output any schema nodes and does not detect invalid AST nodes |
277
+
| `printLocation` | _modified_ | won't output source snippets |
278
+
| `printSourceLocation` | _modified_ | won't output source snippets |
279
+
| `parse` | _modified_ | won't parse schema nodes or throw precise syntax errors |
280
+
| `parseType` | _modified_ | won't throw precise syntax errors |
281
+
| `parseValue` | _modified_ | won't throw precise syntax errors |
282
+
| `GraphQLError` | _modified_ | doesn't handle locations and Error stacks |
283
+
| `syntaxError` | _removed_ | n/a |
284
+
| `printType` | _removed_ | n/a |
285
+
| `printSchema` | _removed_ | n/a |
286
+
| `printIntrospectionSchema` | _removed_ | n/a |
287
+
| `lexicographicSortSchema` | _removed_ | n/a |
288
+
| `isSchema` | _removed_ | n/a |
289
+
| `isInterfaceType` | _removed_ | n/a |
290
+
| `getDescription` | _removed_ | n/a |
291
+
| `findDeprecatedUsages` | _removed_ | n/a |
292
+
| `buildSchema` | _removed_ | n/a |
293
+
| `buildASTSchema` | _removed_ | n/a |
294
+
| `assertSchema` | _removed_ | n/a |
295
+
| `assertInterfaceType` | _removed_ | n/a |
296
+
| `assertCompositeType` | _removed_ | n/a |
297
+
| `assertAbstractType` | _removed_ | n/a |
298
+
| `TokenKind` | _removed_ | n/a |
299
+
| `Token` | _removed_ | n/a |
300
+
| `Lexer` | _removed_ | n/a |
301
+
| `GraphQLUnionType` | _removed_ | n/a |
302
+
| `GraphQLInterfaceType` | _removed_ | n/a |
303
+
| `GraphQLInputObjectType` | _removed_ | n/a |