@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.
@@ -1,308 +1,14 @@
1
- import { getCachedDocumentNodeFromSchema, oldVisit } from '@graphql-codegen/plugin-helpers';
2
- import { Kind, visit, print, isScalarType, isInputObjectType, isEnumType, concatAST } from 'graphql';
3
- import { indent, indentMultiline, ClientSideBaseVisitor, buildScalarsFromConfig, DocumentMode, getBaseTypeNode } from '@graphql-codegen/visitor-plugin-common';
1
+ import { ClientSideBaseVisitor, DocumentMode, indentMultiline, getBaseTypeNode, indent, buildScalarsFromConfig, } from '@graphql-codegen/visitor-plugin-common';
4
2
  import autoBind from 'auto-bind';
5
- import { extname } from 'path';
6
- import gql from 'graphql-tag';
7
-
8
- const C_SHARP_SCALARS = {
9
- ID: 'string',
10
- String: 'string',
11
- Boolean: 'bool',
12
- Int: 'int',
13
- Float: 'double',
14
- Date: 'DateTime',
15
- };
16
- const csharpValueTypes = [
17
- 'bool',
18
- 'byte',
19
- 'sbyte',
20
- 'char',
21
- 'decimal',
22
- 'double',
23
- 'float',
24
- 'int',
25
- 'uint',
26
- 'long',
27
- 'ulong',
28
- 'short',
29
- 'ushort',
30
- 'DateTime',
31
- ];
32
-
33
- // This file is bundled and inlined.
34
- function transformComment(comment, indentLevel = 0) {
35
- if (!comment) {
36
- return '';
37
- }
38
- if (isStringValueNode(comment)) {
39
- comment = comment.value;
40
- }
41
- comment = comment.trimStart().split('*/').join('*\\/');
42
- let lines = comment.split('\n');
43
- lines = ['/// <summary>', ...lines.map(line => `/// ${line}`), '/// </summary>'];
44
- return lines
45
- .map(line => indent(line, indentLevel))
46
- .concat('')
47
- .join('\n');
48
- }
49
- function isStringValueNode(node) {
50
- return node && typeof node === 'object' && node.kind === Kind.STRING;
51
- }
52
- function isValueType(type) {
53
- // Limitation: only checks the list of known built in value types
54
- // Eg .NET types and struct types won't be detected correctly
55
- return csharpValueTypes.includes(type);
56
- }
57
- function getListTypeField(typeNode) {
58
- if (typeNode.kind === Kind.LIST_TYPE) {
59
- return {
60
- required: false,
61
- type: getListTypeField(typeNode.type),
62
- };
63
- }
64
- if (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type.kind === Kind.LIST_TYPE) {
65
- return Object.assign(getListTypeField(typeNode.type), {
66
- required: true,
67
- });
68
- }
69
- if (typeNode.kind === Kind.NON_NULL_TYPE) {
70
- return getListTypeField(typeNode.type);
71
- }
72
- return undefined;
73
- }
74
- function getListTypeDepth(listType) {
75
- if (listType) {
76
- return getListTypeDepth(listType.type) + 1;
77
- }
78
- return 0;
79
- }
80
- function getListInnerTypeNode(typeNode) {
81
- if (typeNode.kind === Kind.LIST_TYPE) {
82
- return getListInnerTypeNode(typeNode.type);
83
- }
84
- if (typeNode.kind === Kind.NON_NULL_TYPE && typeNode.type.kind === Kind.LIST_TYPE) {
85
- return getListInnerTypeNode(typeNode.type);
86
- }
87
- return typeNode;
88
- }
89
- function wrapFieldType(fieldType, listTypeField, listType = 'IEnumerable') {
90
- if (listTypeField) {
91
- const innerType = wrapFieldType(fieldType, listTypeField.type, listType);
92
- return `${listType}<${innerType}>`;
93
- }
94
- return fieldType.innerTypeName;
95
- }
96
-
97
- // This file is bundled and inlined.
98
- class CSharpDeclarationBlock {
99
- constructor() {
100
- this._name = null;
101
- this._extendStr = [];
102
- this._implementsStr = [];
103
- this._kind = null;
104
- this._access = 'public';
105
- this._final = false;
106
- this._static = false;
107
- this._block = null;
108
- this._comment = null;
109
- this._nestedClasses = [];
110
- }
111
- nestedClass(nstCls) {
112
- this._nestedClasses.push(nstCls);
113
- return this;
114
- }
115
- access(access) {
116
- this._access = access;
117
- return this;
118
- }
119
- asKind(kind) {
120
- this._kind = kind;
121
- return this;
122
- }
123
- final() {
124
- this._final = true;
125
- return this;
126
- }
127
- static() {
128
- this._static = true;
129
- return this;
130
- }
131
- withComment(comment) {
132
- if (comment) {
133
- this._comment = transformComment(comment, 1);
134
- }
135
- return this;
136
- }
137
- withBlock(block) {
138
- this._block = block;
139
- return this;
140
- }
141
- extends(extendStr) {
142
- this._extendStr = extendStr;
143
- return this;
144
- }
145
- implements(implementsStr) {
146
- this._implementsStr = implementsStr;
147
- return this;
148
- }
149
- withName(name) {
150
- this._name = typeof name === 'object' ? name.value : name;
151
- return this;
152
- }
153
- get string() {
154
- let result = '';
155
- if (this._kind) {
156
- let name = '';
157
- if (this._name) {
158
- name = this._name;
159
- }
160
- if (this._kind === 'namespace') {
161
- result += `${this._kind} ${name} `;
162
- }
163
- else {
164
- let extendStr = '';
165
- let implementsStr = '';
166
- const final = this._final ? ' final' : '';
167
- const isStatic = this._static ? ' static' : '';
168
- if (this._extendStr.length > 0) {
169
- extendStr = ` : ${this._extendStr.join(', ')}`;
170
- }
171
- if (this._implementsStr.length > 0) {
172
- implementsStr = ` : ${this._implementsStr.join(', ')}`;
173
- }
174
- result += `${this._access}${isStatic}${final} ${this._kind} ${name}${extendStr}${implementsStr} `;
175
- }
176
- }
177
- const nestedClasses = this._nestedClasses.length
178
- ? this._nestedClasses.map(c => indentMultiline(c.string)).join('\n\n')
179
- : null;
180
- const before = '{';
181
- const after = '}';
182
- const block = [before, nestedClasses, this._block, after].filter(f => f).join('\n');
183
- result += block;
184
- return (this._comment ? this._comment : '') + result + '\n';
185
- }
186
- }
187
-
188
- class CSharpFieldType {
189
- constructor(fieldType) {
190
- Object.assign(this, fieldType);
191
- }
192
- get innerTypeName() {
193
- const nullable = this.baseType.valueType && !this.baseType.required ? '?' : '';
194
- return `${this.baseType.type}${nullable}`;
195
- }
196
- get isOuterTypeRequired() {
197
- return this.listType ? this.listType.required : this.baseType.required;
198
- }
199
- }
200
-
201
- /**
202
- * C# keywords
203
- * https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
204
- */
205
- const csharpKeywords = new Set([
206
- 'abstract',
207
- 'as',
208
- 'base',
209
- 'bool',
210
- 'break',
211
- 'byte',
212
- 'case',
213
- 'catch',
214
- 'char',
215
- 'checked',
216
- 'class',
217
- 'const',
218
- 'continue',
219
- 'decimal',
220
- 'default',
221
- 'delegate',
222
- 'do',
223
- 'double',
224
- 'else',
225
- 'enum',
226
- 'event',
227
- 'explicit',
228
- 'extern',
229
- 'false',
230
- 'finally',
231
- 'fixed',
232
- 'float',
233
- 'for',
234
- 'foreach',
235
- 'goto',
236
- 'if',
237
- 'implicit',
238
- 'in',
239
- 'int',
240
- 'interface',
241
- 'internal',
242
- 'is',
243
- 'lock',
244
- 'long',
245
- 'namespace',
246
- 'new',
247
- 'null',
248
- 'object',
249
- 'operator',
250
- 'out',
251
- 'override',
252
- 'params',
253
- 'private',
254
- 'protected',
255
- 'public',
256
- 'readonly',
257
- 'record',
258
- 'ref',
259
- 'return',
260
- 'sbyte',
261
- 'sealed',
262
- 'short',
263
- 'sizeof',
264
- 'stackalloc',
265
- 'static',
266
- 'string',
267
- 'struct',
268
- 'switch',
269
- 'this',
270
- 'throw',
271
- 'true',
272
- 'try',
273
- 'typeof',
274
- 'uint',
275
- 'ulong',
276
- 'unchecked',
277
- 'unsafe',
278
- 'ushort',
279
- 'using',
280
- 'virtual',
281
- 'void',
282
- 'volatile',
283
- 'while',
284
- ]);
285
- /**
286
- * Checks name against list of keywords. If it is, will prefix value with @
287
- *
288
- * Note:
289
- * This class should first invoke the convertName from base-visitor to convert the string or node
290
- * value according the naming configuration, eg upper or lower case. Then resulting string checked
291
- * against the list or keywords.
292
- * However the generated C# code is not yet able to handle fields that are in a different case so
293
- * the invocation of convertName is omitted purposely.
294
- */
295
- function convertSafeName(node) {
296
- const name = typeof node === 'string' ? node : node.value;
297
- return csharpKeywords.has(name) ? `@${name}` : name;
298
- }
299
-
3
+ import { print, visit, Kind, isScalarType, isEnumType, isInputObjectType, } from 'graphql';
4
+ import { getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
5
+ import { getListInnerTypeNode, C_SHARP_SCALARS, getListTypeField, getListTypeDepth, CSharpFieldType, convertSafeName, isValueType, wrapFieldType, CSharpDeclarationBlock, } from '@graphql-codegen/c-sharp-common';
300
6
  const defaultSuffix = 'GQL';
301
7
  const R_NAME = /name:\s*"([^"]+)"/;
