@orion-js/graphql 4.0.9 → 4.0.11

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/dist/index.js CHANGED
@@ -264,8 +264,8 @@ import { GraphQLObjectType as GraphQLObjectType2 } from "graphql";
264
264
  import { GraphQLList as GraphQLList2, GraphQLObjectType } from "graphql";
265
265
  import {
266
266
  getFieldType as getFieldType2,
267
- getSchemaWithMetadataFromAnyOrionForm,
268
- isSchemaLike
267
+ getSchemaWithMetadataFromAnyOrionForm as getSchemaWithMetadataFromAnyOrionForm2,
268
+ isSchemaLike as isSchemaLike2
269
269
  } from "@orion-js/schema";
270
270
 
271
271
  // ../../node_modules/.pnpm/rambdax@11.3.1/node_modules/rambdax/src/type.js
@@ -567,8 +567,12 @@ function getScalar_default(fieldType) {
567
567
  }
568
568
 
569
569
  // src/buildSchema/getArgs/getField.ts
570
- import { GraphQLList, GraphQLInputObjectType } from "graphql";
571
- import { getFieldType } from "@orion-js/schema";
570
+ import { GraphQLInputObjectType, GraphQLList } from "graphql";
571
+ import {
572
+ getFieldType,
573
+ getSchemaWithMetadataFromAnyOrionForm,
574
+ isSchemaLike
575
+ } from "@orion-js/schema";
572
576
 
573
577
  // src/resolversSchemas/getStaticFields.ts
