@@ -2,6 +2,8 @@ import '@graphql-codegen/testing';
22import { buildSchema } from 'graphql' ;
33import { plugin } from '../src/index' ;
44import { CSharpResolversPluginRawConfig } from '../src/config' ;
5+ import { getJsonAttributeSourceConfiguration } from '../src/json-attributes' ;
6+ import each from 'jest-each' ;
57
68describe ( 'C#' , ( ) => {
79 describe ( 'Using directives' , ( ) => {
@@ -15,9 +17,36 @@ describe('C#', () => {
1517
1618 expect ( result ) . toContain ( 'using System;' ) ;
1719 expect ( result ) . toContain ( 'using System.Collections.Generic;' ) ;
20+ } ) ;
21+
22+ it ( 'Should include default JSON using directives' , async ( ) => {
23+ const schema = buildSchema ( /* GraphQL */ `
24+ enum ns {
25+ dummy
26+ }
27+ ` ) ;
28+ const result = await plugin ( schema , [ ] , { } , { outputFile : '' } ) ;
29+
1830 expect ( result ) . toContain ( 'using Newtonsoft.Json;' ) ;
19- expect ( result ) . toContain ( 'using GraphQL;' ) ;
2031 } ) ;
32+
33+ each ( [ 'Newtonsoft.Json' , 'System.Text.Json' ] ) . it (
34+ `Should include configured '%s' using directives` ,
35+ async source => {
36+ const schema = buildSchema ( /* GraphQL */ `
37+ enum ns {
38+ dummy
39+ }
40+ ` ) ;
41+ const config : CSharpResolversPluginRawConfig = {
42+ jsonAttributesSource : source ,
43+ } ;
44+ const result = await plugin ( schema , [ ] , config , { outputFile : '' } ) ;
45+ const jsonConfig = getJsonAttributeSourceConfiguration ( source ) ;
46+
47+ expect ( result ) . toContain ( `using ${ jsonConfig . namespace } ;` ) ;
48+ }
49+ ) ;
2150 } ) ;
2251
2352 describe ( 'Namespaces' , ( ) => {
@@ -232,6 +261,7 @@ describe('C#', () => {
232261 /// <summary>
233262 /// User id
234263 /// </summary>
264+ [Required]
235265 [JsonRequired]
236266 public int id { get; set; }
237267 ` ) ;
@@ -293,20 +323,46 @@ describe('C#', () => {
293323 expect ( result ) . toContain ( 'public class @public {' ) ;
294324 } ) ;
295325
296- it ( 'Should generate properties for types' , async ( ) => {
326+ each ( [ 'Newtonsoft.Json' , 'System.Text.Json' ] ) . it (
327+ `Should generate properties for types using '%s' source` ,
328+ async source => {
329+ const schema = buildSchema ( /* GraphQL */ `
330+ type User {
331+ id: Int
332+ email: String
333+ }
334+ ` ) ;
335+ const config : CSharpResolversPluginRawConfig = {
336+ jsonAttributesSource : source ,
337+ } ;
338+ const result = await plugin ( schema , [ ] , config , { outputFile : '' } ) ;
339+ const jsonConfig = getJsonAttributeSourceConfiguration ( source ) ;
340+
341+ expect ( result ) . toBeSimilarStringTo ( `
342+ [${ jsonConfig . propertyAttribute } ("id")]
343+ public int? id { get; set; }
344+ [${ jsonConfig . propertyAttribute } ("email")]
345+ public string email { get; set; }
346+ ` ) ;
347+ }
348+ ) ;
349+
350+ it ( `Should generate properties for types without JSON attributes` , async ( ) => {
297351 const schema = buildSchema ( /* GraphQL */ `
298352 type User {
299353 id: Int
300354 email: String
301355 }
302356 ` ) ;
303- const result = await plugin ( schema , [ ] , { } , { outputFile : '' } ) ;
357+ const config : CSharpResolversPluginRawConfig = {
358+ emitJsonAttributes : false ,
359+ } ;
360+ const result = await plugin ( schema , [ ] , config , { outputFile : '' } ) ;
361+
304362 expect ( result ) . toBeSimilarStringTo ( `
305- [JsonProperty("id")]
306- public int? id { get; set; }
307- [JsonProperty("email")]
308- public string email { get; set; }
309- ` ) ;
363+ public int? id { get; set; }
364+ public string email { get; set; }
365+ ` ) ;
310366 } ) ;
311367
312368 it ( 'Should generate summary header for class and properties' , async ( ) => {
@@ -406,7 +462,50 @@ describe('C#', () => {
406462
407463 describe ( 'GraphQL Value Types' , ( ) => {
408464 describe ( 'Scalar' , ( ) => {
409- it ( 'Should generate properties for mandatory scalar types' , async ( ) => {
465+ each ( [ 'Newtonsoft.Json' , 'System.Text.Json' ] ) . it (
466+ `Should generate properties for mandatory scalar types using '%s' source` ,
467+ async source => {
468+ const schema = buildSchema ( /* GraphQL */ `
469+ input BasicTypeInput {
470+ intReq: Int!
471+ fltReq: Float!
472+ idReq: ID!
473+ strReq: String!
474+ boolReq: Boolean!
475+ }
476+ ` ) ;
477+
478+ const config : CSharpResolversPluginRawConfig = {
479+ jsonAttributesSource : source ,
480+ } ;
481+ const result = await plugin ( schema , [ ] , config , { outputFile : '' } ) ;
482+ const jsonConfig = getJsonAttributeSourceConfiguration ( source ) ;
483+ const attributes =
484+ `
485+ [Required]
486+ ` +
487+ ( jsonConfig . requiredAttribute == null
488+ ? ``
489+ : `
490+ [${ jsonConfig . requiredAttribute } ]
491+ ` ) ;
492+
493+ expect ( result ) . toBeSimilarStringTo ( `
494+ ${ attributes }
495+ public int intReq { get; set; }
496+ ${ attributes }
497+ public double fltReq { get; set; }
498+ ${ attributes }
499+ public string idReq { get; set; }
500+ ${ attributes }
501+ public string strReq { get; set; }
502+ ${ attributes }
503+ public bool boolReq { get; set; }
504+ ` ) ;
505+ }
506+ ) ;
507+
508+ it ( `Should generate properties for mandatory scalar types with JSON attributes disabled` , async ( ) => {
410509 const schema = buildSchema ( /* GraphQL */ `
411510 input BasicTypeInput {
412511 intReq: Int!
@@ -416,18 +515,21 @@ describe('C#', () => {
416515 boolReq: Boolean!
417516 }
418517 ` ) ;
419- const result = await plugin ( schema , [ ] , { } , { outputFile : '' } ) ;
518+ const config : CSharpResolversPluginRawConfig = {
519+ emitJsonAttributes : false ,
520+ } ;
521+ const result = await plugin ( schema , [ ] , config , { outputFile : '' } ) ;
420522
421523 expect ( result ) . toBeSimilarStringTo ( `
422- [JsonRequired ]
524+ [Required ]
423525 public int intReq { get; set; }
424- [JsonRequired ]
526+ [Required ]
425527 public double fltReq { get; set; }
426- [JsonRequired ]
528+ [Required ]
427529 public string idReq { get; set; }
428- [JsonRequired ]
530+ [Required ]
429531 public string strReq { get; set; }
430- [JsonRequired ]
532+ [Required ]
431533 public bool boolReq { get; set; }
432534 ` ) ;
433535 } ) ;
@@ -514,8 +616,10 @@ describe('C#', () => {
514616 expect ( result ) . toBeSimilarStringTo ( `
515617 public IEnumerable<int> arr1 { get; set; }
516618 public IEnumerable<double?> arr2 { get; set; }
619+ [Required]
517620 [JsonRequired]
518621 public IEnumerable<int?> arr3 { get; set; }
622+ [Required]
519623 [JsonRequired]
520624 public IEnumerable<bool> arr4 { get; set; }
521625 ` ) ;
@@ -539,8 +643,10 @@ describe('C#', () => {
539643
540644 expect ( result ) . toBeSimilarStringTo ( `
541645 public IEnumerable<IEnumerable<int>> arr1 { get; set; }
646+ [Required]
542647 [JsonRequired]
543648 public IEnumerable<IEnumerable<IEnumerable<double?>>> arr2 { get; set; }
649+ [Required]
544650 [JsonRequired]
545651 public IEnumerable<IEnumerable<Complex>> arr3 { get; set; }
546652 ` ) ;
0 commit comments