@graphql-codegen/c-sharp-operations 2.2.13 → 2.2.15-alpha-2cc8b095b.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.
- package/cjs/config.js +2 -0
- package/cjs/index.js +43 -0
- package/cjs/package.json +1 -0
- package/cjs/visitor.js +424 -0
- package/esm/config.js +1 -0
- package/esm/index.js +37 -0
- package/{index.mjs → esm/visitor.js} +5 -333
- package/package.json +22 -14
- package/{config.d.ts → typings/config.d.ts} +0 -0
- package/{index.d.ts → typings/index.d.ts} +2 -2
- package/{visitor.d.ts → typings/visitor.d.ts} +2 -2
- package/index.js +0 -756
package/cjs/config.js
ADDED
package/cjs/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CSharpOperationsVisitor = exports.validate = exports.addToSchema = exports.plugin = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
6
|
+
const graphql_1 = require("graphql");
|
|
7
|
+
const visitor_js_1 = require("./visitor.js");
|
|
8
|
+
Object.defineProperty(exports, "CSharpOperationsVisitor", { enumerable: true, get: function () { return visitor_js_1.CSharpOperationsVisitor; } });
|
|
9
|
+
const path_1 = require("path");
|
|
10
|
+
const graphql_tag_1 = tslib_1.__importDefault(require("graphql-tag"));
|
|
11
|
+
const plugin = (schema, documents, config) => {
|
|
12
|
+
const schemaAST = (0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(schema);
|
|
13
|
+
const allAst = (0, graphql_1.concatAST)(documents.map(v => v.document).concat(schemaAST));
|
|
14
|
+
const allFragments = [
|
|
15
|
+
...allAst.definitions.filter(d => d.kind === graphql_1.Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
16
|
+
node: fragmentDef,
|
|
17
|
+
name: fragmentDef.name.value,
|
|
18
|
+
onType: fragmentDef.typeCondition.name.value,
|
|
19
|
+
isExternal: false,
|
|
20
|
+
})),
|
|
21
|
+
...(config.externalFragments || []),
|
|
22
|
+
];
|
|
23
|
+
const visitor = new visitor_js_1.CSharpOperationsVisitor(schema, allFragments, config, documents);
|
|
24
|
+
const visitorResult = (0, plugin_helpers_1.oldVisit)(allAst, { leave: visitor });
|
|
25
|
+
const imports = visitor.getCSharpImports();
|
|
26
|
+
const openNameSpace = `namespace ${visitor.config.namespaceName} {`;
|
|
27
|
+
return {
|
|
28
|
+
prepend: [],
|
|
29
|
+
content: [imports, openNameSpace, ...visitorResult.definitions.filter(t => typeof t === 'string'), '}']
|
|
30
|
+
.filter(a => a)
|
|
31
|
+
.join('\n'),
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
exports.plugin = plugin;
|
|
35
|
+
exports.addToSchema = (0, graphql_tag_1.default) `
|
|
36
|
+
directive @namedClient(name: String!) on OBJECT | FIELD
|
|
37
|
+
`;
|
|
38
|
+
const validate = async (schema, documents, config, outputFile) => {
|
|
39
|
+
if ((0, path_1.extname)(outputFile) !== '.cs') {
|
|
40
|
+
throw new Error(`Plugin "c-sharp-operations" requires extension to be ".cs"!`);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
exports.validate = validate;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/visitor.js
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CSharpOperationsVisitor = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
|
|
6
|
+
const auto_bind_1 = tslib_1.__importDefault(require("auto-bind"));
|
|
7
|
+
const graphql_1 = require("graphql");
|
|
8
|
+
const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
|
|
9
|
+
const c_sharp_common_1 = require("@graphql-codegen/c-sharp-common");
|
|
10
|
+
const defaultSuffix = 'GQL';
|
|
11
|
+
const R_NAME = /name:\s*"([^"]+)"/;
|
|
12
|
+
function R_DEF(directive) {
|
|
13
|
+
return new RegExp(`\\s+\\@${directive}\\([^)]+\\)`, 'gm');
|
|
14
|
+
}
|
|
15
|
+
class CSharpOperationsVisitor extends visitor_plugin_common_1.ClientSideBaseVisitor {
|
|
16
|
+
constructor(schema, fragments, rawConfig, documents) {
|
|
17
|
+
super(schema, fragments, rawConfig, {
|
|
18
|
+
namespaceName: rawConfig.namespaceName || 'GraphQLCodeGen',
|
|
19
|
+
namedClient: rawConfig.namedClient,
|
|
20
|
+
querySuffix: rawConfig.querySuffix || defaultSuffix,
|
|
21
|
+
mutationSuffix: rawConfig.mutationSuffix || defaultSuffix,
|
|
22
|
+
subscriptionSuffix: rawConfig.subscriptionSuffix || defaultSuffix,
|
|
23
|
+
scalars: (0, visitor_plugin_common_1.buildScalarsFromConfig)(schema, rawConfig, c_sharp_common_1.C_SHARP_SCALARS),
|
|
24
|
+
typesafeOperation: rawConfig.typesafeOperation || false,
|
|
25
|
+
}, documents);
|
|
26
|
+
this._operationsToInclude = [];
|
|
27
|
+
this.overruleConfigSettings();
|
|
28
|
+
(0, auto_bind_1.default)(this);
|
|
29
|
+
this._schemaAST = (0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(schema);
|
|
30
|
+
}
|
|
31
|
+
// Some settings aren't supported with C#, overruled here
|
|
32
|
+
overruleConfigSettings() {
|
|
33
|
+
if (this.config.documentMode === visitor_plugin_common_1.DocumentMode.graphQLTag) {
|
|
34
|
+
// C# operations does not (yet) support graphQLTag mode
|
|
35
|
+
this.config.documentMode = visitor_plugin_common_1.DocumentMode.documentNode;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
_operationHasDirective(operation, directive) {
|
|
39
|
+
if (typeof operation === 'string') {
|
|
40
|
+
return operation.includes(`${directive}`);
|
|
41
|
+
}
|
|
42
|
+
let found = false;
|
|
43
|
+
(0, graphql_1.visit)(operation, {
|
|
44
|
+
Directive(node) {
|
|
45
|
+
if (node.name.value === directive) {
|
|
46
|
+
found = true;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
return found;
|
|
51
|
+
}
|
|
52
|
+
_extractDirective(operation, directive) {
|
|
53
|
+
const directives = (0, graphql_1.print)(operation).match(R_DEF(directive));
|
|
54
|
+
if (directives.length > 1) {
|
|
55
|
+
throw new Error(`The ${directive} directive used multiple times in '${operation.name}' operation`);
|
|
56
|
+
}
|
|
57
|
+
return directives[0];
|
|
58
|
+
}
|
|
59
|
+
_namedClient(operation) {
|
|
60
|
+
let name;
|
|
61
|
+
if (this._operationHasDirective(operation, 'namedClient')) {
|
|
62
|
+
name = this._extractNamedClient(operation);
|
|
63
|
+
}
|
|
64
|
+
else if (this.config.namedClient) {
|
|
65
|
+
name = this.config.namedClient;
|
|
66
|
+
}
|
|
67
|
+
return name ? `client = '${name}';` : '';
|
|
68
|
+
}
|
|
69
|
+
_extractNamedClient(operation) {
|
|
70
|
+
const [, name] = this._extractDirective(operation, 'namedClient').match(R_NAME);
|
|
71
|
+
return name;
|
|
72
|
+
}
|
|
73
|
+
_gql(node) {
|
|
74
|
+
const fragments = this._transformFragments(node);
|
|
75
|
+
const doc = this._prepareDocument([(0, graphql_1.print)(node), this._includeFragments(fragments, node.kind)].join('\n'));
|
|
76
|
+
return doc.replace(/"/g, '""');
|
|
77
|
+
}
|
|
78
|
+
_getDocumentNodeVariable(node, documentVariableName) {
|
|
79
|
+
return this.config.documentMode === visitor_plugin_common_1.DocumentMode.external ? `Operations.${node.name.value}` : documentVariableName;
|
|
80
|
+
}
|
|
81
|
+
_gqlInputSignature(variable) {
|
|
82
|
+
const typeNode = variable.type;
|
|
83
|
+
const innerType = (0, visitor_plugin_common_1.getBaseTypeNode)(typeNode);
|
|
84
|
+
const schemaType = this._schema.getType(innerType.name.value);
|
|
85
|
+
const name = variable.variable.name.value;
|
|
86
|
+
const baseType = !(0, graphql_1.isScalarType)(schemaType) ? innerType.name.value : this.scalars[schemaType.name] || 'object';
|
|
87
|
+
const listType = (0, c_sharp_common_1.getListTypeField)(typeNode);
|
|
88
|
+
const required = (0, c_sharp_common_1.getListInnerTypeNode)(typeNode).kind === graphql_1.Kind.NON_NULL_TYPE;
|
|
89
|
+
return {
|
|
90
|
+
required: listType ? listType.required : required,
|
|
91
|
+
signature: !listType
|
|
92
|
+
? `${name}=(${baseType})`
|
|
93
|
+
: `${name}=(${baseType}${'[]'.repeat((0, c_sharp_common_1.getListTypeDepth)(listType))})`,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
getCSharpImports() {
|
|
97
|
+
return (['System', 'Newtonsoft.Json', 'GraphQL', 'GraphQL.Client.Abstractions'].map(i => `using ${i};`).join('\n') + '\n');
|
|
98
|
+
}
|
|
99
|
+
_operationSuffix(operationType) {
|
|
100
|
+
switch (operationType) {
|
|
101
|
+
case 'query':
|
|
102
|
+
return this.config.querySuffix;
|
|
103
|
+
case 'mutation':
|
|
104
|
+
return this.config.mutationSuffix;
|
|
105
|
+
case 'subscription':
|
|
106
|
+
return this.config.subscriptionSuffix;
|
|
107
|
+
default:
|
|
108
|
+
return defaultSuffix;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
resolveFieldType(typeNode, hasDefaultValue = false) {
|
|
112
|
+
const innerType = (0, visitor_plugin_common_1.getBaseTypeNode)(typeNode);
|
|
113
|
+
const schemaType = this._schema.getType(innerType.name.value);
|
|
114
|
+
const listType = (0, c_sharp_common_1.getListTypeField)(typeNode);
|
|
115
|
+
const required = (0, c_sharp_common_1.getListInnerTypeNode)(typeNode).kind === graphql_1.Kind.NON_NULL_TYPE;
|
|
116
|
+
let result = null;
|
|
117
|
+
if ((0, graphql_1.isScalarType)(schemaType)) {
|
|
118
|
+
if (this.scalars[schemaType.name]) {
|
|
119
|
+
const baseType = this.scalars[schemaType.name];
|
|
120
|
+
result = new c_sharp_common_1.CSharpFieldType({
|
|
121
|
+
baseType: {
|
|
122
|
+
type: baseType,
|
|
123
|
+
required,
|
|
124
|
+
valueType: (0, c_sharp_common_1.isValueType)(baseType),
|
|
125
|
+
},
|
|
126
|
+
listType,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
result = new c_sharp_common_1.CSharpFieldType({
|
|
131
|
+
baseType: {
|
|
132
|
+
type: 'object',
|
|
133
|
+
required,
|
|
134
|
+
valueType: false,
|
|
135
|
+
},
|
|
136
|
+
listType,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
else if ((0, graphql_1.isInputObjectType)(schemaType)) {
|
|
141
|
+
result = new c_sharp_common_1.CSharpFieldType({
|
|
142
|
+
baseType: {
|
|
143
|
+
type: `${this.convertName(schemaType.name)}`,
|
|
144
|
+
required,
|
|
145
|
+
valueType: false,
|
|
146
|
+
},
|
|
147
|
+
listType,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else if ((0, graphql_1.isEnumType)(schemaType)) {
|
|
151
|
+
result = new c_sharp_common_1.CSharpFieldType({
|
|
152
|
+
baseType: {
|
|
153
|
+
type: this.convertName(schemaType.name),
|
|
154
|
+
required,
|
|
155
|
+
valueType: true,
|
|
156
|
+
},
|
|
157
|
+
listType,
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
result = new c_sharp_common_1.CSharpFieldType({
|
|
162
|
+
baseType: {
|
|
163
|
+
type: `${schemaType.name}`,
|
|
164
|
+
required,
|
|
165
|
+
valueType: false,
|
|
166
|
+
},
|
|
167
|
+
listType,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
if (hasDefaultValue) {
|
|
171
|
+
// Required field is optional when default value specified, see #4273
|
|
172
|
+
(result.listType || result.baseType).required = false;
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
_getResponseFieldRecursive(node, parentSchema) {
|
|
177
|
+
switch (node.kind) {
|
|
178
|
+
case graphql_1.Kind.OPERATION_DEFINITION: {
|
|
179
|
+
return new c_sharp_common_1.CSharpDeclarationBlock()
|
|
180
|
+
.access('public')
|
|
181
|
+
.asKind('class')
|
|
182
|
+
.withName('Response')
|
|
183
|
+
.withBlock('\n' +
|
|
184
|
+
node.selectionSet.selections
|
|
185
|
+
.map(opr => {
|
|
186
|
+
if (opr.kind !== graphql_1.Kind.FIELD) {
|
|
187
|
+
throw new Error(`Unknown kind; ${opr.kind} in OperationDefinitionNode`);
|
|
188
|
+
}
|
|
189
|
+
return this._getResponseFieldRecursive(opr, parentSchema);
|
|
190
|
+
})
|
|
191
|
+
.join('\n')).string;
|
|
192
|
+
}
|
|
193
|
+
case graphql_1.Kind.FIELD: {
|
|
194
|
+
const fieldSchema = parentSchema.fields.find(f => f.name.value === node.name.value);
|
|
195
|
+
if (!fieldSchema) {
|
|
196
|
+
throw new Error(`Field schema not found; ${node.name.value}`);
|
|
197
|
+
}
|
|
198
|
+
const responseType = this.resolveFieldType(fieldSchema.type);
|
|
199
|
+
if (!node.selectionSet) {
|
|
200
|
+
const responseTypeName = (0, c_sharp_common_1.wrapFieldType)(responseType, responseType.listType, 'System.Collections.Generic.List');
|
|
201
|
+
return (0, visitor_plugin_common_1.indentMultiline)([
|
|
202
|
+
`[JsonProperty("${node.name.value}")]`,
|
|
203
|
+
`public ${responseTypeName} ${(0, c_sharp_common_1.convertSafeName)(node.name.value)} { get; set; }`,
|
|
204
|
+
].join('\n') + '\n');
|
|
205
|
+
}
|
|
206
|
+
const selectionBaseTypeName = `${responseType.baseType.type}Selection`;
|
|
207
|
+
const selectionType = Object.assign(new c_sharp_common_1.CSharpFieldType(responseType), {
|
|
208
|
+
baseType: { type: selectionBaseTypeName },
|
|
209
|
+
});
|
|
210
|
+
const selectionTypeName = (0, c_sharp_common_1.wrapFieldType)(selectionType, selectionType.listType, 'System.Collections.Generic.List');
|
|
211
|
+
const innerClassSchema = this._schemaAST.definitions.find(d => d.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION && d.name.value === responseType.baseType.type);
|
|
212
|
+
const innerClassDefinition = new c_sharp_common_1.CSharpDeclarationBlock()
|
|
213
|
+
.access('public')
|
|
214
|
+
.asKind('class')
|
|
215
|
+
.withName((0, c_sharp_common_1.convertSafeName)(selectionBaseTypeName))
|
|
216
|
+
.withBlock('\n' +
|
|
217
|
+
node.selectionSet.selections
|
|
218
|
+
.map(s => {
|
|
219
|
+
if (s.kind === graphql_1.Kind.INLINE_FRAGMENT) {
|
|
220
|
+
throw new Error(`Unsupported kind; ${node.name} ${s.kind}`);
|
|
221
|
+
}
|
|
222
|
+
return this._getResponseFieldRecursive(s, innerClassSchema);
|
|
223
|
+
})
|
|
224
|
+
.join('\n')).string;
|
|
225
|
+
return (0, visitor_plugin_common_1.indentMultiline)([
|
|
226
|
+
innerClassDefinition,
|
|
227
|
+
`[JsonProperty("${node.name.value}")]`,
|
|
228
|
+
`public ${selectionTypeName} ${(0, c_sharp_common_1.convertSafeName)(node.name.value)} { get; set; }`,
|
|
229
|
+
].join('\n') + '\n');
|
|
230
|
+
}
|
|
231
|
+
case graphql_1.Kind.FRAGMENT_SPREAD: {
|
|
232
|
+
const fragmentSchema = this._fragments.find(f => f.name === node.name.value);
|
|
233
|
+
if (!fragmentSchema) {
|
|
234
|
+
throw new Error(`Fragment schema not found; ${node.name.value}`);
|
|
235
|
+
}
|
|
236
|
+
return fragmentSchema.node.selectionSet.selections
|
|
237
|
+
.map(s => {
|
|
238
|
+
if (s.kind === graphql_1.Kind.INLINE_FRAGMENT) {
|
|
239
|
+
throw new Error(`Unsupported kind; ${node.name} ${s.kind}`);
|
|
240
|
+
}
|
|
241
|
+
return this._getResponseFieldRecursive(s, parentSchema);
|
|
242
|
+
})
|
|
243
|
+
.join('\n');
|
|
244
|
+
}
|
|
245
|
+
default: {
|
|
246
|
+
return '';
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
_getResponseClass(node) {
|
|
251
|
+
const operationSchema = this._schemaAST.definitions.find(s => s.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION && s.name.value.toLowerCase() === node.operation);
|
|
252
|
+
return this._getResponseFieldRecursive(node, operationSchema);
|
|
253
|
+
}
|
|
254
|
+
_getVariablesClass(node) {
|
|
255
|
+
var _a, _b;
|
|
256
|
+
if (!((_a = node.variableDefinitions) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
257
|
+
return '';
|
|
258
|
+
}
|
|
259
|
+
return new c_sharp_common_1.CSharpDeclarationBlock()
|
|
260
|
+
.access('public')
|
|
261
|
+
.asKind('class')
|
|
262
|
+
.withName('Variables')
|
|
263
|
+
.withBlock('\n' +
|
|
264
|
+
((_b = node.variableDefinitions) === null || _b === void 0 ? void 0 : _b.map(v => {
|
|
265
|
+
const inputType = this.resolveFieldType(v.type);
|
|
266
|
+
const inputTypeName = (0, c_sharp_common_1.wrapFieldType)(inputType, inputType.listType, 'System.Collections.Generic.List');
|
|
267
|
+
return (0, visitor_plugin_common_1.indentMultiline)([
|
|
268
|
+
`[JsonProperty("${v.variable.name.value}")]`,
|
|
269
|
+
`public ${inputTypeName} ${(0, c_sharp_common_1.convertSafeName)(v.variable.name.value)} { get; set; }`,
|
|
270
|
+
].join('\n') + '\n');
|
|
271
|
+
}).join('\n'))).string;
|
|
272
|
+
}
|
|
273
|
+
_getOperationMethod(node) {
|
|
274
|
+
var _a, _b, _c, _d;
|
|
275
|
+
const operationSchema = this._schemaAST.definitions.find(s => s.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION && s.name.value.toLowerCase() === node.operation);
|
|
276
|
+
if (!operationSchema) {
|
|
277
|
+
throw new Error(`Operation schema not found; ${node.operation}`);
|
|
278
|
+
}
|
|
279
|
+
const variablesArgument = ((_a = node.variableDefinitions) === null || _a === void 0 ? void 0 : _a.length) ? ', Variables variables' : '';
|
|
280
|
+
switch (node.operation) {
|
|
281
|
+
case 'query':
|
|
282
|
+
case 'mutation':
|
|
283
|
+
return [
|
|
284
|
+
`public static System.Threading.Tasks.Task<GraphQLResponse<Response>> Send${operationSchema.name.value}Async(IGraphQLClient client${variablesArgument}, System.Threading.CancellationToken cancellationToken = default) {`,
|
|
285
|
+
(0, visitor_plugin_common_1.indent)(`return client.Send${operationSchema.name.value}Async<Response>(Request(${((_b = node.variableDefinitions) === null || _b === void 0 ? void 0 : _b.length) ? 'variables' : ''}), cancellationToken);`),
|
|
286
|
+
`}`,
|
|
287
|
+
].join('\n');
|
|
288
|
+
case 'subscription': {
|
|
289
|
+
return [
|
|
290
|
+
`public static System.IObservable<GraphQLResponse<Response>> CreateSubscriptionStream(IGraphQLClient client${variablesArgument}) {`,
|
|
291
|
+
(0, visitor_plugin_common_1.indent)(`return client.CreateSubscriptionStream<Response>(Request(${((_c = node.variableDefinitions) === null || _c === void 0 ? void 0 : _c.length) ? 'variables' : ''}));`),
|
|
292
|
+
`}`,
|
|
293
|
+
'',
|
|
294
|
+
`public static System.IObservable<GraphQLResponse<Response>> CreateSubscriptionStream(IGraphQLClient client${variablesArgument}, System.Action<System.Exception> exceptionHandler) {`,
|
|
295
|
+
(0, visitor_plugin_common_1.indent)(`return client.CreateSubscriptionStream<Response>(Request(${((_d = node.variableDefinitions) === null || _d === void 0 ? void 0 : _d.length) ? 'variables' : ''}), exceptionHandler);`),
|
|
296
|
+
`}`,
|
|
297
|
+
].join('\n');
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
throw new Error(`Unexpected operation type: ${node.operation}`);
|
|
301
|
+
}
|
|
302
|
+
OperationDefinition(node) {
|
|
303
|
+
var _a;
|
|
304
|
+
if (!node.name || !node.name.value) {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
this._collectedOperations.push(node);
|
|
308
|
+
const documentVariableName = this.convertName(node, {
|
|
309
|
+
suffix: this.config.documentVariableSuffix,
|
|
310
|
+
prefix: this.config.documentVariablePrefix,
|
|
311
|
+
useTypesPrefix: false,
|
|
312
|
+
});
|
|
313
|
+
let documentString = '';
|
|
314
|
+
if (this.config.documentMode !== visitor_plugin_common_1.DocumentMode.external) {
|
|
315
|
+
const gqlBlock = (0, visitor_plugin_common_1.indentMultiline)(this._gql(node), 4);
|
|
316
|
+
documentString = `${this.config.noExport ? '' : 'public'} static string ${(0, c_sharp_common_1.convertSafeName)(documentVariableName)} = @"\n${gqlBlock}";`;
|
|
317
|
+
}
|
|
318
|
+
const operationType = node.operation;
|
|
319
|
+
const operationTypeSuffix = this.config.dedupeOperationSuffix && node.name.value.toLowerCase().endsWith(node.operation)
|
|
320
|
+
? ''
|
|
321
|
+
: !operationType
|
|
322
|
+
? ''
|
|
323
|
+
: operationType;
|
|
324
|
+
const operationResultType = this.convertName(node, {
|
|
325
|
+
suffix: operationTypeSuffix + this._parsedConfig.operationResultSuffix,
|
|
326
|
+
});
|
|
327
|
+
const operationVariablesTypes = this.convertName(node, {
|
|
328
|
+
suffix: operationTypeSuffix + 'Variables',
|
|
329
|
+
});
|
|
330
|
+
const serviceName = `${this.convertName(node)}${this._operationSuffix(operationType)}`;
|
|
331
|
+
this._operationsToInclude.push({
|
|
332
|
+
node,
|
|
333
|
+
documentVariableName,
|
|
334
|
+
operationType,
|
|
335
|
+
operationResultType,
|
|
336
|
+
operationVariablesTypes,
|
|
337
|
+
});
|
|
338
|
+
const inputSignatures = (_a = node.variableDefinitions) === null || _a === void 0 ? void 0 : _a.map(v => this._gqlInputSignature(v));
|
|
339
|
+
const hasInputArgs = !!(inputSignatures === null || inputSignatures === void 0 ? void 0 : inputSignatures.length);
|
|
340
|
+
const inputArgsHint = hasInputArgs
|
|
341
|
+
? `
|
|
342
|
+
/// <para>Required variables:<br/> { ${inputSignatures
|
|
343
|
+
.filter(sig => sig.required)
|
|
344
|
+
.map(sig => sig.signature)
|
|
345
|
+
.join(', ')} }</para>
|
|
346
|
+
/// <para>Optional variables:<br/> { ${inputSignatures
|
|
347
|
+
.filter(sig => !sig.required)
|
|
348
|
+
.map(sig => sig.signature)
|
|
349
|
+
.join(', ')} }</para>`
|
|
350
|
+
: '';
|
|
351
|
+
// Should use ObsoleteAttribute but VS treats warnings as errors which would be super annoying so use remarks comment instead
|
|
352
|
+
const obsoleteMessage = '/// <remarks>This method is obsolete. Use Request instead.</remarks>';
|
|
353
|
+
let typesafeOperations = '';
|
|
354
|
+
if (this.config.typesafeOperation) {
|
|
355
|
+
typesafeOperations = `
|
|
356
|
+
${this._getVariablesClass(node)}
|
|
357
|
+
${this._getResponseClass(node)}
|
|
358
|
+
${this._getOperationMethod(node)}
|
|
359
|
+
`;
|
|
360
|
+
typesafeOperations = (0, visitor_plugin_common_1.indentMultiline)(typesafeOperations, 3);
|
|
361
|
+
}
|
|
362
|
+
const content = `
|
|
363
|
+
public class ${serviceName} {
|
|
364
|
+
/// <summary>
|
|
365
|
+
/// ${serviceName}.Request ${inputArgsHint}
|
|
366
|
+
/// </summary>
|
|
367
|
+
public static GraphQLRequest Request(${hasInputArgs ? 'object variables = null' : ''}) {
|
|
368
|
+
return new GraphQLRequest {
|
|
369
|
+
Query = ${this._getDocumentNodeVariable(node, documentVariableName)},
|
|
370
|
+
OperationName = "${node.name.value}"${hasInputArgs
|
|
371
|
+
? `,
|
|
372
|
+
Variables = variables`
|
|
373
|
+
: ''}
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
${obsoleteMessage}
|
|
378
|
+
public static GraphQLRequest get${serviceName}() {
|
|
379
|
+
return Request();
|
|
380
|
+
}
|
|
381
|
+
${this._namedClient(node)}
|
|
382
|
+
${documentString}
|
|
383
|
+
${typesafeOperations}
|
|
384
|
+
}
|
|
385
|
+
`;
|
|
386
|
+
return [content].filter(a => a).join('\n');
|
|
387
|
+
}
|
|
388
|
+
InputObjectTypeDefinition(node) {
|
|
389
|
+
var _a;
|
|
390
|
+
if (!this.config.typesafeOperation) {
|
|
391
|
+
return '';
|
|
392
|
+
}
|
|
393
|
+
const inputClass = new c_sharp_common_1.CSharpDeclarationBlock()
|
|
394
|
+
.access('public')
|
|
395
|
+
.asKind('class')
|
|
396
|
+
.withName((0, c_sharp_common_1.convertSafeName)(this.convertName(node)))
|
|
397
|
+
.withBlock('\n' +
|
|
398
|
+
((_a = node.fields) === null || _a === void 0 ? void 0 : _a.map(f => {
|
|
399
|
+
if (f.kind !== graphql_1.Kind.INPUT_VALUE_DEFINITION) {
|
|
400
|
+
return null;
|
|
401
|
+
}
|
|
402
|
+
const inputType = this.resolveFieldType(f.type);
|
|
403
|
+
const inputTypeName = (0, c_sharp_common_1.wrapFieldType)(inputType, inputType.listType, 'System.Collections.Generic.List');
|
|
404
|
+
return (0, visitor_plugin_common_1.indentMultiline)([
|
|
405
|
+
`[JsonProperty("${f.name.value}")]`,
|
|
406
|
+
`public ${inputTypeName} ${(0, c_sharp_common_1.convertSafeName)(f.name.value)} { get; set; }`,
|
|
407
|
+
].join('\n') + '\n');
|
|
408
|
+
}).filter(f => !!f).join('\n'))).string;
|
|
409
|
+
return (0, visitor_plugin_common_1.indentMultiline)(inputClass, 2);
|
|
410
|
+
}
|
|
411
|
+
EnumTypeDefinition(node) {
|
|
412
|
+
var _a;
|
|
413
|
+
if (!this.config.typesafeOperation) {
|
|
414
|
+
return '';
|
|
415
|
+
}
|
|
416
|
+
const enumDefinition = new c_sharp_common_1.CSharpDeclarationBlock()
|
|
417
|
+
.access('public')
|
|
418
|
+
.asKind('enum')
|
|
419
|
+
.withName((0, c_sharp_common_1.convertSafeName)(this.convertName(node.name)))
|
|
420
|
+
.withBlock((0, visitor_plugin_common_1.indentMultiline)((_a = node.values) === null || _a === void 0 ? void 0 : _a.map(v => v.name.value).join(',\n'))).string;
|
|
421
|
+
return (0, visitor_plugin_common_1.indentMultiline)(enumDefinition, 2);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
exports.CSharpOperationsVisitor = CSharpOperationsVisitor;
|
package/esm/config.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { getCachedDocumentNodeFromSchema, oldVisit, } from '@graphql-codegen/plugin-helpers';
|
|
2
|
+
import { concatAST, Kind } from 'graphql';
|
|
3
|
+
import { CSharpOperationsVisitor } from './visitor.js';
|
|
4
|
+
import { extname } from 'path';
|
|
5
|
+
import gql from 'graphql-tag';
|
|
6
|
+
export const plugin = (schema, documents, config) => {
|
|
7
|
+
const schemaAST = getCachedDocumentNodeFromSchema(schema);
|
|
8
|
+
const allAst = concatAST(documents.map(v => v.document).concat(schemaAST));
|
|
9
|
+
const allFragments = [
|
|
10
|
+
...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
|
|
11
|
+
node: fragmentDef,
|
|
12
|
+
name: fragmentDef.name.value,
|
|
13
|
+
onType: fragmentDef.typeCondition.name.value,
|
|
14
|
+
isExternal: false,
|
|
15
|
+
})),
|
|
16
|
+
...(config.externalFragments || []),
|
|
17
|
+
];
|
|
18
|
+
const visitor = new CSharpOperationsVisitor(schema, allFragments, config, documents);
|
|
19
|
+
const visitorResult = oldVisit(allAst, { leave: visitor });
|
|
20
|
+
const imports = visitor.getCSharpImports();
|
|
21
|
+
const openNameSpace = `namespace ${visitor.config.namespaceName} {`;
|
|
22
|
+
return {
|
|
23
|
+
prepend: [],
|
|
24
|
+
content: [imports, openNameSpace, ...visitorResult.definitions.filter(t => typeof t === 'string'), '}']
|
|
25
|
+
.filter(a => a)
|
|
26
|
+
.join('\n'),
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export const addToSchema = gql `
|
|
30
|
+
directive @namedClient(name: String!) on OBJECT | FIELD
|
|
31
|
+
`;
|
|
32
|
+
export const validate = async (schema, documents, config, outputFile) => {
|
|
33
|
+
if (extname(outputFile) !== '.cs') {
|
|
34
|
+
throw new Error(`Plugin "c-sharp-operations" requires extension to be ".cs"!`);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
export { CSharpOperationsVisitor };
|