forked from atulmy/gql-query-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultMutationAdapter.ts
120 lines (109 loc) · 3.25 KB
/
DefaultMutationAdapter.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
/*
@class DefaultMutationAdapter
@desc A basic implementation to use
@desc modify the output of the mutation template by passing a second argument to mutation(options, AdapterClass)
*/
import Fields from "../Fields";
import IQueryBuilderOptions, { IOperation } from "../IQueryBuilderOptions";
import OperationType from "../OperationType";
import Utils from "../Utils";
import IMutationAdapter from "./IMutationAdapter";
export default class DefaultMutationAdapter implements IMutationAdapter {
private variables: any | undefined;
private fields: Fields | undefined;
private operation!: string | IOperation;
private config: { [key: string]: unknown };
constructor(
options: IQueryBuilderOptions | IQueryBuilderOptions[],
configuration?: { [key: string]: unknown }
) {
if (Array.isArray(options)) {
this.variables = Utils.resolveVariables(options);
} else {
this.variables = options.variables;
this.fields = options.fields;
this.operation = options.operation;
}
// Default configs
this.config = {
operationName: "",
};
if (configuration) {
Object.entries(configuration).forEach(([key, value]) => {
this.config[key] = value;
});
}
}
public mutationBuilder() {
return this.operationWrapperTemplate(
OperationType.Mutation,
this.variables,
this.operationTemplate(this.operation)
);
}
public mutationsBuilder(mutations: IQueryBuilderOptions[]) {
const content = mutations.map((opts) => {
this.operation = opts.operation;
this.variables = opts.variables;
this.fields = opts.fields;
return this.operationTemplate(opts.operation);
});
return this.operationWrapperTemplate(
OperationType.Mutation,
Utils.resolveVariables(mutations),
content.join("\n ")
);
}
private queryDataArgumentAndTypeMap(variablesUsed: any): string {
if (this.fields && typeof this.fields === "object") {
variablesUsed = {
...Utils.getNestedVariables(this.fields),
...variablesUsed,
};
}
return variablesUsed && Object.keys(variablesUsed).length > 0
? `(${Object.keys(variablesUsed).reduce(
(dataString, key, i) =>
`${dataString}${i !== 0 ? ", " : ""}$${key}: ${Utils.queryDataType(
variablesUsed[key]
)}`,
""
)})`
: "";
}
// start of mutation building
private operationWrapperTemplate(
type: OperationType,
variables: any,
content: string
) {
let query = `${type} ${this.queryDataArgumentAndTypeMap(variables)} {
${content}
}`;
if (this.config.operationName) {
query = query.replace(
"mutation",
`mutation ${this.config.operationName}`
);
}
return {
query,
variables: Utils.queryVariablesMap(variables, this.fields),
};
}
private operationTemplate(operation: string | IOperation) {
const operationName =
typeof operation === "string"
? operation
: `${operation.alias}: ${operation.name}`;
return `${operationName} ${Utils.queryDataNameAndArgumentMap(
this.variables
)} ${
this.fields && this.fields.length > 0
? `{
${Utils.queryFieldsMap(this.fields)}
}`
: ""
}`;
}
}