Skip to content

Commit

Permalink
chore(gatsby): migrate static-query-components reducer to TypeScript (#…
Browse files Browse the repository at this point in the history
…23475)

This commit migrates the `static-query-components` reducer to
TypeScript with few adjustments to the existing IGatsbyState
interface.

The main adjustment is the update of the type of the key used for
the map storing each static query from `number` to `string`.
This change reflect the fact that the key is described as being
the `id` field of the interface, and this field is described as a
`string` in the object definition.
  • Loading branch information
hiwelo authored Apr 27, 2020
1 parent 9e72691 commit bb38afd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby/src/redux/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const reduxNodes = require(`./nodes`)
const lokiNodes = require(`../../db/loki/nodes`).reducer
import { redirectsReducer } from "./redirects"
import { staticQueryComponentsReducer } from "./static-query-components"
import { reducer as logReducer } from "gatsby-cli/lib/reporter/redux/reducer"

const backend = process.env.GATSBY_DB_NODES || `redux`
Expand Down Expand Up @@ -56,7 +57,7 @@ module.exports = {
status: require(`./status`),
componentDataDependencies: require(`./component-data-dependencies`),
components: require(`./components`),
staticQueryComponents: require(`./static-query-components`),
staticQueryComponents: staticQueryComponentsReducer,
jobs: require(`./jobs`),
jobsV2: require(`./jobsv2`),
webpack: require(`./webpack`),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
module.exports = (state = new Map(), action) => {
import { ActionsUnion, IGatsbyState } from "../types"

export const staticQueryComponentsReducer = (
state: IGatsbyState["staticQueryComponents"] = new Map(),
action: ActionsUnion
): IGatsbyState["staticQueryComponents"] => {
switch (action.type) {
case `DELETE_CACHE`:
return new Map()
Expand Down
34 changes: 26 additions & 8 deletions packages/gatsby/src/redux/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export interface IGatsbyPluginContext {
[key: string]: (...args: any[]) => any
}

export interface IGatsbyStaticQueryComponents {
name: string
componentPath: SystemPath
id: Identifier
query: string
hash: string
}

type GatsbyNodes = Map<string, IGatsbyNode>

export interface IGatsbyState {
Expand Down Expand Up @@ -139,14 +147,8 @@ export interface IGatsbyState {
}
>
staticQueryComponents: Map<
number,
{
name: string
componentPath: SystemPath
id: Identifier
query: string
hash: string
}
IGatsbyStaticQueryComponents["id"],
IGatsbyStaticQueryComponents
>
// @deprecated
jobs: {
Expand Down Expand Up @@ -207,6 +209,7 @@ export interface ICachedReduxState {

export type ActionsUnion =
| ICreatePageDependencyAction
| IDeleteCacheAction
| IDeleteComponentDependenciesAction
| IReplaceComponentQueryAction
| IReplaceStaticQueryAction
Expand All @@ -220,6 +223,7 @@ export type ActionsUnion =
| ICreateTypes
| ICreateFieldExtension
| IPrintTypeDefinitions
| IRemoveStaticQuery

export interface ICreatePageDependencyAction {
type: `CREATE_COMPONENT_DEPENDENCY`
Expand Down Expand Up @@ -359,3 +363,17 @@ export interface ICreateRedirectAction {
type: `CREATE_REDIRECT`
payload: IRedirect
}

export interface IDeleteCacheAction {
type: `DELETE_CACHE`
}

export interface IReplaceStaticQueryAction {
type: `REPLACE_STATIC_QUERY`
payload: IGatsbyStaticQueryComponents
}

export interface IRemoveStaticQuery {
type: `REMOVE_STATIC_QUERY`
payload: IGatsbyStaticQueryComponents["id"]
}

0 comments on commit bb38afd

Please sign in to comment.