@envelop/preload-assets 3.4.0-alpha-26c4ae2.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +80 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ ## `@envelop/preload-assets`
2
+
3
+ Inject a function for registering assets that should be preloaded on the client.
4
+ The registered assets will be added under the `extensions.preloadAssets` key on the execution result.
5
+
6
+ On your client network layer you can register a handler for preloading the given resources as soon as the operation result is arriving on the client!
7
+
8
+ ## Getting Started
9
+
10
+ ```
11
+ yarn add @envelop/preload-assets
12
+ ```
13
+
14
+ ```ts
15
+ import { envelop } from '@envelop/core';
16
+ import { usePreloadAssets } from '@envelop/preload-asset';
17
+ import { makeExecutableSchema } from 'graphql';
18
+
19
+ const schema = makeExecutableSchema({
20
+ typeDefs: /* GraphQL */ `
21
+ type Query {
22
+ imageUrl: String!
23
+ }
24
+ `,
25
+ resolvers: {
26
+ Query: {
27
+ imageUrl: (_: unknown, __: unknown, context: any) => {
28
+ const imageUrl = 'https://localhost/some-asset.png';
29
+ context.registerPreloadAsset(imageUrl);
30
+ return Promise.resolve(imageUrl);
31
+ },
32
+ },
33
+ },
34
+ });
35
+
36
+ const getEnveloped = envelop({
37
+ plugins: [usePreloadAssets()],
38
+ });
39
+ ```
40
+
41
+ **Example response**
42
+
43
+ ```json
44
+ {
45
+ "data": {
46
+ "imageUrl": "https://localhost/some-asset.png"
47
+ },
48
+ "extensions": {
49
+ "preloadAssets": ["https://localhost/some-asset.png"]
50
+ }
51
+ }
52
+ ```
53
+
54
+ **Example client prefetch logic**
55
+
56
+ ```ts
57
+ const preloadAsset = (url) => {
58
+ var request = new XMLHttpRequest();
59
+ request.open('GET', url);
60
+ request.responseType = 'blob';
61
+ request.onload = () => {
62
+ if (request.status !== 200 ) {
63
+ console.error((new Error(`Image preload failed; error code '${request.statusText}'.`));
64
+ }
65
+ };
66
+ request.onerror = () => {
67
+ console.error(new Error(`There was a network error while preloading '${url}'.`));
68
+ };
69
+ request.send();
70
+ }
71
+
72
+ // call this function with the execution result within your network layer.
73
+ const onExecutionResult = (result) => {
74
+ if (Array.isArray(result?.extensions?.preloadAssets)) {
75
+ result.extension.preloadAssets.forEach((url) => {
76
+ preloadAsset(url);
77
+ });
78
+ }
79
+ };
80
+ ```
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@envelop/preload-assets",
3
- "version": "3.4.0-alpha-26c4ae2.0",
3
+ "version": "3.4.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "2.4.0-alpha-26c4ae2.0",
6
+ "@envelop/core": "^2.4.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {},