prisma-nestjs-graphql 23.0.0 → 23.0.1

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/README.md CHANGED
@@ -152,6 +152,13 @@ Disable usage of graphql `ID` type and use `Int/Float` for fields marked as `@id
152
152
  Type: `boolean`
153
153
  Default: `false`
154
154
 
155
+ #### `typeListNullable`
156
+
157
+ Adds `nullable: true` for relation list properties out output models,
158
+ it makes graphql field looks like `[Type!]`, default `[Type!]!`
159
+ Type: `boolean`
160
+ Default: `false`
161
+
155
162
  #### `requireSingleFieldsInWhereUniqueInput`
156
163
 
157
164
  When a model `*WhereUniqueInput` class has only a single field, mark that field as **required** (TypeScript) and **not nullable** (GraphQL).
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "prisma-nestjs-graphql",
3
3
  "type": "module",
4
- "version": "23.0.0",
4
+ "version": "23.0.1",
5
5
  "license": "MIT",
6
6
  "description": "Generate object types, inputs, args, etc. from prisma schema file for usage with @nestjs/graphql module",
7
7
  "bin": "bin.mjs",
@@ -47,6 +47,7 @@ declare function createConfig(data: Record<string, unknown>): {
47
47
  reExport: ReExportType;
48
48
  requireSingleFieldsInWhereUniqueInput: boolean;
49
49
  tsConfigFilePath: string | undefined;
50
+ typeListNullable: boolean;
50
51
  unsafeCompatibleWhereUniqueInput: boolean;
51
52
  useInputType: ConfigInputItem[];
52
53
  };
@@ -1456,16 +1456,13 @@ function modelOutputType(outputType, args) {
1456
1456
  name: 'HideField'
1457
1457
  });
1458
1458
  } else {
1459
- // Generate `@Field()` decorator
1460
- property.decorators.push({
1461
- arguments: [isList ? `() => [${graphqlType}]` : `() => ${graphqlType}`, JSON5.stringify({
1462
- ...settings?.fieldArguments(),
1463
- defaultValue: ['number', 'string', 'boolean'].includes(typeof modelField?.default) ? modelField?.default : undefined,
1464
- description: modelField?.documentation,
1465
- nullable: Boolean(field.isNullable)
1466
- })],
1467
- name: 'Field'
1468
- });
1459
+ property.decorators.push(generateFieldDecorator({
1460
+ config,
1461
+ field,
1462
+ graphqlType,
1463
+ modelField,
1464
+ settings
1465
+ }));
1469
1466
  for (const setting of settings || []) {
1470
1467
  if (shouldBeDecorated(setting) && (setting.match?.(field.name) ?? true)) {
1471
1468
  property.decorators.push({
@@ -1476,15 +1473,15 @@ function modelOutputType(outputType, args) {
1476
1473
  importDeclarations.createFrom(setting);
1477
1474
  }
1478
1475
  }
1479
- for (const decorate of config.decorate) {
1480
- if (decorate.isMatchField(field.name) && decorate.isMatchType(outputTypeName)) {
1476
+ for (const d of config.decorate) {
1477
+ if (d.isMatchField(field.name) && d.isMatchType(outputTypeName)) {
1481
1478
  property.decorators.push({
1482
- arguments: decorate.arguments?.map(x => pupa(x, {
1479
+ arguments: d.arguments?.map(x => pupa(x, {
1483
1480
  propertyType
1484
1481
  })),
1485
- name: decorate.name
1482
+ name: d.name
1486
1483
  });
1487
- importDeclarations.createFrom(decorate);
1484
+ importDeclarations.createFrom(d);
1488
1485
  }
1489
1486
  }
1490
1487
  }
@@ -1519,6 +1516,47 @@ function modelOutputType(outputType, args) {
1519
1516
  });
1520
1517
  }
1521
1518
  }
1519
+
1520
+ /**
1521
+ * Get structure for `@Field()` decorator
1522
+ */
1523
+ function generateFieldDecorator(args) {
1524
+ const {
1525
+ config,
1526
+ field,
1527
+ graphqlType,
1528
+ modelField,
1529
+ settings
1530
+ } = args;
1531
+ const {
1532
+ isList,
1533
+ namespace
1534
+ } = field.outputType;
1535
+ const {
1536
+ typeListNullable
1537
+ } = config;
1538
+ const {
1539
+ isNullable
1540
+ } = field;
1541
+ const firstArgumentType = isList ? `() => [${graphqlType}]` : `() => ${graphqlType}`;
1542
+ const getNullable = () => {
1543
+ if (!typeListNullable && namespace === 'model' && isList && isNullable) {
1544
+ return false;
1545
+ }
1546
+ return Boolean(isNullable);
1547
+ };
1548
+ const defaultValue = ['number', 'string', 'boolean'].includes(typeof modelField?.default) ? modelField?.default : undefined;
1549
+ const secondArgumentOptions = JSON5.stringify({
1550
+ ...settings?.fieldArguments(),
1551
+ defaultValue,
1552
+ description: modelField?.documentation,
1553
+ nullable: getNullable()
1554
+ });
1555
+ return {
1556
+ arguments: [firstArgumentType, secondArgumentOptions],
1557
+ name: 'Field'
1558
+ };
1559
+ }
1522
1560
  function shouldBeDecorated(setting) {
1523
1561
  return setting.kind === 'Decorator' && (setting.output || setting.model) && !(setting.output && setting.model);
1524
1562
  }
@@ -2032,6 +2070,7 @@ function createConfig(data) {
2032
2070
  reExport: ReExport[String(config.reExport)] || ReExport.None,
2033
2071
  requireSingleFieldsInWhereUniqueInput: toBoolean(config.requireSingleFieldsInWhereUniqueInput),
2034
2072
  tsConfigFilePath: createTsConfigFilePathValue(config.tsConfigFilePath),
2073
+ typeListNullable: toBoolean(config.typeListNullable ?? 'false'),
2035
2074
  unsafeCompatibleWhereUniqueInput: toBoolean(config.unsafeCompatibleWhereUniqueInput),
2036
2075
  useInputType: createUseInputType(config.useInputType)
2037
2076
  };