graphile-build-pg 4.13.0 → 4.14.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.
@@ -11,6 +11,8 @@ var _postgresInterval = require("../postgresInterval");
11
11
 
12
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
13
 
14
+ const ENUM_DOMAIN_SUFFIX = "_enum_domain";
15
+
14
16
  function indent(str) {
15
17
  return " " + str.replace(/\n/g, "\n ");
16
18
  }
@@ -31,6 +33,7 @@ var PgTypesPlugin = function PgTypesPlugin(builder, {
31
33
  builder.hook("build", build => {
32
34
  const {
33
35
  pgIntrospectionResultsByKind: introspectionResultsByKind,
36
+ getPgFakeEnumIdentifier,
34
37
  getTypeByName,
35
38
  pgSql: sql,
36
39
  inflection,
@@ -569,8 +572,10 @@ var PgTypesPlugin = function PgTypesPlugin(builder, {
569
572
  if (gqlInputType) {
570
573
  gqlInputTypeByTypeIdAndModifier[type.id][typeModifierKey] = gqlInputType;
571
574
  }
572
- } // Enums
575
+ } // cf https://github.com/graphile/graphile-engine/pull/748#discussion_r650828353
576
+
573
577
 
578
+ let shouldSkipAddType = false; // Enums
574
579
 
575
580
  if (!gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] && type.type === "e") {
576
581
  gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] = newWithHooks(GraphQLEnumType, {
@@ -729,19 +734,42 @@ end`;
729
734
 
730
735
 
731
736
  if (!gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] && type.type === "d" && type.domainBaseTypeId) {
732
- const baseType = getGqlTypeByTypeIdAndModifier(type.domainBaseTypeId, typeModifier);
733
- const baseInputType = getGqlInputTypeByTypeIdAndModifier(type.domainBaseTypeId, typeModifier); // Hack stolen from: https://github.com/graphile/postgraphile/blob/ade728ed8f8e3ecdc5fdad7d770c67aa573578eb/src/graphql/schema/type/aliasGqlType.ts#L16
737
+ // might be used as an enum alias: https://github.com/graphile/postgraphile/issues/1500
738
+ const tagEnumName = type.tags && typeof type.tags.enum === "string" ? type.tags.enum : null;
739
+ const truncatedTypeName = !tagEnumName && type.name.endsWith(ENUM_DOMAIN_SUFFIX) ? type.name.slice(0, type.name.length - ENUM_DOMAIN_SUFFIX.length) : null;
740
+ const underlyingEnumTypeName = tagEnumName || truncatedTypeName;
741
+
742
+ if (underlyingEnumTypeName !== null) {
743
+ const baseTypeId = getPgFakeEnumIdentifier(type.namespaceName, underlyingEnumTypeName);
744
+ const baseType = getGqlTypeByTypeIdAndModifier(baseTypeId, typeModifierKey);
745
+ const baseInputType = getGqlInputTypeByTypeIdAndModifier(baseTypeId, typeModifierKey);
746
+
747
+ if (!baseType || !baseInputType) {
748
+ if (tagEnumName) {
749
+ throw new Error(`The domain ${type.name} uses '@enum ${tagEnumName}' to references its underlying enum type, but no type named ${tagEnumName} could be found.`);
750
+ } else {
751
+ throw new Error(`The domain ${type.name} references the enum type ${underlyingEnumTypeName}, but no such type could be found.`);
752
+ }
753
+ }
734
754
 
735
- gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] = Object.assign(Object.create(baseType), {
736
- name: inflection.domainType(type),
737
- description: type.description
738
- });
755
+ gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] = baseType;
756
+ gqlInputTypeByTypeIdAndModifier[type.id][typeModifierKey] = baseInputType;
757
+ shouldSkipAddType = true;
758
+ } else {
759
+ const baseType = getGqlTypeByTypeIdAndModifier(type.domainBaseTypeId, typeModifier);
760
+ const baseInputType = getGqlInputTypeByTypeIdAndModifier(type.domainBaseTypeId, typeModifier); // Hack stolen from: https://github.com/graphile/postgraphile/blob/ade728ed8f8e3ecdc5fdad7d770c67aa573578eb/src/graphql/schema/type/aliasGqlType.ts#L16
739
761
 
740
- if (baseInputType && baseInputType !== baseType) {
741
- gqlInputTypeByTypeIdAndModifier[type.id][typeModifierKey] = Object.assign(Object.create(baseInputType), {
742
- name: inflection.inputType(gqlTypeByTypeIdAndModifier[type.id][typeModifierKey]),
762
+ gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] = Object.assign(Object.create(baseType), {
763
+ name: inflection.domainType(type),
743
764
  description: type.description
744
765
  });
766
+
767
+ if (baseInputType && baseInputType !== baseType) {
768
+ gqlInputTypeByTypeIdAndModifier[type.id][typeModifierKey] = Object.assign(Object.create(baseInputType), {
769
+ name: inflection.inputType(gqlTypeByTypeIdAndModifier[type.id][typeModifierKey]),
770
+ description: type.description
771
+ });
772
+ }
745
773
  }
746
774
  } // Arrays
747
775
 
@@ -782,9 +810,14 @@ end`;
782
810
  if (isInputType(gqlTypeByTypeIdAndModifier[type.id][typeModifierKey])) {
783
811
  gqlInputTypeByTypeIdAndModifier[type.id][typeModifierKey] = gqlTypeByTypeIdAndModifier[type.id][typeModifierKey];
784
812
  }
813
+ } // if its a domain type substituting for an enum table,
814
+ // adding it creates a duplicate
815
+
816
+
817
+ if (!shouldSkipAddType) {
818
+ addType(getNamedType(gqlTypeByTypeIdAndModifier[type.id][typeModifierKey]));
785
819
  }
786
820
 
787
- addType(getNamedType(gqlTypeByTypeIdAndModifier[type.id][typeModifierKey]));
788
821
  return gqlTypeByTypeIdAndModifier[type.id][typeModifierKey];
789
822
  };
790
823
 
@@ -5,6 +5,8 @@ import makeGraphQLJSONType from "../GraphQLJSON";
5
5
 
6
6
  import { parseInterval } from "../postgresInterval";
7
7
 
8
+ const ENUM_DOMAIN_SUFFIX = "_enum_domain";
9
+
8
10
  function indent(str) {
9
11
  return " " + str.replace(/\n/g, "\n ");
10
12
  }
@@ -30,6 +32,7 @@ export default (function PgTypesPlugin(
30
32
  build => {
31
33
  const {
32
34
  pgIntrospectionResultsByKind: introspectionResultsByKind,
35
+ getPgFakeEnumIdentifier,
33
36
  getTypeByName,
34
37
  pgSql: sql,
35
38
  inflection,
@@ -753,6 +756,10 @@ export default (function PgTypesPlugin(
753
756
  gqlInputType;
754
757
  }
755
758
  }
759
+
760
+ // cf https://github.com/graphile/graphile-engine/pull/748#discussion_r650828353
761
+ let shouldSkipAddType = false;
762
+
756
763
  // Enums
757
764
  if (
758
765
  !gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] &&
@@ -994,30 +1001,72 @@ end`;
994
1001
  type.type === "d" &&
995
1002
  type.domainBaseTypeId
996
1003
  ) {
997
- const baseType = getGqlTypeByTypeIdAndModifier(
998
- type.domainBaseTypeId,
999
- typeModifier
1000
- );
1001
- const baseInputType = getGqlInputTypeByTypeIdAndModifier(
1002
- type.domainBaseTypeId,
1003
- typeModifier
1004
- );
1005
- // Hack stolen from: https://github.com/graphile/postgraphile/blob/ade728ed8f8e3ecdc5fdad7d770c67aa573578eb/src/graphql/schema/type/aliasGqlType.ts#L16
1006
- gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] = Object.assign(
1007
- Object.create(baseType),
1008
- {
1009
- name: inflection.domainType(type),
1010
- description: type.description,
1004
+ // might be used as an enum alias: https://github.com/graphile/postgraphile/issues/1500
1005
+ const tagEnumName =
1006
+ type.tags && typeof type.tags.enum === "string"
1007
+ ? type.tags.enum
1008
+ : null;
1009
+ const truncatedTypeName =
1010
+ !tagEnumName && type.name.endsWith(ENUM_DOMAIN_SUFFIX)
1011
+ ? type.name.slice(0, type.name.length - ENUM_DOMAIN_SUFFIX.length)
1012
+ : null;
1013
+ const underlyingEnumTypeName = tagEnumName || truncatedTypeName;
1014
+ if (underlyingEnumTypeName !== null) {
1015
+ const baseTypeId = getPgFakeEnumIdentifier(
1016
+ type.namespaceName,
1017
+ underlyingEnumTypeName
1018
+ );
1019
+
1020
+ const baseType = getGqlTypeByTypeIdAndModifier(
1021
+ baseTypeId,
1022
+ typeModifierKey
1023
+ );
1024
+
1025
+ const baseInputType = getGqlInputTypeByTypeIdAndModifier(
1026
+ baseTypeId,
1027
+ typeModifierKey
1028
+ );
1029
+
1030
+ if (!baseType || !baseInputType) {
1031
+ if (tagEnumName) {
1032
+ throw new Error(
1033
+ `The domain ${type.name} uses '@enum ${tagEnumName}' to references its underlying enum type, but no type named ${tagEnumName} could be found.`
1034
+ );
1035
+ } else {
1036
+ throw new Error(
1037
+ `The domain ${type.name} references the enum type ${underlyingEnumTypeName}, but no such type could be found.`
1038
+ );
1039
+ }
1011
1040
  }
1012
- );
1013
- if (baseInputType && baseInputType !== baseType) {
1041
+
1042
+ gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] = baseType;
1014
1043
  gqlInputTypeByTypeIdAndModifier[type.id][typeModifierKey] =
1015
- Object.assign(Object.create(baseInputType), {
1016
- name: inflection.inputType(
1017
- gqlTypeByTypeIdAndModifier[type.id][typeModifierKey]
1018
- ),
1044
+ baseInputType;
1045
+ shouldSkipAddType = true;
1046
+ } else {
1047
+ const baseType = getGqlTypeByTypeIdAndModifier(
1048
+ type.domainBaseTypeId,
1049
+ typeModifier
1050
+ );
1051
+ const baseInputType = getGqlInputTypeByTypeIdAndModifier(
1052
+ type.domainBaseTypeId,
1053
+ typeModifier
1054
+ );
1055
+ // Hack stolen from: https://github.com/graphile/postgraphile/blob/ade728ed8f8e3ecdc5fdad7d770c67aa573578eb/src/graphql/schema/type/aliasGqlType.ts#L16
1056
+ gqlTypeByTypeIdAndModifier[type.id][typeModifierKey] =
1057
+ Object.assign(Object.create(baseType), {
1058
+ name: inflection.domainType(type),
1019
1059
  description: type.description,
1020
1060
  });
1061
+ if (baseInputType && baseInputType !== baseType) {
1062
+ gqlInputTypeByTypeIdAndModifier[type.id][typeModifierKey] =
1063
+ Object.assign(Object.create(baseInputType), {
1064
+ name: inflection.inputType(
1065
+ gqlTypeByTypeIdAndModifier[type.id][typeModifierKey]
1066
+ ),
1067
+ description: type.description,
1068
+ });
1069
+ }
1021
1070
  }
1022
1071
  }
1023
1072
 
@@ -1076,9 +1125,13 @@ end`;
1076
1125
  gqlTypeByTypeIdAndModifier[type.id][typeModifierKey];
1077
1126
  }
1078
1127
  }
1079
- addType(
1080
- getNamedType(gqlTypeByTypeIdAndModifier[type.id][typeModifierKey])
1081
- );
1128
+ // if its a domain type substituting for an enum table,
1129
+ // adding it creates a duplicate
1130
+ if (!shouldSkipAddType) {
1131
+ addType(
1132
+ getNamedType(gqlTypeByTypeIdAndModifier[type.id][typeModifierKey])
1133
+ );
1134
+ }
1082
1135
  return gqlTypeByTypeIdAndModifier[type.id][typeModifierKey];
1083
1136
  };
1084
1137