-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
snippets.js
55 lines (51 loc) · 1.37 KB
/
snippets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const removeQueryName = query =>
query.replace(
/^[^{(]+([{(])/,
(_match, openingCurlyBracketsOrParenthesis) =>
`query ${openingCurlyBracketsOrParenthesis}`,
);
const getQuery = (arg, spaceCount) => {
const { operationDataList } = arg;
console.log(arg);
const { query } = operationDataList[0];
const anonymousQuery = removeQueryName(query);
return (
' '.repeat(spaceCount) +
anonymousQuery.replaceAll('\n', '\n' + ' '.repeat(spaceCount))
);
};
export const getSnippets = ({ serverUrl }) => {
const exampleSnippetZero = {
name: 'cURL',
language: 'shell',
codeMirrorMode: 'shell',
options: [],
generate: arg => `curl -g \
-X POST \
-H "Content-Type: application/json" \
-d '{"query": "${arg.operationDataList[0].query.replaceAll('\n', ' ')}"}' \
${serverUrl}`,
};
const exampleSnippetOne = {
name: 'Example One',
language: 'JavaScript',
codeMirrorMode: 'jsx',
options: [],
generate: arg => `export const query = graphql\`
${getQuery(arg, 2)}
\`
`,
};
const exampleSnippetTwo = {
name: 'Example Two',
language: 'JavaScript',
codeMirrorMode: 'jsx',
options: [],
generate: arg => `import { graphql } from 'graphql'
export const query = graphql\`
${getQuery(arg, 2)}
\`
`,
};
return [exampleSnippetZero, exampleSnippetOne, exampleSnippetTwo];
};