302
8
  function R_DEF(directive) {
303
9
  return new RegExp(`\\s+\\@${directive}\\([^)]+\\)`, 'gm');
304
10
  }
305
- class CSharpOperationsVisitor extends ClientSideBaseVisitor {
11
+ export class CSharpOperationsVisitor extends ClientSideBaseVisitor {
306
12
  constructor(schema, fragments, rawConfig, documents) {
307
13
  super(schema, fragments, rawConfig, {
308
14
  namespaceName: rawConfig.namespaceName || 'GraphQLCodeGen',
@@ -711,37 +417,3 @@ ${this._getOperationMethod(node)}
711
417
  return indentMultiline(enumDefinition, 2);
712
418
  }
713
419
  }
714
-
715
- const plugin = (schema, documents, config) => {
716
- const schemaAST = getCachedDocumentNodeFromSchema(schema);
717
- const allAst = concatAST(documents.map(v => v.document).concat(schemaAST));
718
- const allFragments = [
719
- ...allAst.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION).map(fragmentDef => ({
720
- node: fragmentDef,
721
- name: fragmentDef.name.value,
722
- onType: fragmentDef.typeCondition.name.value,
723
- isExternal: false,
724
- })),
725
- ...(config.externalFragments || []),
726
- ];
727
- const visitor = new CSharpOperationsVisitor(schema, allFragments, config, documents);
728
- const visitorResult = oldVisit(allAst, { leave: visitor });
729
- const imports = visitor.getCSharpImports();
730
- const openNameSpace = `namespace ${visitor.config.namespaceName} {`;
731
- return {
732
- prepend: [],
733
- content: [imports, openNameSpace, ...visitorResult.definitions.filter(t => typeof t === 'string'), '}']
734
- .filter(a => a)
735
- .join('\n'),
736
- };
737
- };
738
- const addToSchema = gql `
739
- directive @namedClient(name: String!) on OBJECT | FIELD
740
- `;
741
- const validate = async (schema, documents, config, outputFile) => {
742
- if (extname(outputFile) !== '.cs') {
743
- throw new Error(`Plugin "c-sharp-operations" requires extension to be ".cs"!`);
744
- }
745
- };
746
-
747
- export { CSharpOperationsVisitor, addToSchema, plugin, validate };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/c-sharp-operations",
3
- "version": "2.2.13",
3
+ "version": "2.2.15-alpha-2cc8b095b.0",
4
4
  "description": "GraphQL Code Generator plugin for generating CSharp code based on GraphQL operations",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -9,28 +9,36 @@
9
9
  },
10
10
  "dependencies": {
11
11
  "@graphql-codegen/plugin-helpers": "^2.4.0",
12
- "@graphql-codegen/visitor-plugin-common": "2.9.1",
12
+ "@graphql-codegen/visitor-plugin-common": "^2.11.0-alpha-2cc8b095b.0",
13
+ "@graphql-codegen/c-sharp-common": "0.0.1-alpha-2cc8b095b.0",
13
14
  "auto-bind": "~4.0.0",
14
15
  "change-case-all": "1.0.14",
15
16
  "tslib": "~2.4.0"
16
17
  },
17
18
  "repository": "git@github.com:dotansimha/graphql-code-generator.git",
18
19
  "license": "MIT",
19
- "main": "index.js",
20
- "module": "index.mjs",
21
- "typings": "index.d.ts",
20
+ "main": "cjs/index.js",
21
+ "module": "esm/index.js",
22
+ "typings": "typings/index.d.ts",
22
23
  "typescript": {
23
- "definition": "index.d.ts"
24
+ "definition": "typings/index.d.ts"
24
25
  },
26
+ "type": "module",
25
27
  "exports": {
26
- "./package.json": "./package.json",
27
28
  ".": {
28
- "require": "./index.js",
29
- "import": "./index.mjs"
29
+ "require": {
30
+ "types": "./typings/index.d.ts",
31
+ "default": "./cjs/index.js"
32
+ },
33
+ "import": {
34
+ "types": "./typings/index.d.ts",
35
+ "default": "./esm/index.js"
36
+ },
37
+ "default": {
38
+ "types": "./typings/index.d.ts",
39
+ "default": "./esm/index.js"
40
+ }
30
41
  },
31
- "./*": {
32
- "require": "./*.js",
33
- "import": "./*.mjs"
34
- }
42
+ "./package.json": "./package.json"
35
43
  }
36
- }
44
+ }
File without changes
@@ -1,6 +1,6 @@
1
1
  import { PluginValidateFn, PluginFunction } from '@graphql-codegen/plugin-helpers';
