graphql-custom-fields.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { printSchema } from 'graphql';
  2. import { CustomFields } from '@vendure/common/lib/shared-types';
  3. import { addGraphQLCustomFields } from './graphql-custom-fields';
  4. describe('addGraphQLCustomFields()', () => {
  5. it('uses JSON scalar if no custom fields defined', () => {
  6. const input = `
  7. type Product {
  8. id: ID
  9. }
  10. `;
  11. const customFieldConfig: CustomFields = {
  12. Product: [],
  13. };
  14. const result = addGraphQLCustomFields(input, customFieldConfig);
  15. expect(printSchema(result)).toMatchSnapshot();
  16. });
  17. it('extends a type', () => {
  18. const input = `
  19. type Product {
  20. id: ID
  21. }
  22. `;
  23. const customFieldConfig: CustomFields = {
  24. Product: [{ name: 'available', type: 'boolean' }],
  25. };
  26. const result = addGraphQLCustomFields(input, customFieldConfig);
  27. expect(printSchema(result)).toMatchSnapshot();
  28. });
  29. it('extends a type with a translation', () => {
  30. const input = `
  31. type Product {
  32. id: ID
  33. translations: [ProductTranslation!]!
  34. }
  35. type ProductTranslation {
  36. id: ID
  37. }
  38. `;
  39. const customFieldConfig: CustomFields = {
  40. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  41. };
  42. const result = addGraphQLCustomFields(input, customFieldConfig);
  43. expect(printSchema(result)).toMatchSnapshot();
  44. });
  45. it('extends a type with a Create input', () => {
  46. const input = `
  47. type Product {
  48. id: ID
  49. }
  50. input CreateProductInput {
  51. image: String
  52. }
  53. `;
  54. const customFieldConfig: CustomFields = {
  55. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  56. };
  57. const result = addGraphQLCustomFields(input, customFieldConfig);
  58. expect(printSchema(result)).toMatchSnapshot();
  59. });
  60. it('extends a type with an Update input', () => {
  61. const input = `
  62. type Product {
  63. id: ID
  64. }
  65. input UpdateProductInput {
  66. image: String
  67. }
  68. `;
  69. const customFieldConfig: CustomFields = {
  70. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  71. };
  72. const result = addGraphQLCustomFields(input, customFieldConfig);
  73. expect(printSchema(result)).toMatchSnapshot();
  74. });
  75. it('extends a type with a Create input and a translation', () => {
  76. const input = `
  77. type Product {
  78. id: ID
  79. }
  80. type ProductTranslation {
  81. id: ID
  82. }
  83. input ProductTranslationInput {
  84. id: ID
  85. }
  86. input CreateProductInput {
  87. image: String
  88. }
  89. `;
  90. const customFieldConfig: CustomFields = {
  91. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  92. };
  93. const result = addGraphQLCustomFields(input, customFieldConfig);
  94. expect(printSchema(result)).toMatchSnapshot();
  95. });
  96. it('extends a type with SortParameters', () => {
  97. const input = `
  98. type Product {
  99. id: ID
  100. }
  101. input ProductSortParameter {
  102. id: SortOrder
  103. }
  104. enum SortOrder {
  105. ASC
  106. DESC
  107. }
  108. `;
  109. const customFieldConfig: CustomFields = {
  110. Product: [{ name: 'available', type: 'boolean' }, { name: 'shortName', type: 'localeString' }],
  111. };
  112. const result = addGraphQLCustomFields(input, customFieldConfig);
  113. expect(printSchema(result)).toMatchSnapshot();
  114. });
  115. it('extends a type with FilterParameters', () => {
  116. const input = `
  117. type Product {
  118. name: String
  119. }
  120. input ProductFilterParameter {
  121. id: StringOperators
  122. }
  123. input StringOperators {
  124. eq: String
  125. }
  126. input NumberOperators {
  127. eq: Float
  128. }
  129. input DateOperators {
  130. eq: String
  131. }
  132. input BooleanOperators {
  133. eq: Boolean
  134. }
  135. `;
  136. const customFieldConfig: CustomFields = {
  137. Product: [
  138. { name: 'available', type: 'boolean' },
  139. { name: 'shortName', type: 'localeString' },
  140. { name: 'rating', type: 'float' },
  141. { name: 'published', type: 'datetime' },
  142. ],
  143. };
  144. const result = addGraphQLCustomFields(input, customFieldConfig);
  145. expect(printSchema(result)).toMatchSnapshot();
  146. });
  147. });