574
578
  function getStaticFields(schema) {
@@ -587,57 +591,70 @@ function getStaticFields(schema) {
587
591
  }
588
592
 
589
593
  // src/buildSchema/getArgs/getField.ts
590
- Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
591
- var storedModelInput = {};
592
- var getCachedModelInput = (model, fields) => {
593
- if (!storedModelInput[model.name]) {
594
- storedModelInput[model.name] = new GraphQLInputObjectType({
595
- name: `${model.name}Input`,
596
- fields
597
- });
598
- }
599
- return storedModelInput[model.name];
600
- };
601
- var resolveModelFields = (model) => {
602
- const fields = {};
603
- for (const field of getStaticFields(model)) {
604
- fields[field.key] = { type: resolveType(field.type) };
605
- }
606
- return fields;
607
- };
608
- var resolveArrayType = (type2) => new GraphQLList(resolveType(type2[0]));
609
- var resolvePlainObjectOrModelType = (type2) => {
610
- const model = type2.__isModel ? type2 : type2.__model;
611
- if (!model || !model.__isModel) {
612
- throw new Error("A type is not a Model");
613
- }
614
- const fields = resolveModelFields(model);
615
- return getCachedModelInput(model, fields);
616
- };
617
- var resolveType = (type2) => {
594
+ var registeredGraphQLTypes = /* @__PURE__ */ new Map();
595
+ var resolveType = (type2, options) => {
618
596
  var _a;
619
597
  if (!type2) throw new Error("No type specified");
620
598
  if ((_a = type2 == null ? void 0 : type2[Symbol.metadata]) == null ? void 0 : _a._getModel) {
621
599
  const model = type2[Symbol.metadata]._getModel();
622
- return resolveType(model);
600
+ return resolveType(model, options);
601
+ }
602
+ if (Array.isArray(type2)) {
603
+ return new GraphQLList(resolveType(type2[0], options));
623
604
  }
624
- if (Array.isArray(type2)) return resolveArrayType(type2);
625
- if (!type2.__isFieldType && (isType("Object", type2) || type2.__isModel)) {
626
- return resolvePlainObjectOrModelType(type2);
605
+ if (isSchemaLike(type2)) {
606
+ const schema = getSchemaWithMetadataFromAnyOrionForm(type2);
607
+ const modelName = `${schema.__modelName}Input`;
608
+ if (schema.__graphQLType) {
609
+ return schema.__graphQLType;
610
+ }
611
+ if (!schema.__modelName) {
612
+ throw new Error(
613
+ `Schema name is not defined. Register a name with schemaWithName. Schema: ${JSON.stringify(schema)}`
614
+ );
615
+ }
616
+ if (registeredGraphQLTypes.has(modelName)) {
617
+ const { graphQLType: graphQLType2, schema: registeredSchema } = registeredGraphQLTypes.get(modelName);
618
+ if (equals(registeredSchema, schema)) {
619
+ return graphQLType2;
620
+ }
621
+ throw new Error(`Schema named "${modelName}" already registered`);
622
+ }
623
+ const graphQLType = createGraphQLInputType(modelName, schema, options);
624
+ registeredGraphQLTypes.set(modelName, { schema, graphQLType });
625
+ return graphQLType;
627
626
  }
628
627
  const schemaType = getFieldType(type2);
629
628
  return getScalar_default(schemaType);
630
629
  };
631
630
  var getField_default = resolveType;
631
+ var createGraphQLInputType = (modelName, schema, options) => new GraphQLInputObjectType({
632
+ name: modelName,
633
+ fields: () => buildFields(schema, options)
634
+ });
635
+ var buildFields = (schema, options) => {
636
+ const fields = {};
637
+ const addStaticFields = () => {
638
+ for (const field of getStaticFields(schema)) {
639
+ try {
640
+ fields[field.key] = { type: resolveType(field.type, options) };
641
+ } catch (error) {
642
+ throw new Error(`Error getting type for ${field.key}: ${error.message}`);
643
+ }
644
+ }
645
+ };
646
+ addStaticFields();
647
+ return fields;
648
+ };
632
649
 
633
650
  // src/buildSchema/getArgs/index.ts
634
- async function getArgs_default(params) {
651
+ async function getArgs_default(params, options) {
635
652
  if (!params) return;
636
653
  if (Object.keys(params).length === 0) return;
637
654
  const fields = {};
638
655
  for (const key of Object.keys(params).filter((key2) => !key2.startsWith("__"))) {
639
656
  try {
640
- const type2 = getField_default(params[key].type);
657
+ const type2 = getField_default(params[key].type, options);
641
658
  fields[key] = { type: type2 };
642
659
  } catch (error) {
643
660
  throw new Error(`Error creating GraphQL resolver params argument ${key}: ${error.message}`);
@@ -681,7 +698,7 @@ function errorHandler(error, data) {
681
698
  // src/buildSchema/getType/getTypeAsResolver.ts
682
699
  function getTypeAsResolver_default({ resolver, getGraphQLType: getGraphQLType2, options, schema }) {
683
700
  const type2 = getGraphQLType2(resolver.returns, options);
684
- const args = getArgs_default(resolver.params);
701
+ const args = getArgs_default(resolver.params, options);
685
702
  return {
686
703
  type: type2,
687
704
  args,
@@ -739,9 +756,9 @@ function getModelLoadedResolvers(schema, options) {
739
756
  // src/buildSchema/getType/index.ts
740
757
  var createGraphQLObjectType = (modelName, schema, options) => new GraphQLObjectType({
741
758
  name: modelName,
742
- fields: () => buildFields(schema, options)
759
+ fields: () => buildFields2(schema, options)
743
760
  });
744
- var buildFields = (schema, options) => {
761
+ var buildFields2 = (schema, options) => {
745
762
  const fields = {};
746
763
  const addStaticFields = () => {
747
764
  for (const field of getStaticFields(schema)) {
@@ -779,14 +796,14 @@ var buildFields = (schema, options) => {
779
796
  addModelLoadedResolvers();
780
797
  return fields;
781
798
  };
782
- var registeredGraphQLTypes = /* @__PURE__ */ new Map();
799
+ var registeredGraphQLTypes2 = /* @__PURE__ */ new Map();
783
800
  function getGraphQLType(type2, options) {
784
801
  if (!type2) throw new Error("Type is undefined");
785
802
  if (Array.isArray(type2)) {
786
803
  return new GraphQLList2(getGraphQLType(type2[0], options));
787
804
  }
788
- if (isSchemaLike(type2)) {
789
- const schema = getSchemaWithMetadataFromAnyOrionForm(type2);
805
+ if (isSchemaLike2(type2)) {
806
+ const schema = getSchemaWithMetadataFromAnyOrionForm2(type2);
790
807
  const modelName = schema.__modelName;
791
808
  if (schema.__graphQLType) {
792
809
  return schema.__graphQLType;
@@ -796,15 +813,15 @@ function getGraphQLType(type2, options) {
796
813
  `Schema name is not defined. Register a name with schemaWithName. Schema: ${JSON.stringify(schema)}`
797
814
  );
798
815
  }
799
- if (registeredGraphQLTypes.has(modelName)) {
800
- const { graphQLType: graphQLType2, schema: registeredSchema } = registeredGraphQLTypes.get(modelName);
816
+ if (registeredGraphQLTypes2.has(modelName)) {
817
+ const { graphQLType: graphQLType2, schema: registeredSchema } = registeredGraphQLTypes2.get(modelName);
801
818
  if (equals(registeredSchema, schema)) {
802
819
  return graphQLType2;
803
820
  }
804
821
  throw new Error(`Schema named "${modelName}" already registered`);
805
822
  }
806
823
  const graphQLType = createGraphQLObjectType(modelName, schema, options);
807
- registeredGraphQLTypes.set(modelName, { schema, graphQLType });
824
+ registeredGraphQLTypes2.set(modelName, { schema, graphQLType });
808
825
  return graphQLType;
809
826
  }
810
827
  return getScalar_default(getFieldType2(type2));
@@ -826,7 +843,7 @@ async function getResolvers_default(options, mutation) {
826
843
  for (const { resolver, name } of filteredResolvers) {
827
844
  resolversStore[name] = resolver;
828
845
  const type2 = await getGraphQLType(resolver.returns, options);
829
- const args = await getArgs_default(resolver.params);
846
+ const args = await getArgs_default(resolver.params, options);
830
847
  fields[name] = {
831
848
  type: type2,
832
849
  args,
@@ -876,7 +893,7 @@ async function getSubscriptions_default(options) {
876
893
  const subscription = subscriptions[key];
877
894
  subscription.name = subscription.name || key;
878
895
  const type2 = await getGraphQLType(subscription.returns, options);
879
- const args = await getArgs_default(subscription.params);
896
+ const args = await getArgs_default(subscription.params, options);
880
897
  fields[subscription.name] = {
881
898
  type: type2,
882
899
  args,
@@ -1064,8 +1081,8 @@ import { getSchemaFromAnyOrionForm as getSchemaFromAnyOrionForm2 } from "@orion-
1064
1081
  // src/resolversSchemas/getField.ts
1065
1082
  import {
1066
1083
  getFieldType as getFieldType3,
1067
- getSchemaWithMetadataFromAnyOrionForm as getSchemaWithMetadataFromAnyOrionForm2,
1068
- isSchemaLike as isSchemaLike2
1084
+ getSchemaWithMetadataFromAnyOrionForm as getSchemaWithMetadataFromAnyOrionForm3,
1085
+ isSchemaLike as isSchemaLike3
1069
1086
  } from "@orion-js/schema";
1070
1087
  async function getParams(field) {
1071
1088
  const { type: type2 } = field;
@@ -1077,9 +1094,9 @@ async function getParams(field) {
1077
1094
  __graphQLType: `[${serialized.__graphQLType}]`
1078
1095
  };
1079
1096
  }
1080
- const isSchema = isSchemaLike2(type2);
1097
+ const isSchema = isSchemaLike3(type2);
1081
1098
  if (isSchema) {
1082
- const schemaOfType = getSchemaWithMetadataFromAnyOrionForm2(type2);
1099
+ const schemaOfType = getSchemaWithMetadataFromAnyOrionForm3(type2);
1083
1100
  const modelName = schemaOfType.__modelName;
1084
1101
  if (!modelName) {
1085
1102
  throw new Error("The schema needs a model name to be serialized for GraphQL");
@@ -1110,21 +1127,18 @@ async function serializeSchema(params) {
1110
1127
  const schema = getSchemaFromAnyOrionForm2(params);
1111
1128
  if (Object.keys(schema).length === 0) return;
1112
1129
  const fields = {};
1113
- console.log(schema, "schema");
1114
1130
  for (const key of Object.keys(schema).filter((key2) => !key2.startsWith("__"))) {
1115
1131
  const field = schema[key];
1116
1132
  fields[key] = await getParams(field);
1117
- console.log(fields[key], field);
1118
1133
  }
1119
- console.log(fields, "fields");
1120
1134
  return fields;
1121
1135
  }
1122
1136
 
1123
1137
  // src/resolversSchemas/params.ts
1124
- import { getSchemaFromAnyOrionForm as getSchemaFromAnyOrionForm4, isSchemaLike as isSchemaLike4 } from "@orion-js/schema";
1138
+ import { getSchemaFromAnyOrionForm as getSchemaFromAnyOrionForm4, isSchemaLike as isSchemaLike5 } from "@orion-js/schema";
1125
1139
 
1126
1140
  // src/resolversSchemas/getBasicResultQuery.ts
1127
- import { getSchemaFromAnyOrionForm as getSchemaFromAnyOrionForm3, isSchemaLike as isSchemaLike3 } from "@orion-js/schema";
1141
+ import { getSchemaFromAnyOrionForm as getSchemaFromAnyOrionForm3, isSchemaLike as isSchemaLike4 } from "@orion-js/schema";
1128
1142
  async function getBasicQuery(field) {
1129
1143
  if (!field.type) return "";
1130
1144
  if (Array.isArray(field.type)) {
@@ -1133,7 +1147,7 @@ async function getBasicQuery(field) {
1133
1147
  type: field.type[0]
1134
1148
  });
1135
1149
  }
1136
- if (!isSchemaLike3(field.type)) {
1150
+ if (!isSchemaLike4(field.type)) {
1137
1151
  return field.key;
1138
1152
  }
1139
1153
  const schema = getSchemaFromAnyOrionForm3(field.type);
@@ -1154,7 +1168,7 @@ function getResultTypeName(type2) {
1154
1168
  }
1155
1169
  async function getInternalBasicResultQuery(type2) {
1156
1170
  const returns = Array.isArray(type2) ? type2[0] : type2;
1157
- if (isSchemaLike4(returns)) {
1171
+ if (isSchemaLike5(returns)) {
1158
1172
  const schema = getSchemaFromAnyOrionForm4(returns);
1159
1173
  return await getBasicQuery({
1160
1174
  type: schema