-
Notifications
You must be signed in to change notification settings - Fork 2
/
repl.bootstrap.js
executable file
·108 lines (90 loc) · 2.53 KB
/
repl.bootstrap.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
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
#!/usr/bin/env node
require('./register')
const {
asyncWalkResolverMap,
atNicely,
ExtendedResolver,
gql,
Schemata,
walkResolverMap,
} = require('.')
const { parse, print, printSchema } = require('graphql')
const { merge } = require('lodash')
let repl = require('repl')
// Patch Function.prototype to provide the signature in the REPL
Object.defineProperty(Function.prototype, 'signature', {
get() {
let source = this.toString()
if (source.startsWith('class')) {
let parts = /constructor\((.*?)\)\s*\{/.exec(source)
return `class ${this.name}(${parts[1]})`
}
return source.slice(0, this.toString().indexOf('{')).trim()
},
})
// End patching Function.prototype for 'signature'
let sdlA = Schemata.from(
` type Box { faces: Int color: String } type Query { box: Box } `,
{
box() {
return { faces: 6, color: 'tan' }
},
},
)
let sdlB = Schemata.from(
` type Chest { goldBars: Int } type Query { pirateTreasure: [Chest] } `,
{
pirateTreasure() {
return [{ goldBars: 23 }]
},
},
)
let merged = sdlA.merge(sdlB, { createMissingResolvers: true })
let schema = merged.schema
let context = merge(global, {
Schemata,
gql,
parse,
print,
printSchema,
merge,
repl,
at: atNicely,
walkResolverMap,
asyncWalkResolverMap,
ExtendedResolver,
graphql: require('graphql'),
sdlA,
sdlB,
merged,
schema,
})
const help =
`
Welcome to the Schemata string repl bootstrapping process, the
following objects are in scope for you to use
Schemata - the Schemata class
gql - template tag function for creating Schemata instances
graphql - require('graphql')
parse - converts SDL/IDL strings into AST nodes
print - converts AST nodes into a SDL/IDL string
printSchema - converts a GraphQLSchema object into SDL/IDL
merge - underscore's merge() function
at - useful function dynamically getting props of an object
walkResolverMap - a function that walks over a resolver map and makes
changes. Callback is (key, value, path, map)
asyncWalkResolverMap - \x1b[3masync version of the above\x1b[0m
ExtendedResolver - the resolver wrapper class
sdlA - Schemata instance
sdlB - Schemata instance
merged - Merged schemas of sdlA and sdlB
schema - GraphQLSchema instance of merged
`
Object.defineProperty(context, 'help', {
get() {
console.log(help,)
return help
},
})
console.log(help)
merge(repl.start('> ').context, context)