Note:
<example>is meant to denote text replaced by you (including brackets).
// global dependencies
npm install -g knex| async function doSomething(data) { | |
| if(data.ok) { | |
| return await db.insert(data) | |
| } | |
| if(typeof data === 'string') { | |
| return new Alert(data) | |
| } | |
| if(data.type === 'result') { |
| // ./SomeClass.ts | |
| export default class SomeClass { | |
| public static myMethod(params: any) { | |
| ... do something | |
| } | |
| } | |
| // ./SomeComponent.tsx |
| /** | |
| * Copyright (c) 2015-present, Facebook, Inc. | |
| * | |
| * This source code is licensed under the MIT license found in the | |
| * LICENSE file in the root directory of this source tree. | |
| * | |
| * @format | |
| */ | |
| 'use strict'; |
If your project is using the apollo-client to communicate with your GraphQL backend, there is no need to include redux or mobx for the global state management; you can store this state in the same Apollo client you are already using. This will be a contrived example, but this post will cover:
apollo-client you are already using with the local graphqltypeDefs and codegen@graphql-codegen to create custom hooks for your queries and mutations| // src/Balloon/schema.js | |
| const gql = require("graphql-tag"); | |
| module.exports = gql` | |
| input BalloonInput { | |
| color: String! | |
| id: ID! | |
| } | |
| type Balloon { |
| // src/Balloon/queries.ts | |
| import gql from "graphql-tag"; | |
| // | |
| // βββ GQL TAGS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // | |
| export const GetSelectedBalloonLocal = gql` | |
| query GetSelectedBalloonLocal { | |
| selectedBalloon @client { |
| // src/Balloon/mutations.ts | |
| import { Resolver } from "apollo-client"; | |
| import gql from "graphql-tag"; | |
| // | |
| // βββ GQL TAGS βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| // | |
| export const SetSelectedBalloonLocal = gql` | |
| mutation SetSelectedBalloonLocal($selectedBalloon: BalloonInput!) { |
| // src/Balloon/defaults.ts | |
| import { IBalloon } from "../types/graphql"; // <-- we will auto-generate this file a bit later in the post | |
| const defaultSelectedBalloon: IBalloon = { | |
| color: "", | |
| id: "", | |
| }; | |
| export default { | |
| selectedBalloon: { ...defaultSelectedBalloon, __typename: "Balloon" }, |
| // src/client.ts | |
| import { InMemoryCache } from 'apollo-cache-inmemory'; | |
| import balloonDefaultCacheValues from './Balloon/defaults'; | |
| import { setSelectedBalloonMutationResolver } from './Balloon/mutations'; | |
| import balloonSchema from './Balloon | |
| const cache = new InMemoryCache(); | |
| export const client = new ApolloClient({ |