11# GraphQL
22
3+
4+
35Using Graphql with Overmind gives you the following benefits:
46
57* ** Query:** The query for data is run with the rest of your application logic, unrelated to mounting components
@@ -23,7 +25,7 @@ npm install overmind-graphql
2325The Graphql package is a _ configuration factory_ . That means you need some existing configuration before going:
2426
2527{% tabs %}
26- {% tab title="overmind/index.js " %}
28+ {% tab title="overmind/index.ts " %}
2729``` typescript
2830import { state } from ' ./state'
2931
@@ -33,10 +35,16 @@ export const config = {
3335```
3436{% endtab %}
3537
36- {% tab title="overmind/state.js " %}
38+ {% tab title="overmind/state.ts " %}
3739``` typescript
40+ // We will talk about this one soon :)
41+ import { Post } from ' ./graphql-types'
42+
43+ type State = {
44+ posts: Post []
45+ }
3846
39- export const state = {
47+ export const state: State = {
4048 posts: []
4149}
4250```
@@ -48,7 +56,7 @@ export const state = {
4856Now let us introduce the factory:
4957
5058{% tabs %}
51- {% tab title="overmind/index.js " %}
59+ {% tab title="overmind/index.ts " %}
5260``` typescript
5361import { graphql } from ' overmind-graphql'
5462import * as queries from ' ./queries'
@@ -65,11 +73,12 @@ export const config = graphql({
6573```
6674{% endtab %}
6775
68- {% tab title="overmind/queries.js " %}
76+ {% tab title="overmind/queries.ts " %}
6977``` typescript
70- import { gql } from ' overmind-graphql'
78+ import { Query , gql } from ' overmind-graphql'
79+ import { Posts } from ' ./graphql-types'
7180
72- export const posts = gql `
81+ export const posts: Query < Posts > = gql `
7382 query Posts {
7483 posts {
7584 id
@@ -80,11 +89,12 @@ export const posts = gql`
8089```
8190{% endtab %}
8291
83- {% tab title="overmind/mutations.js " %}
92+ {% tab title="overmind/mutations.ts " %}
8493``` typescript
85- import { gql } from ' overmind-graphql'
94+ import { Query , gql } from ' overmind-graphql'
95+ import { CreatePost , CreatePostVariables } from ' ./graphql-types'
8696
87- export const createPost = gql `
97+ export const createPost: Query < CreatePost , CreatePostVariables > = gql `
8898 mutation CreatePost($title: String!) {
8999 createPost(title: $title) {
90100 id
@@ -102,7 +112,7 @@ You define **queries** and **mutations** as part of the second argument to the f
102112To call a query you will typically use an action. Let us create an action that uses our ** posts** query.
103113
104114{% tabs %}
105- {% tab title="overmind/index.js " %}
115+ {% tab title="overmind/index.ts " %}
106116``` typescript
107117import { graphql } from ' overmind-graphql'
108118import * as actions from ' ./actions'
@@ -121,9 +131,11 @@ export const config = graphql({
121131```
122132{% endtab %}
123133
124- {% tab title="overmind/actions.js " %}
134+ {% tab title="overmind/actions.ts " %}
125135``` typescript
126- export const getPosts = async ({ state , effects }) => {
136+ import { AsyncAction } from ' overmind'
137+
138+ export const getPosts: AsyncAction = async ({ state , effects }) => {
127139 const { posts } = await effects .queries .posts ()
128140
129141 state .posts = posts
@@ -137,15 +149,17 @@ export const getPosts = async ({ state, effects }) => {
137149Mutation queries are basically the same as normal queries. You would typically also call these from an action.
138150
139151{% tabs %}
140- {% tab title="overmind/actions.js " %}
152+ {% tab title="overmind/actions.ts " %}
141153``` typescript
142- export const getPosts = async ({ state , effects }) => {
154+ import { AsyncAction } from ' overmind'
155+
156+ export const getPosts: AsyncAction = async ({ state , effects }) => {
143157 const { posts } = await effects .queries .posts ()
144158
145159 state .posts = posts
146160}
147161
148- export const addPost = async ({ effects }, title ) => {
162+ export const addPost: AsyncAction < string > = async ({ effects }, title ) => {
149163 await effects .mutations .createPost ({ title })
150164}
151165```
@@ -161,15 +175,17 @@ Now that we have the data from our query in the state, we can decide ourselves w
161175Again, 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.
162176
163177{% tabs %}
164- {% tab title="overmind/actions.js " %}
178+ {% tab title="overmind/actions.ts " %}
165179``` typescript
166- export const getPosts = async ({ state , effects }) => {
180+ import { AsyncAction } from ' overmind'
181+
182+ export const getPosts: AsyncAction = async ({ state , effects }) => {
167183 const { posts } = await effects .queries .posts ()
168184
169185 state .posts = posts
170186}
171187
172- export const addPost = async ({ state , effects }, title ) => {
188+ export const addPost: AsyncAction < string > = async ({ state , effects }, title ) => {
173189 const optimisticId = String (Date .now ())
174190
175191 state .posts .push ({
@@ -193,7 +209,7 @@ There are two points of options in the Graphql factory. The **headers** and the
193209The headers option is a function which receives the state of the application. That means you can produce request headers dynamically. This can be useful related to authentciation.
194210
195211{% tabs %}
196- {% tab title="overmind/index.js " %}
212+ {% tab title="overmind/index.ts " %}
197213``` typescript
198214import { graphql } from ' overmind-graphql'
199215import * as queries from ' ./queries'
@@ -217,7 +233,7 @@ export const config = graphql({
217233The options are the options passed to [ GRAPHQL-REQUEST] ( https://github.com/prisma-labs/graphql-request ) .
218234
219235{% tabs %}
220- {% tab title="overmind/index.js " %}
236+ {% tab title="overmind/index.ts " %}
221237``` typescript
222238import { graphql } from ' overmind-graphql'
223239import * as queries from ' ./queries'
@@ -242,50 +258,7 @@ export const config = graphql({
242258{% endtab %}
243259{% endtabs %}
244260
245- ## Typescript
246-
247- There is only a single type exposed by the library, ** Query** . It is used for both queries and mutations.
248-
249- {% tabs %}
250- {% tab title="overmind/queries.ts" %}
251- ``` typescript
252- import { Query , gql } from ' overmind-graphql'
253- // You will understand this very soon
254- import { Posts } from ' ./graphql-types'
255-
256- export const posts: Query <Posts > = gql `
257- query Posts {
258- posts {
259- id
260- title
261- }
262- }
263- ` ;
264- ```
265- {% endtab %}
266- {% endtabs %}
267-
268- 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.
269-
270- {% tabs %}
271- {% tab title="overmind/mutations.ts" %}
272- ``` typescript
273- import { Query , gql } from ' overmind-graphql'
274- // You will understand this very soon
275- import { CreatePost , CreatePostVariables } from ' ./graphql-types'
276-
277- export const createPost: Query <CreatePost , CreatePostVariables > = gql `
278- mutation CreatePost($title: String!) {
279- createPost(title: $title) {
280- id
281- }
282- }
283- `
284- ```
285- {% endtab %}
286- {% endtabs %}
287-
288- ### Generate typings
261+ ## Generate typings
289262
290263It is possible to generate all the typings for the queries and mutations. This is done by using the [ APOLLO] ( https://www.apollographql.com/ ) project CLI. Install it with:
291264
@@ -311,10 +284,6 @@ npm run schema
311284
312285Apollo 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.
313286
314- {% hint style="info" %}
315- 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
316- {% endhint %}
317-
318287## Optimize query
319288
320289It 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] ( https://github.com/gajus/babel-plugin-graphql-tag ) .
0 commit comments