All Projects → jointoucan → finch-graphql

jointoucan / finch-graphql

Licence: MIT license
Local GraphQL API in the background process of a web extension.

Programming Languages

typescript
32286 projects
CSS
56736 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to finch-graphql

kanji
A strongly typed GraphQL API framework
Stars: ✭ 12 (-75%)
Mutual labels:  graphql-server, graphql-api
Gqlify
[NOT MAINTAINED]An API integration framework using GraphQL
Stars: ✭ 182 (+279.17%)
Mutual labels:  graphql-server, graphql-api
Pop
Monorepo of the PoP project, including: a server-side component model in PHP, a GraphQL server, a GraphQL API plugin for WordPress, and a website builder
Stars: ✭ 160 (+233.33%)
Mutual labels:  graphql-server, graphql-api
Daptin
Daptin - Backend As A Service - GraphQL/JSON-API Headless CMS
Stars: ✭ 1,195 (+2389.58%)
Mutual labels:  graphql-server, graphql-api
36 Graphql Concepts
📜 36 concepts every GraphQL developer should know.
Stars: ✭ 209 (+335.42%)
Mutual labels:  graphql-server, graphql-api
Graphql Stack
A visual explanation of how the various tools in the GraphQL ecosystem fit together.
Stars: ✭ 117 (+143.75%)
Mutual labels:  graphql-server, graphql-api
Graphql2rest
GraphQL to REST converter: automatically generate a RESTful API from your existing GraphQL API
Stars: ✭ 181 (+277.08%)
Mutual labels:  graphql-server, graphql-api
Graph Node
Graph Node indexes data from blockchains such as Ethereum and serves it over GraphQL
Stars: ✭ 884 (+1741.67%)
Mutual labels:  graphql-server, graphql-api
Rails Devise Graphql
A Rails 6 boilerplate to create your next Saas product. Preloaded with graphQL, devise, JWT, CanCanCan, RailsAdmin, Rubocop, Rspec, i18n and more.
Stars: ✭ 199 (+314.58%)
Mutual labels:  graphql-server, graphql-api
Grial
A Node.js framework for creating GraphQL API servers easily and without a lot of boilerplate.
Stars: ✭ 194 (+304.17%)
Mutual labels:  graphql-server, graphql-api
Rest And Graphql
⚡️ Highly scalable REST API codebase with GraphQL layer on its ⚡️
Stars: ✭ 55 (+14.58%)
Mutual labels:  graphql-server, graphql-api
DotNetGraphQL
A sample demonstrating how to create a GraphQL Backend in .NET and consume it from a .NET mobile app created using Xamarin
Stars: ✭ 78 (+62.5%)
Mutual labels:  graphql-server, graphql-api
Graphql
Haskell GraphQL implementation
Stars: ✭ 36 (-25%)
Mutual labels:  graphql-server, graphql-api
Hangzhou Graphql Party
杭州 GraphQLParty 往期记录(slide,照片,预告,视频等)
Stars: ✭ 142 (+195.83%)
Mutual labels:  graphql-server, graphql-api
Graphql Spqr
Java 8+ API for rapid development of GraphQL services
Stars: ✭ 843 (+1656.25%)
Mutual labels:  graphql-server, graphql-api
Storefront Api
Storefront GraphQL API Gateway. Modular architecture. ElasticSearch included. Works great with Magento1, Magento2, Spree, OpenCart, Pimcore and custom backends
Stars: ✭ 180 (+275%)
Mutual labels:  graphql-server, graphql-api
Typegql
Create GraphQL schema with TypeScript classes.
Stars: ✭ 415 (+764.58%)
Mutual labels:  graphql-server, graphql-api
Graphql Engine
Blazing fast, instant realtime GraphQL APIs on your DB with fine grained access control, also trigger webhooks on database events.
Stars: ✭ 24,845 (+51660.42%)
Mutual labels:  graphql-server, graphql-api
Graphql Spqr Spring Boot Starter
Spring Boot 2 starter powered by GraphQL SPQR
Stars: ✭ 187 (+289.58%)
Mutual labels:  graphql-server, graphql-api
Graphqlize
A Clojure & JVM library for developing GraphQL API instantly from Postgres and MySQL databases
Stars: ✭ 240 (+400%)
Mutual labels:  graphql-server, graphql-api

