const handleQuery = ( { components, staticQueryComponents }, query, component ) => { // If this is a static query // Add action / reducer + watch staticquery files if (query.isStaticQuery) { const isNewQuery = !staticQueryComponents.has(query.jsonName) if ( isNewQuery || staticQueryComponents.get(query.jsonName).query !== query.text ) { boundActionCreators.replaceStaticQuery({ name: query.name, componentPath: query.path, id: query.jsonName, jsonName: query.jsonName, query: query.text, hash: query.hash, }) debug( `Static query in ${component} ${ isNewQuery ? `was added` : `has changed` }.` ) boundActionCreators.deleteComponentsDependencies([query.jsonName]) queueQueryForPathname(query.jsonName) } return true // If this is page query } else if (components.has(component)) { if (components.get(component).query !== query.text) { boundActionCreators.replaceComponentQuery({ query: query.text, componentPath: component, }) debug( `Page query in ${component} ${ components.get(component).query.length === 0 ? `was added` : `has changed` }.` ) queueQueriesForPageComponent(component) } return true } return false }
components.forEach(component => { const query = queries.get(normalize(component)) boundActionCreators.replaceComponentQuery({ query: query && query.text, componentPath: component, }) })
const runQueriesForComponent = componentPath => { const pages = getPagesForComponent(componentPath) // Remove page data dependencies before re-running queries because // the changing of the query could have changed the data dependencies. // Re-running the queries will add back data dependencies. boundActionCreators.deletePagesDependencies(pages.map(p => p.path)) const component = store.getState().pageComponents[componentPath] return Promise.all(pages.map(p => queryRunner(p, component))) }
staticQueryComponents.forEach(c => { if (c.query !== `` && !queries.has(c.componentPath)) { debug(`Static query was removed from ${c.componentPath}`) store.dispatch({ type: `REMOVE_STATIC_QUERY`, payload: c.jsonName, }) boundActionCreators.deleteComponentsDependencies([c.jsonName]) } })
components.forEach(c => { if (c.query !== `` && !queries.has(c.componentPath)) { debug(`Page query was removed from ${c.componentPath}`) boundActionCreators.replaceComponentQuery({ query: ``, componentPath: c.componentPath, }) queueQueriesForPageComponent(c.componentPath) } })
const queueQueriesForPageComponent = componentPath => { const pages = getPagesForComponent(componentPath) // Remove page data dependencies before re-running queries because // the changing of the query could have changed the data dependencies. // Re-running the queries will add back data dependencies. boundActionCreators.deleteComponentsDependencies( pages.map(p => p.path || p.id) ) pages.forEach(page => queueQueryForPathname(page.path)) }
queries.forEach(({ text }, path) => { invariant( pages[path], `Path ${path} not found in the store pages: ${JSON.stringify(pages)}` ) if (text !== pages[path].query) { boundActionCreators.replacePageComponentQuery({ query: text, componentPath: path, }) runQueriesForComponent(path) } })
queries.forEach(({ text }, id) => { // Queries can be parsed from non page/layout components e.g. components // with fragments so ignore those. // // If the query has changed, set the new query in the store and run // its queries. if (components[id] && text !== components[id].query) { boundActionCreators.replaceComponentQuery({ query: text, componentPath: id, }) runQueriesForComponent(id) } })