@slickgrid-universal/graphql 4.0.3 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,143 @@
1
+ /**
2
+ * This GraphqlQueryBuilder class is a lib that already existed
3
+ * but was causing issues with TypeScript, RequireJS and other bundler/packagers,
4
+ * so I simply rewrote the code in TypeScript to make it easier to import.
5
+ *
6
+ * The previous lib can be found here at this Github link:
7
+ * https://github.com/codemeasandwich/graphql-query-builder
8
+ * With an MIT licence that and can be found at
9
+ * https://github.com/codemeasandwich/graphql-query-builder/blob/master/LICENSE
10
+ */
11
+ export default class GraphqlQueryBuilder {
12
+ // eslint-disable-next-line @typescript-eslint/ban-types
13
+ alias!: string | Function;
14
+ head: any[] = [];
15
+ body: any;
16
+
17
+ /* Constructor, query/mutator you wish to use, and an alias or filter arguments. */
18
+ // eslint-disable-next-line @typescript-eslint/ban-types
19
+ constructor(protected queryFnName: string, aliasOrFilter?: string | object) {
20
+ if (typeof aliasOrFilter === 'string') {
21
+ this.alias = aliasOrFilter;
22
+ } else if (typeof aliasOrFilter === 'object') {
23
+ this.filter(aliasOrFilter);
24
+ } else if (aliasOrFilter === undefined && arguments.length === 2) {
25
+ throw new TypeError(`You have passed undefined as Second argument to "Query"`);
26
+ } else if (aliasOrFilter !== undefined) {
27
+ throw new TypeError(`Second argument to "Query" should be an alias name(String) or filter arguments(Object). What was passed is: ${aliasOrFilter}`);
28
+ }
29
+ }
30
+
31
+ /**
32
+ * The parameters to run the query against.
33
+ * @param filters An object mapping attribute to values
34
+ */
35
+ filter(filters: any) {
36
+ for (const prop of Object.keys(filters)) {
37
+ if (typeof filters[prop] === 'function') {
38
+ continue;
39
+ }
40
+ const val = this.getGraphQLValue(filters[prop]);
41
+ if (val === '{}') {
42
+ continue;
43
+ }
44
+ this.head.push(`${prop}:${val}`);
45
+ }
46
+ return this;
47
+ }
48
+
49
+ /**
50
+ * Outlines the properties you wish to be returned from the query.
51
+ * @param properties representing each attribute you want Returned
52
+ */
53
+ find(...searches: any[]) { // THIS NEED TO BE A "FUNCTION" to scope 'arguments'
54
+ if (!searches || !Array.isArray(searches) || searches.length === 0) {
55
+ throw new TypeError(`find value can not be >>falsy<<`);
56
+ }
57
+ // if its a string.. it may have other values
58
+ // else it sould be an Object or Array of maped values
59
+ const searchKeys = (searches.length === 1 && Array.isArray(searches[0])) ? searches[0] : searches;
60
+ this.body = this.parceFind(searchKeys);
61
+ return this;
62
+ }
63
+
64
+ /**
65
+ * set an alias for this result.
66
+ * @param alias
67
+ */
68
+ setAlias(alias: string) {
69
+ this.alias = alias;
70
+ }
71
+
72
+ /**
73
+ * Return to the formatted query string
74
+ * @return
75
+ */
76
+ toString() {
77
+ if (this.body === undefined) {
78
+ throw new ReferenceError(`return properties are not defined. use the 'find' function to defined them`);
79
+ }
80
+
81
+ return `${(this.alias) ? (this.alias + ':') : ''} ${this.queryFnName} ${(this.head.length > 0) ? '(' + this.head.join(',') + ')' : ''} { ${this.body} }`;
82
+ }
83
+
84
+ // --
85
+ // protected functions
86
+ // --------------------
87
+
88
+ protected parceFind(_levelA: any[]) {
89
+ const propsA = _levelA.map((_currentValue, index) => {
90
+ const itemX = _levelA[index];
91
+
92
+ if (itemX instanceof GraphqlQueryBuilder) {
93
+ return itemX.toString();
94
+ } else if (!Array.isArray(itemX) && typeof itemX === 'object') {
95
+ const propsAA = Object.keys(itemX);
96
+ if (1 !== propsAA.length) {
97
+ throw new RangeError(`Alias objects should only have one value. was passed: ${JSON.stringify(itemX)}`);
98
+ }
99
+ const propS = propsAA[0];
100
+ const item = itemX[propS];
101
+
102
+ if (Array.isArray(item)) {
103
+ return new GraphqlQueryBuilder(propS).find(item);
104
+ }
105
+ return `${propS} : ${item} `;
106
+ } else if (typeof itemX === 'string') {
107
+ return itemX;
108
+ } else {
109
+ throw new RangeError(`cannot handle Find value of ${itemX}`);
110
+ }
111
+ });
112
+
113
+ return propsA.join(',');
114
+ }
115
+
116
+ protected getGraphQLValue(value: any) {
117
+ if (typeof value === 'string') {
118
+ value = JSON.stringify(value);
119
+ } else if (Array.isArray(value)) {
120
+ value = value.map(item => {
121
+ return this.getGraphQLValue(item);
122
+ }).join();
123
+ value = `[${value}]`;
124
+ } else if (value instanceof Date) {
125
+ value = JSON.stringify(value);
126
+ } else if (value !== null && typeof value === 'object') {
127
+ value = this.objectToString(value);
128
+ }
129
+ return value;
130
+ }
131
+
132
+ protected objectToString(obj: any) {
133
+ const sourceA = [];
134
+
135
+ for (const prop of Object.keys(obj)) {
136
+ if (typeof obj[prop] === 'function') {
137
+ continue;
138
+ }
139
+ sourceA.push(`${prop}:${this.getGraphQLValue(obj[prop])}`);
140
+ }
141
+ return `{${sourceA.join()}}`;
142
+ }
143
+ }
@@ -0,0 +1,2 @@
1
+ export * from './graphql.service';
2
+ export { default as GraphqlQueryBuilder } from './graphqlQueryBuilder';