2
- import { CSharpOperationsVisitor } from './visitor';
3
- import { CSharpOperationsRawPluginConfig } from './config';
2
+ import { CSharpOperationsVisitor } from './visitor.js';
3
+ import { CSharpOperationsRawPluginConfig } from './config.js';
4
4
  export declare const plugin: PluginFunction<CSharpOperationsRawPluginConfig>;
5
5
  export declare const addToSchema: import("graphql").DocumentNode;
6
6
  export declare const validate: PluginValidateFn<any>;
@@ -1,8 +1,8 @@
1
1
  import { ClientSideBaseVisitor, ClientSideBasePluginConfig, LoadedFragment } from '@graphql-codegen/visitor-plugin-common';
2
2
  import { OperationDefinitionNode, GraphQLSchema, TypeNode, InputObjectTypeDefinitionNode, EnumTypeDefinitionNode } from 'graphql';
3
- import { CSharpOperationsRawPluginConfig } from './config';
3
+ import { CSharpOperationsRawPluginConfig } from './config.js';
4
4
  import { Types } from '@graphql-codegen/plugin-helpers';
5
- import { CSharpFieldType } from '../../common/common';
5
+ import { CSharpFieldType } from '@graphql-codegen/c-sharp-common';
6
6
  export interface CSharpOperationsPluginConfig extends ClientSideBasePluginConfig {
7
7
  namespaceName: string;
8
8
  namedClient: string;