This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
drop.ts
188 lines (157 loc) · 6.32 KB
/
drop.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {flags} from '@oclif/command'
import {BaseCommand} from '../lib'
import {cli} from 'cli-ux'
import {getTypesDiff, getFieldsDiff} from '../lib/schema-parser/getdiff'
import {Backend} from '../lib/backend'
const DROP_DATA = `
mutation {
dropData(allData: true) {
response { code message }
}
}`
const DROP_SCHEMA = `
mutation {
dropData(allDataAndSchema: true) {
response { code message }
}
}`
const DROP_TYPES = `
mutation($types: [String!]) {
dropData(types: $types) {
response { code message }
}
}
`
const DROP_FIELDS = `
mutation($fields: [String!]) {
dropData(fields: $fields) {
response { code message }
}
}
`
export default class Drop extends BaseCommand {
static description = 'Drop all data in your backend'
static examples = [
'$ slash-graphql drop -e https://frozen-mango.cloud.dgraph.io/graphql -t <apiToken> [-l] [-d] [-s] [-T <types>] [-F <fields>]',
]
static getOtherFlags(excludeFlag: string) {
const options = ['list-unused', 'drpo-unused', 'drop-data', 'drop-schema', 'drop-types', 'drop-fields']
const index = options.indexOf(excludeFlag)
options.splice(index, 1)
return options
}
static flags = {
...BaseCommand.commonFlags,
...BaseCommand.endpointFlags,
'list-unused': flags.boolean({char: 'l', description: 'List unused types and fields', default: false, exclusive: Drop.getOtherFlags('list-unused')}),
'drop-unused': flags.boolean({char: 'u', description: 'Drops all unused types and fields', default: false, exclusive: Drop.getOtherFlags('drop-unused')}),
'drop-data': flags.boolean({char: 'd', description: 'Drop data and leave the schema', default: false, exclusive: Drop.getOtherFlags('drop-data')}),
'drop-schema': flags.boolean({char: 's', description: 'Drop Schema along with the data', default: false, exclusive: Drop.getOtherFlags('drop-schema')}),
'drop-types': flags.string({char: 'T', description: 'Drop types', multiple: true, exclusive: ['list-unused', 'drop-unused', 'drop-data', 'drop-schema']}),
'drop-fields': flags.string({char: 'F', description: 'Drop types', multiple: true, exclusive: ['list-unused', 'drop-unused', 'drop-data', 'drop-schema']}),
confirm: flags.boolean({char: 'y', description: 'Skip Confirmation', default: false}),
}
confirm(message: string) {
this.log(message)
return cli.confirm('Are you sure you want to proceed?')
}
handleResult(errors: [{message: string}] | undefined, successMessage: string | '') {
if (errors) {
for (const {message} of errors) {
this.error(message)
}
return
}
this.log(successMessage)
}
async listUnused(backend: Backend) {
const opts = this.parse(Drop)
const {data: graphqlex, errors: graphqlexErrors} = await backend.query<{data: object }>('schema { __typename }')
const {data: graphql, errors: graphqlErrors} = await backend.adminQuery<{getGQLSchema: { schema: string }}>('{ getGQLSchema { schema } }')
if (graphqlErrors || graphqlexErrors) {
this.handleResult(graphqlErrors, '')
this.handleResult(graphqlexErrors, '')
}
const unusedTypes = getTypesDiff(graphql.getGQLSchema.schema, graphqlex)
const unusedFields = getFieldsDiff(graphql.getGQLSchema.schema, graphqlex)
const unusedTypesList = unusedTypes.map((type: string) => ({name: type, type: 'type'}))
const unusedFieldsList = unusedFields.map((field: string) => ({name: field, type: 'field'}))
const unused = [...unusedTypesList, ...unusedFieldsList]
if (unused.length === 0) {
this.log('There are no unused types or fields')
return
}
cli.table(unused, {Name: {get: ({name}) => name}, Type: {get: ({type}) => type}}, {printLine: this.log, ...opts.flags})
return {
types: unusedTypes,
fields: unusedFields,
}
}
async dropUnused(backend: Backend, dropTypes: string[], dropFields: string[]) {
// TODO: fix proxy and do these in a single query
if (dropTypes) {
const {errors} = await backend.slashAdminQuery<{dropData: {response: {code: string; message: string}}}>(DROP_TYPES, {
types: dropTypes,
})
if (errors) {
this.handleResult(errors, '')
}
}
if (dropFields) {
const {errors} = await backend.slashAdminQuery<{dropData: {response: {code: string; message: string}}}>(DROP_FIELDS, {
fields: dropFields,
})
if (errors) {
this.handleResult(errors, '')
}
}
this.log('Successfully dropped listed types/fields')
}
async run() {
const opts = this.parse(Drop)
const backend = await this.backendFromOpts(opts)
if (opts.flags['list-unused']) {
await this.listUnused(backend)
return
}
if (opts.flags['drop-unused']) {
const unused = await this.listUnused(backend)
if (!unused) return
if (opts.flags.confirm || await this.confirm('This will drop the listed unused types/fields, and cannot be reversed')) {
await this.dropUnused(backend, unused.types, unused.fields)
} else {
this.log('Aborting')
}
return
}
if (opts.flags['drop-schema']) {
if (opts.flags.confirm || await this.confirm('This will drop all data and schema in your backend, and cannot be reversed')) {
const {errors} = await backend.slashAdminQuery<{dropData: {response: {code: string; message: string}}}>(DROP_SCHEMA)
this.handleResult(errors, 'Successfully dropped all data and schema!')
} else {
this.log('Aborting')
}
return
}
if (opts.flags['drop-data']) {
if (opts.flags.confirm || await this.confirm('This will drop all data in your backend, and cannot be reversed')) {
const {errors} = await backend.slashAdminQuery<{dropData: {response: {code: string; message: string}}}>(DROP_DATA)
this.handleResult(errors, 'Successfully dropped all data!')
} else {
this.log('Aborting')
}
return
}
const dropTypes = opts.flags['drop-types']
const dropFields = opts.flags['drop-fields']
if (dropTypes || dropFields) {
if (opts.flags.confirm || await this.confirm('This will drop the listed unused types/fields, and cannot be reversed')) {
await this.dropUnused(backend, dropTypes, dropFields)
} else {
this.log('Aborting')
}
return
}
this.log('No options provided. Use drop --help to learn about the options.')
}
}