graphql-custom-fields.spec.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { printSchema } from 'graphql';
  2. import { describe, expect, it } from 'vitest';
  3. import { CustomFieldConfig, CustomFields } from '../../config/custom-field/custom-field-types';
  4. import {
  5. addActiveAdministratorCustomFields,
  6. addGraphQLCustomFields,
  7. addOrderLineCustomFieldsInput,
  8. addRegisterCustomerCustomFieldsInput,
  9. } from './graphql-custom-fields';
  10. describe('addGraphQLCustomFields()', () => {
  11. it('uses JSON scalar if no custom fields defined', () => {
  12. const input = `
  13. type Product {
  14. id: ID
  15. }
  16. `;
  17. const customFieldConfig: CustomFields = {
  18. Product: [],
  19. };
  20. const result = addGraphQLCustomFields(input, customFieldConfig, false);
  21. expect(printSchema(result)).toMatchSnapshot();
  22. });
  23. // regression test for
  24. // https://github.com/vendure-ecommerce/vendure/issues/3158
  25. it('uses JSON scalar in UpdateActiveAdministratorInput if only internal custom fields defined on Administrator', () => {
  26. // custom field that is internal but not readonly - should not cause
  27. // `addActiveAdministratorCustomFields` to assume that
  28. // `UpdateAdministratorCustomFieldsInput` exists
  29. const customFieldConfig: Required<Pick<CustomFields, 'Administrator'>> = {
  30. Administrator: [{ name: 'testField', type: 'string', internal: true }],
  31. };
  32. // `addActiveAdministratorCustomFields` should add customFields to
  33. // UpdateActiveAdministratorInput as a JSON scalar. need to provide
  34. // those types for that to work
  35. const input = `
  36. scalar JSON
  37. input UpdateActiveAdministratorInput {
  38. placeholder: String
  39. }
  40. `;
  41. const schema = addActiveAdministratorCustomFields(input, customFieldConfig.Administrator);
  42. expect(printSchema(schema)).toMatchSnapshot();
  43. });
  44. it('extends a type', () => {
  45. const input = `
  46. type Product {
  47. id: ID
  48. }
  49. `;
  50. const customFieldConfig: CustomFields = {
  51. Product: [{ name: 'available', type: 'boolean' }],
  52. };
  53. const result = addGraphQLCustomFields(input, customFieldConfig, false);
  54. expect(printSchema(result)).toMatchSnapshot();
  55. });
  56. it('extends a type with a translation', () => {
  57. const input = `
  58. type Product {
  59. id: ID
  60. translations: [ProductTranslation!]!
  61. }
  62. type ProductTranslation {
  63. id: ID
  64. }
  65. `;
  66. const customFieldConfig: CustomFields = {
  67. Product: [
  68. { name: 'available', type: 'boolean' },
  69. { name: 'shortName', type: 'localeString' },
  70. ],
  71. };
  72. const result = addGraphQLCustomFields(input, customFieldConfig, false);
  73. expect(printSchema(result)).toMatchSnapshot();
  74. });
  75. it('extends a type with a Create input', () => {
  76. const input = `
  77. type Product {
  78. id: ID
  79. }
  80. input CreateProductInput {
  81. image: String
  82. }
  83. `;
  84. const customFieldConfig: CustomFields = {
  85. Product: [
  86. { name: 'available', type: 'boolean' },
  87. { name: 'shortName', type: 'localeString' },
  88. ],
  89. };
  90. const result = addGraphQLCustomFields(input, customFieldConfig, false);
  91. expect(printSchema(result)).toMatchSnapshot();
  92. });
  93. it('extends a type with an Update input', () => {
  94. const input = `
  95. type Product {
  96. id: ID
  97. }
  98. input UpdateProductInput {
  99. image: String
  100. }
  101. `;
  102. const customFieldConfig: CustomFields = {
  103. Product: [
  104. { name: 'available', type: 'boolean' },
  105. { name: 'shortName', type: 'localeString' },
  106. ],
  107. };
  108. const result = addGraphQLCustomFields(input, customFieldConfig, false);
  109. expect(printSchema(result)).toMatchSnapshot();
  110. });
  111. it('extends a type with a Create input and a translation', () => {
  112. const input = `
  113. type Product {
  114. id: ID
  115. }
  116. type ProductTranslation {
  117. id: ID
  118. }
  119. input ProductTranslationInput {
  120. id: ID
  121. }
  122. input CreateProductInput {
  123. image: String
  124. }
  125. `;
  126. const customFieldConfig: CustomFields = {
  127. Product: [
  128. { name: 'available', type: 'boolean' },
  129. { name: 'shortName', type: 'localeString' },
  130. ],
  131. };
  132. const result = addGraphQLCustomFields(input, customFieldConfig, false);
  133. expect(printSchema(result)).toMatchSnapshot();
  134. });
  135. it('extends a type with SortParameters', () => {
  136. const input = `
  137. type Product {
  138. id: ID
  139. }
  140. input ProductSortParameter {
  141. id: SortOrder
  142. }
  143. enum SortOrder {
  144. ASC
  145. DESC
  146. }
  147. `;
  148. const customFieldConfig: CustomFields = {
  149. Product: [
  150. { name: 'available', type: 'boolean' },
  151. { name: 'shortName', type: 'localeString' },
  152. ],
  153. };
  154. const result = addGraphQLCustomFields(input, customFieldConfig, false);
  155. expect(printSchema(result)).toMatchSnapshot();
  156. });
  157. it('extends a type with FilterParameters', () => {
  158. const input = `
  159. type Product {
  160. name: String
  161. }
  162. input ProductFilterParameter {
  163. id: StringOperators
  164. }
  165. input StringOperators {
  166. eq: String
  167. }
  168. input NumberOperators {
  169. eq: Float
  170. }
  171. input DateOperators {
  172. eq: String
  173. }
  174. input BooleanOperators {
  175. eq: Boolean
  176. }
  177. `;
  178. const customFieldConfig: CustomFields = {
  179. Product: [
  180. { name: 'available', type: 'boolean' },
  181. { name: 'shortName', type: 'localeString' },
  182. { name: 'rating', type: 'float' },
  183. { name: 'published', type: 'datetime' },
  184. ],
  185. };
  186. const result = addGraphQLCustomFields(input, customFieldConfig, false);
  187. expect(printSchema(result)).toMatchSnapshot();
  188. });
  189. it('publicOnly = true', () => {
  190. const input = `
  191. type Product {
  192. id: ID
  193. }
  194. `;
  195. const customFieldConfig: CustomFields = {
  196. Product: [
  197. { name: 'available', type: 'boolean', public: true },
  198. { name: 'profitMargin', type: 'float', public: false },
  199. ],
  200. };
  201. const result = addGraphQLCustomFields(input, customFieldConfig, true);
  202. expect(printSchema(result)).toMatchSnapshot();
  203. });
  204. it('extends OrderAddress if Address custom fields defined', () => {
  205. const input = `
  206. type Address {
  207. id: ID
  208. streetLine1: String
  209. }
  210. type OrderAddress {
  211. streetLine1: String
  212. }
  213. `;
  214. const customFieldConfig: CustomFields = {
  215. Address: [{ name: 'instructions', type: 'string' }],
  216. };
  217. const result = addGraphQLCustomFields(input, customFieldConfig, true);
  218. expect(printSchema(result)).toMatchSnapshot();
  219. });
  220. });
  221. describe('addOrderLineCustomFieldsInput()', () => {
  222. it('Modifies the schema when the addItemToOrder & adjustOrderLine mutation is present', () => {
  223. const input = `
  224. type Mutation {
  225. addItemToOrder(id: ID!, quantity: Int!): Boolean
  226. adjustOrderLine(id: ID!, quantity: Int): Boolean
  227. }
  228. `;
  229. const customFieldConfig: CustomFieldConfig[] = [
  230. { name: 'giftWrap', type: 'boolean' },
  231. { name: 'message', type: 'string' },
  232. ];
  233. const result = addOrderLineCustomFieldsInput(input, customFieldConfig);
  234. expect(printSchema(result)).toMatchSnapshot();
  235. });
  236. });
  237. describe('addRegisterCustomerCustomFieldsInput()', () => {
  238. it('add public writable custom fields to RegisterCustomerInput', () => {
  239. const input = `
  240. input RegisterCustomerInput {
  241. emailAddress: String!
  242. title: String
  243. firstName: String
  244. lastName: String
  245. phoneNumber: String
  246. password: String
  247. }
  248. type Mutation {
  249. registerCustomerAccount(input: RegisterCustomerInput!): Boolean!
  250. }
  251. `;
  252. const customFieldConfig: CustomFieldConfig[] = [
  253. { name: 'isB2B', type: 'boolean', nullable: false },
  254. { name: 'message', type: 'string' },
  255. { name: 'rating', type: 'int', public: false },
  256. { name: 'dbRef', type: 'int', internal: true },
  257. ];
  258. const result = addRegisterCustomerCustomFieldsInput(input, customFieldConfig);
  259. expect(printSchema(result)).toMatchSnapshot();
  260. });
  261. });