-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
constants.ts
84 lines (74 loc) · 2.12 KB
/
constants.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
import { editor, Uri } from 'monaco-graphql/esm/monaco-editor';
import { initializeMode } from 'monaco-graphql/esm/initializeMode';
type ModelType = 'operations' | 'variables' | 'response';
export const GRAPHQL_URL = 'https://countries.trevorblades.com';
export const DEFAULT_EDITOR_OPTIONS: editor.IStandaloneEditorConstructionOptions =
{
theme: 'vs-dark',
minimap: {
enabled: false,
},
};
export const STORAGE_KEY = {
operations: 'operations',
variables: 'variables',
};
export const DEFAULT_VALUE: Record<ModelType, string> = {
operations:
localStorage.getItem(STORAGE_KEY.operations) ??
`# CMD/CTRL + Return/Enter will execute the operation,
# same in the variables editor below
# also available via context menu & F1 command palette
query($code: ID!) {
country(code: $code) {
awsRegion
native
phone
}
}`,
variables:
localStorage.getItem(STORAGE_KEY.variables) ??
`{
"code": "UA"
}`,
response: '',
};
export const FILE_SYSTEM_PATH: Record<
ModelType,
`${string}.${'graphql' | 'json'}`
> = {
operations: 'operations.graphql',
variables: 'variables.json',
response: 'response.json',
};
export const MONACO_GRAPHQL_API = initializeMode({
diagnosticSettings: {
validateVariablesJSON: {
[Uri.file(FILE_SYSTEM_PATH.operations).toString()]: [
Uri.file(FILE_SYSTEM_PATH.variables).toString(),
],
},
jsonDiagnosticSettings: {
validate: true,
schemaValidation: 'error',
// set these again, because we are entirely re-setting them here
allowComments: true,
trailingCommas: 'ignore',
},
},
});
export const MODEL: Record<ModelType, editor.ITextModel> = {
operations: getOrCreateModel('operations'),
variables: getOrCreateModel('variables'),
response: getOrCreateModel('response'),
};
function getOrCreateModel(
type: 'operations' | 'variables' | 'response',
): editor.ITextModel {
const uri = Uri.file(FILE_SYSTEM_PATH[type]);
const defaultValue = DEFAULT_VALUE[type];
const language = uri.path.split('.').pop();
return (
editor.getModel(uri) ?? editor.createModel(defaultValue, language, uri)
);
}