Skip to content

docs: add GraphQLBox client and server to docs #1311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/content/code/language-support/javascript/client/graphql-box.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
name: GraphQLBox client
description: An extensible GraphQL client with modules for react, caching, request parsing, web workers, websockets and more...
url: https://github.com/badbatch/graphql-box
github: badbatch/graphql-box
npm: "@graphql-box/client"
---

The example below installs and initializes the GraphQLBox client with a persisted cache and debugging enabled.

```bash
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
```

```javascript
import Cachemap from "@cachemap/core";
import indexedDB from "@cachemap/indexed-db";
import reaper from "@cachemap/reaper";
import CacheManager from "@graphql-box/cache-manager";
import Client from "@graphql-box/client";
import DebugManager from "@graphql-box/debug-manager";
import FetchManager from "@graphql-box/fetch-manager";
import RequestParser from "@graphql-box/request-parser";
import introspection from "./introspection-query";

const requestManager = new FetchManager({
apiUrl: "/api/graphql",
batchRequests: true,
logUrl: "/log/graphql",
});

const client = new Client({
cacheManager: new CacheManager({
cache: new Cachemap({
name: "client-cache",
reaper: reaper({ interval: 300000 }),
store: indexedDB(/* configure */),
}),
cascadeCacheControl: true,
typeCacheDirectives: {
// Add any type specific cache control directives in the format:
// TypeName: "public, max-age=3",
},
}),
debugManager: new DebugManager({
environment: "client",
log: (message, data, logLevel) => {
requestManager.log(message, data, logLevel);
},
name: "CLIENT",
performance: self.performance,
}),
requestManager,
requestParser: new RequestParser({ introspection }),
});

// Meanwhile... somewhere else in your code

const { data, errors } = await client.request(queryOrMutation);
```
62 changes: 62 additions & 0 deletions src/content/code/language-support/javascript/server/graphql-box.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
name: GraphQLBox server
description: An extensible GraphQL server with modules for caching, request parsing, debugging, subscriptions and more...
url: https://github.com/badbatch/graphql-box
github: badbatch/graphql-box
npm: "@graphql-box/server"
---

The example below installs and initializes the GraphQLBox server with a persisted cache and debugging enabled.

```bash
npm install @graphql-box/core @graphql-box/server @graphql-box/client @graphql-box/request-parser @graphql-box/cache-manager @graphql-box/debug-manager @graphql-box/execute @graphql-box/helpers @cachemap/core @cachemap/reaper @cachemap/redis @cachemap/constants @cachemap/types
```

```javascript
import Cachemap from "@cachemap/core";
import redis from "@cachemap/redis";
import reaper from "@cachemap/reaper";
import CacheManager from "@graphql-box/cache-manager";
import Client from "@graphql-box/client";
import DebugManager from "@graphql-box/debug-manager";
import Execute from "@graphql-box/execute";
import RequestParser from "@graphql-box/request-parser";
import Server from "@graphql-box/server";
import { makeExecutableSchema } from "@graphql-tools/schema";
import { performance } from "perf_hooks";
import { schemaResolvers, schemaTypeDefs } from "./schema";
import logger from './logger';

const schema = makeExecutableSchema({ typeDefs: schemaTypeDefs, resolvers: schemaResolvers });

const server = new Server({
client: new Client({
cacheManager: new CacheManager({
cache: new Cachemap({
name: "server-cache",
reaper: reaper({ interval: 300000 }),
store: redis(/* configure */),
}),
cascadeCacheControl: true,
typeCacheDirectives: {
// Add any type specific cache control directives in the format:
// TypeName: "public, max-age=3",
},
}),
debugManager: new DebugManager({
environment: "server",
log: (...args) => {
logger.log(...args);
},
name: "SERVER",
performance,
}),
requestManager: new Execute({ schema }),
requestParser: new RequestParser({ schema }),
}),
});

// Meanwhile... somewhere else in your code

app.use("api/graphql", graphqlServer.request());
```