Skip to content

Commit 41f88f1

Browse files
Paul Scanlonpieh
Paul Scanlon
andauthored
feat(gatsby-graphiql-explorer): Amend Query name if available (#36110)
Co-authored-by: pieh <[email protected]>
1 parent 40810c4 commit 41f88f1

File tree

2 files changed

+90
-3
lines changed

2 files changed

+90
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { removeQueryName } from "../snippets"
2+
import { stripIndent } from "common-tags"
3+
4+
describe(`removeQueryName`, () => {
5+
function getFullQuery(startOfQuery) {
6+
return `${startOfQuery}
7+
allMarkdownRemark {
8+
nodes {
9+
excerpt
10+
}
11+
}
12+
}`
13+
}
14+
15+
it(`{`, () => {
16+
const startOfQuery = `{`
17+
expect(removeQueryName(getFullQuery(startOfQuery))).toMatchInlineSnapshot(`
18+
"{
19+
allMarkdownRemark {
20+
nodes {
21+
excerpt
22+
}
23+
}
24+
}"
25+
`)
26+
})
27+
28+
it(`query {`, () => {
29+
const startOfQuery = `query {`
30+
expect(removeQueryName(getFullQuery(startOfQuery))).toMatchInlineSnapshot(`
31+
"query {
32+
allMarkdownRemark {
33+
nodes {
34+
excerpt
35+
}
36+
}
37+
}"
38+
`)
39+
})
40+
41+
it(`query NameOfTheQuery {`, () => {
42+
const startOfQuery = `query NameOfTheQuery {`
43+
expect(removeQueryName(getFullQuery(startOfQuery))).toMatchInlineSnapshot(`
44+
"query {
45+
allMarkdownRemark {
46+
nodes {
47+
excerpt
48+
}
49+
}
50+
}"
51+
`)
52+
})
53+
54+
it(`query ($args: String) {`, () => {
55+
const startOfQuery = `query ($args: String) {`
56+
expect(removeQueryName(getFullQuery(startOfQuery))).toMatchInlineSnapshot(`
57+
"query ($args: String) {
58+
allMarkdownRemark {
59+
nodes {
60+
excerpt
61+
}
62+
}
63+
}"
64+
`)
65+
})
66+
67+
it(`query NameOfTheQuery ($args: String) {`, () => {
68+
const startOfQuery = `query NameOfTheQuery ($args: String) {`
69+
expect(removeQueryName(getFullQuery(startOfQuery))).toMatchInlineSnapshot(`
70+
"query ($args: String) {
71+
allMarkdownRemark {
72+
nodes {
73+
excerpt
74+
}
75+
}
76+
}"
77+
`)
78+
})
79+
})

packages/gatsby-graphiql-explorer/src/app/snippets.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
export function removeQueryName(query) {
2+
return query.replace(
3+
/^[^{(]+([{(])/,
4+
(_match, openingCurlyBracketsOrParenthesis) =>
5+
`query ${openingCurlyBracketsOrParenthesis}`
6+
)
7+
}
8+
19
const getQuery = (arg, spaceCount) => {
210
const { operationDataList } = arg
311
const { query } = operationDataList[0]
4-
const anonymousQuery = query.replace(/query\s.+{/gim, `{`)
12+
const anonymousQuery = removeQueryName(query)
513
return (
614
` `.repeat(spaceCount) +
715
anonymousQuery.replace(/\n/g, `\n` + ` `.repeat(spaceCount))
@@ -16,13 +24,13 @@ const pageQuery = {
1624
generate: arg => `import React from "react"
1725
import { graphql } from "gatsby"
1826
19-
const ComponentName = ({ data }) => <pre>{JSON.stringify(data, null, 4)}</pre>
27+
const Page = ({ data }) => <pre>{JSON.stringify(data, null, 4)}</pre>
2028
2129
export const query = graphql\`
2230
${getQuery(arg, 2)}
2331
\`
2432
25-
export default ComponentName
33+
export default Page
2634
2735
`,
2836
}

0 commit comments

Comments
 (0)