|
| 1 | +--- |
| 2 | +name: GraphQLBox client |
| 3 | +description: An extensible GraphQL client with modules for react, caching, request parsing, web workers, websockets and more... |
| 4 | +url: https://github.com/badbatch/graphql-box |
| 5 | +github: badbatch/graphql-box |
| 6 | +npm: "@graphql-box/client" |
| 7 | +--- |
| 8 | + |
| 9 | +The example below installs and initializes the GraphQLBox client with a persisted cache and debugging enabled. |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install @graphql-box/core @graphql-box/client @graphql-box/request-parser @graphql-box/cache-manager @graphql-box/debug-manager @graphql-box/fetch-manager @graphql-box/helpers @cachemap/core @cachemap/reaper @cachemap/indexed-db @cachemap/constants @cachemap/types |
| 13 | +``` |
| 14 | + |
| 15 | +```javascript |
| 16 | +import Cachemap from "@cachemap/core"; |
| 17 | +import indexedDB from "@cachemap/indexed-db"; |
| 18 | +import reaper from "@cachemap/reaper"; |
| 19 | +import CacheManager from "@graphql-box/cache-manager"; |
| 20 | +import Client from "@graphql-box/client"; |
| 21 | +import DebugManager from "@graphql-box/debug-manager"; |
| 22 | +import FetchManager from "@graphql-box/fetch-manager"; |
| 23 | +import RequestParser from "@graphql-box/request-parser"; |
| 24 | +import introspection from "./introspection-query"; |
| 25 | + |
| 26 | +const requestManager = new FetchManager({ |
| 27 | + apiUrl: "/api/graphql", |
| 28 | + batchRequests: true, |
| 29 | + logUrl: "/log/graphql", |
| 30 | +}); |
| 31 | + |
| 32 | +const client = new Client({ |
| 33 | + cacheManager: new CacheManager({ |
| 34 | + cache: new Cachemap({ |
| 35 | + name: "client-cache", |
| 36 | + reaper: reaper({ interval: 300000 }), |
| 37 | + store: indexedDB(/* configure */), |
| 38 | + }), |
| 39 | + cascadeCacheControl: true, |
| 40 | + typeCacheDirectives: { |
| 41 | + // Add any type specific cache control directives in the format: |
| 42 | + // TypeName: "public, max-age=3", |
| 43 | + }, |
| 44 | + }), |
| 45 | + debugManager: new DebugManager({ |
| 46 | + environment: "client", |
| 47 | + log: (message, data, logLevel) => { |
| 48 | + requestManager.log(message, data, logLevel); |
| 49 | + }, |
| 50 | + name: "CLIENT", |
| 51 | + performance: self.performance, |
| 52 | + }), |
| 53 | + requestManager, |
| 54 | + requestParser: new RequestParser({ introspection }), |
| 55 | +}); |
| 56 | + |
| 57 | +// Meanwhile... somewhere else in your code |
| 58 | + |
| 59 | +const { data, errors } = await client.request(queryOrMutation); |
| 60 | +``` |
0 commit comments