Giter Club home page Giter Club logo

graphql-transport-electron's Introduction

graphql-transport-electron

Why

In case you want to run a graphql server inside of the Electron main process (for instance if you are building a decentralized application), you need to have the client (renderer process) and server (main process) communicate through Electron IPC.

This project provides a very lightweight client and server interface for Electron IPC based on Apollo.

While its primary use-case is to enable a self-sustained and isolated graphql server inside of the Electron main process, you can connect the server interface to any ApolloLink instance. This means that you could also connect it with a HttpLink / WsLink that forwards your queries to a hosted graphql server outside of Electron.

Installation

# With yarn
yarn add graphql-transport-electron

# With npm
npm install graphql-transport-electron

Usage example

Main process

import { ipcMain } from 'electron';
// NOTE: The library ships with a fork of apollo-link-schema that also supports subscriptions.
import { createSchemaLink, createIpcExecutor } from 'graphql-transport-electron';
import schema from './my-graphql-schema';

// NOTE: This also works with any other apollo link (e.g. HttpLink from apollo-link).
const link = createSchemaLink({ schema });
createIpcExecutor({link, ipc: ipcMain });

Renderer process

import { ApolloClient } from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ipcRenderer } from 'electron';
import { createIpcLink } from 'graphql-transport-electron';

const link = createIpcLink({ ipc: ipcRenderer });
const client = new ApolloClient({
  cache: new InMemoryCache(),
  link,
});

graphql-transport-electron's People

Contributors

dependabot[bot] avatar fubhy avatar jimmed avatar jonaskruckenberg avatar megahertz avatar pellejacobs avatar xecez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

graphql-transport-electron's Issues

correct license?

In package.json license field, it is "GPL-3.0" but in LICENSE.md it's MIT

Network error undefined from ApolloError

I'm getting the following error when try to run my Electron app:

Uncaught (in promise) Error: Network error: undefined at new t (ApolloError.ts:57) at QueryManager.ts:576 at QueryManager.ts:1091 at Set.forEach (<anonymous>) at QueryManager.ts:1087 at Map.forEach (<anonymous>) at e.broadcastQueries (QueryManager.ts:1085) at QueryManager.ts:434

Do you have any additional instructions on how to implement this module?
Thanks in advance.

Error: Converting circular structure to JSON

Is the library working with @apollo/client v3? I see it still uses the apollo-http lib that was relocated to @apollo/client.

Following the getting started, I have a similar setup where I create an IpcExecutor that uses an HttpLink pointing to my remote server and a IpcLink in the renderer process that is passed to my ApolloClient. I get the following error:

main:

const link = new HttpLink({ uri: 'http://localhost:4000/graphql', fetch });
createIpcExecutor({ link, ipc: ipcMain });

renderer:

const link = createIpcLink({ ipc: ipcRenderer });
// const link = new HttpLink({ uri: 'http://localhost:4000/graphql' });
const apolloClient = new ApolloClient({
  cache: new InMemoryCache(),
  link
});

const AppContainer = process.env.PLAIN_HMR ? Fragment : ReactHotAppContainer;

document.addEventListener('DOMContentLoaded', () =>
  render(
    <AppContainer>
      <Root store={store} history={history} apolloClient={apolloClient} />
    </AppContainer>,
    document.getElementById('root')
  )
);

stack:

Error: Converting circular structure to JSON
    --> starting at object with constructor 'InMemoryCache'
    |     property 'policies' -> object with constructor 'Policies'
    |     property 'config' -> object with constructor 'Object'
    --- property 'cache' closes the circle
    at new ApolloError (http://localhost:1212/dist/renderer.dev.js:14968:28)
    at ObservableQuery.module.exports../node_modules/@apollo/client/core/ObservableQuery.js.ObservableQuery.getCurrentResult (http://localhost:1212/dist/renderer.dev.js:13337:28)
    at QueryData._this.getQueryResult (http://localhost:1212/dist/renderer.dev.js:16708:61)
    at QueryData.module.exports../node_modules/@apollo/client/react/data/QueryData.js.QueryData.getExecuteResult (http://localhost:1212/dist/renderer.dev.js:16827:27)
    at QueryData.module.exports../node_modules/@apollo/client/react/data/QueryData.js.QueryData.execute (http://localhost:1212/dist/renderer.dev.js:16776:51)
    at http://localhost:1212/dist/renderer.dev.js:17342:151
    at useDeepMemo (http://localhost:1212/dist/renderer.dev.js:17386:42)
    at useBaseQuery (http://localhost:1212/dist/renderer.dev.js:17342:85)
    at useQuery (http://localhost:1212/dist/renderer.dev.js:17228:87)
    at LoginView (http://localhost:1212/dist/renderer.dev.js:10093:76)

Note that if I pass an HttpLink created in the render process to my ApolloClient, things work as expected. Am I missing anything?

Switching to @apollo/client/core?

So currently I need to install react as a peer dependency to use this module even though I'm using vue.
As per the Apollo docs they recommend import the apollo client from @apollo/client/core if you don't need react support.
That'd be awesome!

"An object could not be cloned" electron@8

I'm setting a new project and ran into this:

Uncaught Error: An object could not be cloned.
    at new ApolloError (ApolloError.js?40ae:27)
    at ObservableQuery.getCurrentResult (ObservableQuery.js?010a:101)
    at QueryData._this.getQueryResult (QueryData.js?74a3:28)
    at QueryData.getExecuteResult (QueryData.js?74a3:152)
    at QueryData.execute (QueryData.js?74a3:101)
    at eval (useBaseQuery.js?b9a7:38)
    at useDeepMemo (useDeepMemo.js?31e9:9)
    at useBaseQuery (useBaseQuery.js?b9a7:38)
    at useQuery (useQuery.js?f933:4)
    at useData (useData.js?c525:8)

It wasn't hitting the resolver in the main process. I did a quick search and I suspect it was broken by this change in electron@8 (I have electron@9)

https://www.electronjs.org/docs/breaking-changes#behavior-changed-sending-non-js-objects-over-ipc-now-throws-an-exception

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.