Client Setup
The client represents content, popover or any of the other page scripts that are created internally in the extension. These scripts have a relatively small amount of setup needed because they are already connected with the background script. To install run npm install @finch-graphql/client --save
.
#
Configuring your clientFinch GraphQL has a FinchClient class that is the client version of the FinchApi class. This allows you to configure your client to include things like custom message keys and caching.
#
Querying informationTo be able to get information from the background script you would need to write a GraphQL query, and then use the client method to pull that information from the GraphQL API.
There is also a very similar method that will allow you to make mutations against the background API as well.
In the above example we are asking the background process if the extension has the permission of geolocation available to the extension. The we are able to check the response of the query to see if that permission is available. There is little to no setup here.
#
Custom message keysIf you have a custom message key in your API then you can pass it to the constructor of the client.
#
CachingMentioned in setup of the client you can pass a instance of QueryCache to the FinchClient. This will allow you to keep queries up to date across the codebase by sharing cache between the results. This currently is automatically setup with the React hooks, but subscription methods are exposed on the clients.
The return of a subscribe method will be a function that will unsubscribe you from the updates to the cache key.
#
React HooksFinch GraphQL comes packaged with React Hooks. This makes it super simple to start querying information from the graph from inside React Components.
#
FinchProviderThe FinchClient can then be passed to the FinchProvider when using the client with React. This makes the client available to all of the other hooks in this section.
#
useQueryThis hook queries information when the component is rendered, and exposes a few pieces of data to allow you to control your component until the data is present.
In the example above we are fetching the same data in the other example but in this example we are rendering a React component, and rendering different states based on the state of the API query.
#
useMutationThis hook allows for graphQL mutations sets up a mutation but then only will execute the mutation once a returned function is called.
In the above example we setup a mutation that is going to request a permission from the background script. If an error happens we will show it and we can also confirm that it was successful though the data parameter.