Skip to content

Commit

Permalink
fix(gatsby): Fix static query parsing for for a special case (#21551)
Browse files Browse the repository at this point in the history
* fix(gatsby): Fix static query parsing for for a special case

* Add a comment
  • Loading branch information
vladar authored Feb 19, 2020
1 parent ebfa8c3 commit dd344ac
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,79 @@ query {
},
"text": "query StaticQueryName { foo }",
},
Object {
"doc": Object {
"definitions": Array [
Object {
"directives": Array [],
"kind": "OperationDefinition",
"loc": Object {
"end": 29,
"start": 0,
},
"name": Object {
"kind": "Name",
"loc": Object {
"end": 21,
"start": 6,
},
"value": "StaticQueryName",
},
"operation": "query",
"selectionSet": Object {
"kind": "SelectionSet",
"loc": Object {
"end": 29,
"start": 22,
},
"selections": Array [
Object {
"alias": undefined,
"arguments": Array [],
"directives": Array [],
"kind": "Field",
"loc": Object {
"end": 27,
"start": 24,
},
"name": Object {
"kind": "Name",
"loc": Object {
"end": 27,
"start": 24,
},
"value": "foo",
},
"selectionSet": undefined,
},
],
},
"variableDefinitions": Array [],
},
],
"kind": "Document",
"loc": Object {
"end": 29,
"start": 0,
},
},
"filePath": "static-query-hooks-with-other-export.js",
"hash": 407706182,
"isHook": true,
"isStaticQuery": true,
"templateLoc": SourceLocation {
"end": Position {
"column": 67,
"line": 4,
},
"filename": true,
"start": Position {
"column": 38,
"line": 4,
},
},
"text": "query StaticQueryName { foo }",
},
Object {
"doc": Object {
"definitions": Array [
Expand Down
12 changes: 11 additions & 1 deletion packages/gatsby/src/query/__tests__/file-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ export default () => {
const data = useStaticQuery(graphql\`query StaticQueryName { foo }\`);
return <div>{data.doo}</div>;
}`,
"static-query-hooks-with-other-export.js": `import { graphql, useStaticQuery } from 'gatsby'
export { Bar } from 'bar'
export default () => {
const data = useStaticQuery(graphql\`query StaticQueryName { foo }\`);
return <div>{data.doo}</div>;
}
`,
"static-query-hooks-alternative-import.js": `import * as Gatsby from 'gatsby'
export default () => {
const data = Gatsby.useStaticQuery(Gatsby.graphql\`query StaticQueryName { foo }\`);
Expand Down Expand Up @@ -229,12 +236,15 @@ export default () => {
})

it(`extracts query AST correctly from files`, async () => {
const errors = []
const addError = errors.push.bind(errors)
const results = await parser.parseFiles(
Object.keys(MOCK_FILE_INFO),
jest.fn()
addError
)
expect(results).toMatchSnapshot()
expect(reporter.warn).toMatchSnapshot()
expect(errors.length).toEqual(1)
})

it(`generates spec-compliant query names out of path`, async () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/gatsby/src/query/file-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,15 @@ async function findGraphQLTags(
return binding
}

// Look for exported page queries
// When a component has a StaticQuery we scan all of its exports and follow those exported variables
// to determine if they lead to this static query (via tagged template literal)
traverse(ast, {
ExportNamedDeclaration(path, state) {
// Skipping the edge case of re-exporting (i.e. "export { bar } from 'Bar'")
// (it is handled elsewhere for queries, see usages of warnForUnknownQueryVariable)
if (path.node.source) {
return
}
path.traverse({
TaggedTemplateExpression,
ExportSpecifier(path) {
Expand Down

0 comments on commit dd344ac

Please sign in to comment.