This repository was archived by the owner on Jan 7, 2022. It is now read-only.
forked from charlypoly/graphql-to-json-schema
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdoc-exampleGenerator.ts
More file actions
114 lines (95 loc) · 2.68 KB
/
doc-exampleGenerator.ts
File metadata and controls
114 lines (95 loc) · 2.68 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { inspect } from 'util'
import { spawn } from 'child_process'
import { filter } from 'lodash'
import {
buildSchema,
graphqlSync,
IntrospectionQuery,
getIntrospectionQuery,
} from 'graphql'
import { fromIntrospectionQuery } from './lib/fromIntrospectionQuery'
const nativeScalarsToFilter = ['String', 'Int', 'Boolean']
const readmeSDL: string = `
type Todo {
id: String!
name: String!
completed: Boolean
color: Color
"A field that requires an argument"
colors(filter: [Color!]!): [Color!]!
}
input TodoInputType {
name: String!
completed: Boolean
color: Color=RED
}
enum Color {
"Red color"
RED
"Green color"
GREEN
}
type Query {
"A Query with 1 required argument and 1 optional argument"
todo(id: String!, isCompleted: Boolean=false): Todo
todos: [Todo]
}
type Mutation {
"A Mutation with 1 required argument"
create_todo(todo: TodoInputType!): Todo
"A Mutation with 2 required arguments"
update_todo(id: String!, todo: TodoInputType!): Todo
}
`
const readmeSchema = buildSchema(readmeSDL)
const introspectionQueryJSON = graphqlSync(
readmeSchema,
getIntrospectionQuery()
).data as IntrospectionQuery
const readmeResult = fromIntrospectionQuery(introspectionQueryJSON)
// Get rid of undefined values this way
const cleanedUpReadmeResult = JSON.parse(JSON.stringify(readmeResult))
const startsWithTestGenerator = (stringToTest: string) => {
return (stringToLookFor: string) =>
stringToTest.startsWith(`${stringToLookFor}:`)
}
const keyComparator = (a: string, b: string) => {
// description to the top
if (['description'].some(startsWithTestGenerator(a))) {
// If the other one also satisfies the test (which is impossible, but ok) then
// there is no sort change
return ['description'].some(startsWithTestGenerator(b)) ? 0 : -1
}
if (['description'].some(startsWithTestGenerator(b))) {
return 1
}
// Native Scalars to the bottom
if (nativeScalarsToFilter.some(startsWithTestGenerator(a))) {
// If the other one also satisfies the test, then no sort change
return nativeScalarsToFilter.some(startsWithTestGenerator(b)) ? 0 : 1
}
if (nativeScalarsToFilter.some(startsWithTestGenerator(b))) {
return -1
}
// Stay the same
return 0
}
const output = `
### Input
\`\`\`graphql${readmeSDL}\`\`\`
### Output
\`\`\`js
${inspect(cleanedUpReadmeResult, { depth: null, sorted: keyComparator })}
\`\`\`
`
console.log(`
<BEGIN OUTPUT>
${output}
<END OUTPUT>
`)
if (process.platform === 'darwin') {
const proc = spawn('pbcopy')
proc.stdin.write(output)
proc.stdin.end()
console.log('OUTPUT COPIED TO YOUR CLIPBOARD!!!\n')
}