Finch GraphQL

Finch GraphQL

Finch GraphQL is a library that allows you to build up a local GraphQL API that is accessible via client scripts of an web extension. When external messaging is setup you may even query Finch GraphQL from a connectable website.

Install

npm install --save @finch-graphql/api @finch-graphql/client graphql

If you are planning to use Finch GraphQL with react you can also install our React hooks

npm install --save @finch-graphql/react

How it works

Traditional implementation of GraphQL pass queries through HTTP, Finch GraphQL passes these queries though the browsers messaging system.

Diagram

Checkout an example extension.

Why?

Message passing is one of the main means of communication for content scripts to be able to access network request and access to local storage APIs like IndexDB. Using GraphQL gives you some powerful abilities when using this messaging.

  • Declarative: Imperative code can be hard to structure. Background scripts can become a confusing when imperatively connecting to events. GraphQL resolvers gives you structure on how to write your background script.
  • Error handling: If a error happens in the background script it will be surfaced to the client script. Writing code into resolvers GraphQL will catch the errors that happen on the background script.
  • Common patterns: GraphQL and React are common technology for the web, and when using them in a web extension it strips away some of the nuance of building an extension. This make extension development more accessible to backend and frontend developers.

Build out an API

The FinchApi class is a class that allows you to create an executable graphql schema. It is modeled to look just like the ApolloServer class. The only required properties in the options are typeDefs and resolvers.

import { FinchApi } from '@finch-graphql/api';

// Define your schema
const typeDefs = `
  input PermissionsInput {
    permissions: [String!]
  }

  type Browser {
    permissions($input: PermissionsInput)
  }

  type Query {
    browser: Browser
  }
`;

// Defined resolvers
const resolvers = {
  Browser: (parent, { input }) => {
    return browser.permissions.contains(input);
  },
};

// Create the executable schema
const api = new FinchApi({
  typeDefs,
  resolver,
});

Additional options

When initializing the api Finch has some options to be able to customize your API.

Option Type Description
messageKey string Use a custom message key instead of using the generic Finch-message
attachMessages boolean Auto attach browser messaging.
attachExternalMessages boolean Auto attach external browser messaging.
middleware MiddlewareFN[] Middleware provided by graphql-middleware
disableIntrospection boolean If true introspection queries will be turned off.
rules ValidationRules[] GraphQL validation rules

Attaching to messaging

If you do not have any existing messages you may use the attachMessages option to automatically attach to the runtime messages. If you have existing messages you will want to setup up the manual handler to ensure you are able to resolve async resolvers.

import { FinchMessageKey } from '@finch-graphql/types';

browser.runtime.on[External]Message.addListener(message => {
  if (message.type === FinchMessageKey.Generic) {
    return api.on[External]Message(message);
  }
}, []);

Query from content script

This is the main reason for this library, it makes it super easy to query large amounts of data from the background script without sending multiple messages.

import { FinchClient } from '@finch-graphql/client';

const client = new FinchClient();

const GetBrowserPermission = `
  query getBrowserPermission($input: PermissionsInput) {
    browser {
      permissions(input: $input)
    }
  }
`(async function main() {
  const resp = await client.query(GetBrowserPermission, {
    input: { permissions: ['geolocation'] },
  });

  if (resp.data?.browser?.permissions) {
    // Do stuff with permissions
  }
})();

React Hooks

There is two hooks available to use if you are using a React application. First is the useQuery hook.

import { useQuery } from '@finch-graphql/react';

const MyComponent = () => {
  const { data, error } = useQuery<QueryTypes, VariableTypes>(
    MyComponentQueryDoc,
    { variables: { enabled: true } }
  );

  if (error) {
    return null;
  }

  return (
    ...
  )
}

Testing

Testing between your background resolvers and client scripts is now super easy. Here is a snippet of code that will connect your background resolvers to the content scripts queries. Note this is using a jest mock.

import { backgroundApiInstance } from '~/background/graphql';

// This will connect the background resolvers to the client scripts when called.
export const connectBackgroundResolvers = () => {
  browser.runtime.sendMessage = jest
    .fn()
    .mockImplementation((message, sender) =>
      backgroundApiInstance.onMessage(message, sender),
    );
};
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].