https://github.com/freshollie/apollo-bus-link
An apollo link and server for communication over message bus (Electron, WebWorker)
https://github.com/freshollie/apollo-bus-link
apollo apollo-link electron graphql webworker
Last synced: 3 months ago
JSON representation
An apollo link and server for communication over message bus (Electron, WebWorker)
- Host: GitHub
- URL: https://github.com/freshollie/apollo-bus-link
- Owner: freshollie
- Created: 2021-11-26T21:06:42.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-09T08:58:41.000Z (over 3 years ago)
- Last Synced: 2025-01-10T09:49:18.771Z (4 months ago)
- Topics: apollo, apollo-link, electron, graphql, webworker
- Language: TypeScript
- Homepage: https://npm.im/package/apollo-bus-link
- Size: 1.74 MB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
:trolleybus::busstop: apollo-bus-link :busstop::bus:
![]()
[](https://github.com/freshollie/apollo-bus-link/actions)
[](https://codecov.io/gh/freshollie/apollo-bus-link)
[](https://github.com/prettier/prettier)> Your one stop for message bus apollo links
## What is this?
Complex GraphQL based applications may use a separate process to run
schema execuation.This library provides a set of tools which can used to create an ApolloLink
and a Schema Executor which can communicate across any JS based message bus.This library provides pre-written receipes for `electron` and `webworker`'s
![]()
![]()
## Features
- API to integrate with any Bus
- Subscriptions, Queries, and Mutations
- Built in support for Electron and WebWorker buses## Usage
Electron :electron:
Main
```typescript
import {
createBusLinkBackend,
createSchemaExecutor,
} from "apollo-bus-link/core";
import { electronBus } from "apollo-bus-link/electron";
import { ipcMain } from "electron";// Args come from the link through `.initialiseBackend`
// You can pass whatever object you want
export type Args = {
mockedMode: boolean;
};const backend = createBusLinkBackend({
registerBus: electronBus(mockIpc.ipcMain),
createExecutor: (args) =>
createSchemaExecutor({
// Same API as apollo-server etc
schema,
context,
}),
});
// If you want to preiniitalise the backend, this can be done
// by calling initialise
// backend.initialise(...)
backend.listen();
```Renderer
```typescript
import { createElectronBusLink, electronBus } from "apollo-bus-link/electron";
import { ipcRenderer } from "electron";
import { ApolloClient } from "@apollo/client/core";const link = createElectronBusLink(ipcRenderer);
await link.initialiseBackend({ mockedMode: false });const client = new ApolloClient({
link,
...
});
```WebWorker :factory_worker:
Worker
```typescript
import {
createBusLinkBackend,
createSchemaExecutor,
} from "apollo-bus-link/core";
import { webWorkerBus } from "apollo-bus-link/electron";// Args come from the link through `.initialiseBackend`
// You can pass whatever object you want
export type Args = {
mockedMode: boolean;
};const backend = createBusLinkBackend({
registerBus: webWorkerBus(self),
createExecutor: (args) =>
createSchemaExecutor({
schema,
context,
}),
});// If you want to preiniitalise the backend, this can be done
// by calling initialise
// backend.initialise(...)
backend.listen();
```Window
```typescript
import { createWebWorkerBusLink } from "apollo-bus-link/webworker";
import { ApolloClient } from "@apollo/client/core";const worker = new Worker(
new URL("./Worker.ts", import.meta.url)
);const link = createWebWorkerBusLink(worker);
await link.initialiseBackend({ mockedMode: false });const client = new ApolloClient({
link,
...
});
```Custom
Backend
```typescript
import {
createBusLinkBackend,
createSchemaExecutor,
} from "apollo-bus-link/core";const backend = createBusLinkBackend({
registerBus: (request) => {
myMessageBus.onMessage((message) =>
// It may be that you need the context of the initial message
// to be able to response, hence this being callback hell
request(message, (response) => {
myMessageBus.send(response);
})
);
},
createExecutor: (args) =>
createSchemaExecutor({
// Same API as apollo-server etc
schema,
context,
}),
});
```Link
```typescript
import { BusLink } from "apollo-bus-link/core";
import { ApolloClient } from "@apollo/client/core";const link = new BusLink({
requestHandler: (request) => {
myMessageBus.send(request);
},
registerResponseHandler: (handler) => {
myMessageBus.onMessage((response) => handler(response));
},
});
await link.initialiseBackend({ mockedMode: false });const client = new ApolloClient({
link,
...
});
```## In the wild
- [fresh-configurator](https://github.com/freshollie/fresh-configurator) - Used as both in webworker and electron formats depending on where the app is loaded
## Licence
`MIT`