Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby): Translate redux/nodes to TypeScript #21010

Merged
merged 15 commits into from
Mar 23, 2020
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ module.exports = {
plugins: ["@typescript-eslint/eslint-plugin"],
rules: {
...TSEslint.configs.recommended.rules,
// This rule tries to ensure we use camelCase for all variables, properties
// functions, etc. However, it is not always possible to ensure properties
// are camelCase. Specifically we have `node.__gatsby_resolve` which breaks
// this rule. This allows properties to be whatever they need to be.
"@typescript-eslint/camelcase": ["error", { properties: "never" }],
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
// This rule tries to prevent using `require()`. However in node code,
// there are times where this makes sense. And it specifically is causing
// problems in our tests where we often want this functionality for module
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/commands/build-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
getPageHtmlFilePath,
} from "../utils/page-html"
import { remove as removePageDataFile, fixedPagePath } from "../utils/page-data"
import { IReduxState } from "../redux/types"
import { IGatsbyState } from "../redux/types"

export const getChangedPageDataKeys = (
state: IReduxState,
state: IGatsbyState,
cachedPageData: Map<string, string>
): string[] => {
if (cachedPageData && state.pageData) {
Expand All @@ -31,7 +31,7 @@ export const getChangedPageDataKeys = (
}

export const collectRemovedPageData = (
state: IReduxState,
state: IGatsbyState,
cachedPageData: Map<string, string>
): string[] => {
if (cachedPageData && state.pageData) {
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/query/graphql-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {
ExecutionResult,
} from "graphql"
import { debounce } from "lodash"
import nodeStore from "../db/nodes"
import * as nodeStore from "../db/nodes"
import { createPageDependency } from "../redux/actions/add-page-dependency"

import withResolverContext from "../schema/context"
import { LocalNodeModel } from "../schema/node-model"
import { Store } from "redux"
import { IReduxState } from "../redux/types"
import { IGatsbyState } from "../redux/types"

type Query = string | Source

Expand All @@ -30,7 +30,7 @@ class GraphQLRunner {
validDocuments: WeakSet<DocumentNode>
scheduleClearCache: () => void

constructor(protected store: Store<IReduxState>) {
constructor(protected store: Store<IGatsbyState>) {
const { schema, schemaCustomization } = this.store.getState()

this.nodeModel = new LocalNodeModel({
Expand Down
6 changes: 3 additions & 3 deletions packages/gatsby/src/redux/actions/add-page-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const createPageDependency = ({
}: {
path: string
nodeId: string
connection: string
connection?: string
}): void => {
const { componentDataDependencies } = store.getState()

Expand All @@ -23,7 +23,7 @@ export const createPageDependency = ({
if (
nodeId &&
componentDataDependencies.nodes.has(nodeId) &&
componentDataDependencies.nodes.get(nodeId).has(path)
componentDataDependencies.nodes.get(nodeId)!.has(path)
blainekasten marked this conversation as resolved.
Show resolved Hide resolved
) {
nodeDependencyExists = true
}
Expand All @@ -33,7 +33,7 @@ export const createPageDependency = ({
if (
connection &&
componentDataDependencies.connections.has(connection) &&
componentDataDependencies.connections.get(connection).has(path)
componentDataDependencies.connections.get(connection)!.has(path)
) {
connectionDependencyExists = true
}
Expand Down
32 changes: 1 addition & 31 deletions packages/gatsby/src/redux/actions/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,14 @@ import {
/**
* Create a dependency between a page and data. Probably for
* internal use only.
* @param {Object} $0
* @param {string} $0.path the path to the page
* @param {string} $0.nodeId A node ID
* @param {string} $0.connection A connection type
* @private
*/
export const createPageDependency = (
{
path,
nodeId,
connection,
}: { path: string; nodeId: string; connection: string },
}: { path: string; nodeId?: string; connection?: string },
plugin = ``
): ICreatePageDependencyAction => {
return {
Expand All @@ -44,7 +40,6 @@ export const createPageDependency = (
/**
* Delete dependencies between an array of pages and data. Probably for
* internal use only. Used when deleting pages.
* @param {Array} paths the paths to delete.
* @private
*/
export const deleteComponentsDependencies = (
Expand Down Expand Up @@ -99,11 +94,6 @@ export const replaceStaticQuery = (
*
* Report that a query has been extracted from a component. Used by
* query-compiler.js.
*
* @param {Object} $0
* @param {componentPath} $0.componentPath The path to the component that just had
* its query read.
* @param {query} $0.query The GraphQL query that was extracted from the component.
* @private
*/
export const queryExtracted = (
Expand All @@ -122,11 +112,6 @@ export const queryExtracted = (
/**
*
* Report that the Relay Compiler found a graphql error when attempting to extract a query
*
* @param {Object} $0
* @param {componentPath} $0.componentPath The path to the component that just had
* its query read.
* @param {error} $0.error The GraphQL query that was extracted from the component.
* @private
*/
export const queryExtractionGraphQLError = (
Expand All @@ -146,10 +131,6 @@ export const queryExtractionGraphQLError = (
*
* Report that babel was able to extract the graphql query.
* Indicates that the file is free of JS errors.
*
* @param {Object} $0
* @param {componentPath} $0.componentPath The path to the component that just had
* its query read.
* @private
*/
export const queryExtractedBabelSuccess = (
Expand All @@ -168,11 +149,6 @@ export const queryExtractedBabelSuccess = (
/**
*
* Report that the Relay Compiler found a babel error when attempting to extract a query
*
* @param {Object} $0
* @param {componentPath} $0.componentPath The path to the component that just had
* its query read.
* @param {error} $0.error The Babel error object
* @private
*/
export const queryExtractionBabelError = (
Expand All @@ -190,8 +166,6 @@ export const queryExtractionBabelError = (

/**
* Set overall program status e.g. `BOOTSTRAPING` or `BOOTSTRAP_FINISHED`.
*
* @param {string} Program status
* @private
*/
export const setProgramStatus = (
Expand All @@ -209,8 +183,6 @@ export const setProgramStatus = (

/**
* Broadcast that a page's query was run.
*
* @param {string} Path to the page component that changed.
* @private
*/
export const pageQueryRun = (
Expand All @@ -228,8 +200,6 @@ export const pageQueryRun = (

/**
* Remove jobs which are marked as stale (inputPath doesn't exists)
*
* @param {string} contentDigest
* @private
*/
export const removeStaleJob = (
Expand Down
20 changes: 11 additions & 9 deletions packages/gatsby/src/redux/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import mitt from "mitt"
import thunk from "redux-thunk"
import reducers from "./reducers"
import { writeToCache, readFromCache } from "./persist"
import { IReduxState, ActionsUnion } from "./types"
import { IGatsbyState, ActionsUnion } from "./types"

// Create event emitter for actions
export const emitter = mitt()

// Read old node data from cache.
export const readState = (): IReduxState => {
export const readState = (): IGatsbyState => {
try {
const state = readFromCache() as IReduxState
const state = readFromCache() as IGatsbyState
if (state.nodes) {
// re-create nodesByType
state.nodesByType = new Map()
state.nodes.forEach(node => {
const { type } = node.internal
if (!state.nodesByType!.has(type)) {
state.nodesByType!.set(type, new Map())
if (!state.nodesByType.has(type)) {
state.nodesByType.set(type, new Map())
}
state.nodesByType!.get(type).set(node.id, node)
state.nodesByType.get(type)!.set(node.id, node)
})
}

Expand All @@ -42,7 +42,7 @@ export const readState = (): IReduxState => {
}
// BUG: Would this not cause downstream bugs? seems likely. Why wouldn't we just
// throw and kill the program?
return {} as IReduxState
return {} as IGatsbyState
}

/**
Expand All @@ -53,9 +53,11 @@ const multi: Middleware = ({ dispatch }) => next => (
): ActionsUnion | ActionsUnion[] =>
Array.isArray(action) ? action.filter(Boolean).map(dispatch) : next(action)

export const configureStore = (initialState: IReduxState): Store<IReduxState> =>
export const configureStore = (
initialState: IGatsbyState
): Store<IGatsbyState> =>
createStore(
combineReducers<IReduxState>({ ...reducers }),
combineReducers<IGatsbyState>({ ...reducers }),
initialState,
applyMiddleware(thunk, multi)
)
Expand Down
Loading