-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdoc-exampleGenerator.ts
152 lines (124 loc) · 3.37 KB
/
doc-exampleGenerator.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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: ID!
name: String!
completed: Boolean
color: Color
"A field that requires an argument"
colors(
filter: [Color!]!
): [Color!]!
}
type SimpleTodo {
id: ID!
name: String!
}
union TodoUnion = Todo | SimpleTodo
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: ID!,
"A default value of false"
isCompleted: Boolean=false
): Todo
"Returns a list (or null) that can contain null values"
todos(
"Required argument that is a list that cannot contain null values"
ids: [String!]!
): [Todo]
}
type Mutation {
"A Mutation with 1 required argument"
create_todo(
todo: TodoInputType!
): Todo!
"A Mutation with 2 required arguments"
update_todo(
id: ID!,
data: TodoInputType!
): Todo!
"Returns a list (or null) that can contain null values"
update_todos(
ids: [String!]!
data: TodoInputType!
): [Todo]
}
`
const readmeSchema = buildSchema(readmeSDL)
const introspectionQueryJSON = graphqlSync(
readmeSchema,
getIntrospectionQuery()
).data as IntrospectionQuery
const options = {
nullableArrayItems: true,
}
const readmeResult = fromIntrospectionQuery(introspectionQueryJSON, options)
// 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
// Output is from call to fromIntrospectionQuery with the following options:
const options = ${inspect(options)}
${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')
}