Skip to content

Commit

Permalink
fix(graphql): update variables when query changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hjvedvik committed Sep 16, 2019
1 parent d35ec39 commit 0fc0056
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
7 changes: 5 additions & 2 deletions gridsome/lib/pages/__tests__/pages.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ test('create page with pagination', async () => {
expect(route.path).toEqual('/page/:page(\\d+)?/')
expect(route.internal.path).toEqual('/page')
expect(route.internal.regexp).toEqual(/^\/page(?:\/(\d+))?(?:\/)?$/i)
expect(route.internal.query.paginate).toEqual(true)
expect(route.internal.query.directives).toHaveProperty('paginate')
})

test('create page with custom context', async () => {
Expand All @@ -161,14 +161,17 @@ test('create page with query context', async () => {
})
})

const queryVariables = { id: '1' }

const page = pages.createPage({
path: '/page',
component: './__fixtures__/MovieTemplate.vue',
queryVariables: { id: '1' }
queryVariables
})

expect(page.context).toMatchObject({})
expect(page.internal.query.variables).toMatchObject({ id: '1' })
expect(page.internal.queryVariables).toEqual(queryVariables)
})

test('always include a /404 page', async () => {
Expand Down
39 changes: 24 additions & 15 deletions gridsome/lib/pages/pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,29 @@ class Pages {
)

const options = this._createRouteOptions(validated, meta)
const route = this._routes.by('id', options.id)
const oldOptions = this._routes.by('id', options.id)
const newOptions = Object.assign({}, options, {
$loki: route.$loki,
meta: route.meta
$loki: oldOptions.$loki,
meta: oldOptions.meta
})

this._routes.update(newOptions)

return new Route(newOptions, this)
const route = new Route(newOptions, this)

if (options.internal.query.source !== oldOptions.internal.query.source) {
for (const page of route.pages()) {
const vars = page.internal.queryVariables || page.context || {}
const { paginate, variables, filters } = this._createPageQuery(route.internal.query, vars)

const newOptions = { ...page }
newOptions.internal.query = { paginate, variables, filters }

this._pages.update(newOptions)
}
}

return route
}

removeRoute (id) {
Expand Down Expand Up @@ -273,8 +287,7 @@ class Pages {
_createRouteOptions (options, meta = {}) {
const component = this.app.resolve(options.component)
const { pageQuery } = this._parseComponent(component)
const parsedQuery = this._parseQuery(pageQuery, component)
const { source, document, paginate } = this._createPageQuery(parsedQuery)
const query = this._parseQuery(pageQuery, component)
const { permalinks: { trailingSlash }} = this.app.config

let path = options.path.replace(/\/+/g, '/')
Expand All @@ -289,7 +302,7 @@ class Pages {
name = name || `__${snakeCase(path)}`
}

if (paginate) {
if (query.directives.paginate) {
path = trimEnd(path, '/') + '/:page(\\d+)?' + (hasTrailingSlash ? '/' : '')
}

Expand All @@ -314,12 +327,8 @@ class Pages {
isDynamic,
priority,
regexp,
keys,
query: {
source,
document,
paginate: !!paginate
}
query,
keys
})
})
}
Expand Down Expand Up @@ -495,15 +504,15 @@ class Route {
}

const vars = queryVariables || context || {}
const parsedQuery = this._factory._parseQuery(query.source, this.component)
const { paginate, variables, filters } = this._factory._createPageQuery(parsedQuery, vars)
const { paginate, variables, filters } = this._factory._createPageQuery(query, vars)

return this._createPage.call({
id,
path,
publicPath,
context,
internal: {
queryVariables,
route: this.id,
digest,
isManaged,
Expand Down
2 changes: 1 addition & 1 deletion gridsome/lib/server/middlewares/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module.exports = ({ pages }) => {

// page/1/index.html is not statically generated
// in production and should return 404 in develop
if (route.internal.query.paginate) {
if (route.internal.query.directives.paginate) {
currentPage = parseInt(params.page, 10) || 0

if (params.page && currentPage <= 1) {
Expand Down

0 comments on commit 0fc0056

Please sign in to comment.