File tree 2 files changed +90
-3
lines changed
packages/gatsby-graphiql-explorer/src/app
2 files changed +90
-3
lines changed Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
1
+ export function removeQueryName ( query ) {
2
+ return query . replace (
3
+ / ^ [ ^ { ( ] + ( [ { ( ] ) / ,
4
+ ( _match , openingCurlyBracketsOrParenthesis ) =>
5
+ `query ${ openingCurlyBracketsOrParenthesis } `
6
+ )
7
+ }
8
+
1
9
const getQuery = ( arg , spaceCount ) => {
2
10
const { operationDataList } = arg
3
11
const { query } = operationDataList [ 0 ]
4
- const anonymousQuery = query . replace ( / q u e r y \s . + { / gim , `{` )
12
+ const anonymousQuery = removeQueryName ( query )
5
13
return (
6
14
` ` . repeat ( spaceCount ) +
7
15
anonymousQuery . replace ( / \n / g, `\n` + ` ` . repeat ( spaceCount ) )
@@ -16,13 +24,13 @@ const pageQuery = {
16
24
generate : arg => `import React from "react"
17
25
import { graphql } from "gatsby"
18
26
19
- const ComponentName = ({ data }) => <pre>{JSON.stringify(data, null, 4)}</pre>
27
+ const Page = ({ data }) => <pre>{JSON.stringify(data, null, 4)}</pre>
20
28
21
29
export const query = graphql\`
22
30
${ getQuery ( arg , 2 ) }
23
31
\`
24
32
25
- export default ComponentName
33
+ export default Page
26
34
27
35
` ,
28
36
}
You can’t perform that action at this time.
0 commit comments