import { gql } from'overmind-graphql'exportconstonPostAdded=gql` subscription PostAdded() { postAdded() { id title } }`
You define queries,mutations and subscriptions with the effect. That means you can have multiple effects holding different queries and even endpoints. The endpoints are defined when you initialize the effect. This allows you to dynamically create the endpoints based on state, and also pass state related to requests to the endpoints. The queries, mutations and subscriptions are converted into Overmind effects that you can call from your actions.
Query
To call a query you will typically use an action. Let us create an action that uses our posts query.
Now that we have the data from our query in the state, we can decide ourselves when we want this data to update. It could be related to moving back to a certain page, maybe you want to update the data in the background or maybe it is enough to just grab it once. You do not really think about it any differently here than with any other data fetching solution.
Optimistic updates
Again, since our data is just part of our state we are in complete control of optimistically adding new data. Let us create an optimistic post.
There are two points of options in the Graphql factory. The headers and the options.
The headers option is a function that runs on every request, meaning you can dynamically change the headers based on the state of the application.
exportconstonInitialize= ({ state, effects }) => {effects.gql.initialize({ endpoint:'http://some-endpoint.dev',// This runs on every requestheaders: () => ({ authorization:`Bearer ${state.auth.token}` }),// The options are the options passed to GRAPHQL-REQUEST options: { credentials:'include', mode:'cors', }, }, { endpoint:'ws://some-endpoint.dev',// This runs on every connectparams: () => ({ token:state.auth.token }) })}
Custom subscription socket
If you want to define your own socket for connecting to subscriptions, a function can be used instead:
You can dispose any subscriptions in any action. There are two ways to dispose:
exportconstdisposeSubscriptions=async ({ state, effects }) => {// Disposes all subscriptions on "onPostAdded"effects.gql.subscriptions.onPostAdded.dispose()// If the subscription takes a payload, you can dispose specific// subscriptionseffects.gql.subscriptions.onPostChange.disposeWhere( data =>data.id ===state.currentPostId )}
Typescript
There is only a single type exposed by the library, Query. It is used for queries, mutations and subscriptions.
import { Query, gql } from'overmind-graphql'// You will understand this very soonimport { Posts } from'./graphql-types'exportconstposts:Query<Posts> =gql` query Posts { posts { id title } }`;
The first Query argument is the result of the query. There is also a second query argument which is the payload to the query, as seen here.
import { Query, gql } from'overmind-graphql'// You will understand this very soonimport { CreatePost, CreatePostVariables } from'./graphql-types'exportconstcreatePost:Query<CreatePost,CreatePostVariables> =gql` mutation CreatePost($title: String!) { createPost(title: $title) { id } }`
Generate typings
It is possible to generate all the typings for the queries and mutations. This is done by using the APOLLO project CLI. Install it with:
npm install apollo --save-dev
Now you can create a script in your package.json file that looks something like:
Apollo will look for queries defined with the gql template tag and automatically produce the typings. That means whenever you add, remove or update a query in your code you should run this script to update the typings. It also produces what is called graphql-global-types. These are types related to fields on your queries, which can be used in your state definition and/or actions.
Note that initially you have to define your queries without types and after running the script you can start typing them to get typing in your app and ensure that your app does not break when you change the queries either in the client or on the server
Optimize query
It is possible to transpile the queries from strings into code. This reduces the size of your bundle, though only noticeably if you have a lot of queries. This can be done with the BABEL-PLUGIN-GRAPHQL-TAG.