@soda-gql/codegen 0.12.3 → 0.12.4

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
@@ -34,7 +34,7 @@ const result = await runCodegen({
34
34
  format: "json",
35
35
  schemas: {
36
36
  default: {
37
- schema: "./schema.graphql",
37
+ schema: ["./schema.graphql"],
38
38
  inject: {
39
39
  scalars: "./scalars.ts",
40
40
  },
package/dist/index.cjs CHANGED
@@ -27,6 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
27
27
 
28
28
  //#endregion
29
29
  let neverthrow = require("neverthrow");
30
+ let __soda_gql_core = require("@soda-gql/core");
30
31
  let graphql = require("graphql");
31
32
  let node_fs = require("node:fs");
32
33
  let node_path = require("node:path");
@@ -1944,186 +1945,6 @@ const builtinScalarTypes$1 = new Map([
1944
1945
  output: "boolean"
1945
1946
  }]
1946
1947
  ]);
1947
- const ensureRecord = (collection, key, factory) => {
1948
- const existing = collection.get(key);
1949
- if (existing) {
1950
- return existing;
1951
- }
1952
- const created = factory(key);
1953
- collection.set(key, created);
1954
- return created;
1955
- };
1956
- const addObjectFields = (target, fields) => {
1957
- if (!fields) {
1958
- return;
1959
- }
1960
- for (const field of fields) {
1961
- target.set(field.name.value, field);
1962
- }
1963
- };
1964
- const addInputFields = (target, fields) => {
1965
- if (!fields) {
1966
- return;
1967
- }
1968
- for (const field of fields) {
1969
- target.set(field.name.value, field);
1970
- }
1971
- };
1972
- const addEnumValues = (target, values) => {
1973
- if (!values) {
1974
- return;
1975
- }
1976
- for (const value of values) {
1977
- target.set(value.name.value, value);
1978
- }
1979
- };
1980
- const addUnionMembers = (target, members) => {
1981
- if (!members) {
1982
- return;
1983
- }
1984
- for (const member of members) {
1985
- target.set(member.name.value, member);
1986
- }
1987
- };
1988
- const mergeDirectives = (existing, incoming, precedence) => {
1989
- const current = existing ?? [];
1990
- const next = incoming ? Array.from(incoming) : [];
1991
- return precedence === "definition" ? [...next, ...current] : [...current, ...next];
1992
- };
1993
- const updateOperationTypes = (operationTypes, definition) => {
1994
- for (const operation of definition.operationTypes ?? []) {
1995
- const typeName = operation.type.name.value;
1996
- switch (operation.operation) {
1997
- case "query":
1998
- operationTypes.query = typeName;
1999
- break;
2000
- case "mutation":
2001
- operationTypes.mutation = typeName;
2002
- break;
2003
- case "subscription":
2004
- operationTypes.subscription = typeName;
2005
- break;
2006
- default: break;
2007
- }
2008
- }
2009
- };
2010
- const addDirectiveArgs = (target, args) => {
2011
- if (!args) {
2012
- return;
2013
- }
2014
- for (const arg of args) {
2015
- target.set(arg.name.value, arg);
2016
- }
2017
- };
2018
- const createSchemaIndex = (document) => {
2019
- const objects = new Map();
2020
- const inputs = new Map();
2021
- const enums = new Map();
2022
- const unions = new Map();
2023
- const scalars = new Map();
2024
- const directives = new Map();
2025
- const operationTypes = {};
2026
- for (const definition of document.definitions) {
2027
- switch (definition.kind) {
2028
- case graphql.Kind.OBJECT_TYPE_DEFINITION:
2029
- case graphql.Kind.OBJECT_TYPE_EXTENSION: {
2030
- const precedence = definition.kind === graphql.Kind.OBJECT_TYPE_DEFINITION ? "definition" : "extension";
2031
- const record = ensureRecord(objects, definition.name.value, (name) => ({
2032
- name,
2033
- fields: new Map(),
2034
- directives: []
2035
- }));
2036
- addObjectFields(record.fields, definition.fields);
2037
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2038
- break;
2039
- }
2040
- case graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION:
2041
- case graphql.Kind.INPUT_OBJECT_TYPE_EXTENSION: {
2042
- const precedence = definition.kind === graphql.Kind.INPUT_OBJECT_TYPE_DEFINITION ? "definition" : "extension";
2043
- const record = ensureRecord(inputs, definition.name.value, (name) => ({
2044
- name,
2045
- fields: new Map(),
2046
- directives: []
2047
- }));
2048
- addInputFields(record.fields, definition.fields);
2049
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2050
- break;
2051
- }
2052
- case graphql.Kind.ENUM_TYPE_DEFINITION:
2053
- case graphql.Kind.ENUM_TYPE_EXTENSION: {
2054
- const precedence = definition.kind === graphql.Kind.ENUM_TYPE_DEFINITION ? "definition" : "extension";
2055
- const record = ensureRecord(enums, definition.name.value, (name) => ({
2056
- name,
2057
- values: new Map(),
2058
- directives: []
2059
- }));
2060
- addEnumValues(record.values, definition.values);
2061
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2062
- break;
2063
- }
2064
- case graphql.Kind.UNION_TYPE_DEFINITION:
2065
- case graphql.Kind.UNION_TYPE_EXTENSION: {
2066
- const precedence = definition.kind === graphql.Kind.UNION_TYPE_DEFINITION ? "definition" : "extension";
2067
- const record = ensureRecord(unions, definition.name.value, (name) => ({
2068
- name,
2069
- members: new Map(),
2070
- directives: []
2071
- }));
2072
- addUnionMembers(record.members, definition.types);
2073
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2074
- break;
2075
- }
2076
- case graphql.Kind.SCALAR_TYPE_DEFINITION:
2077
- case graphql.Kind.SCALAR_TYPE_EXTENSION: {
2078
- const precedence = definition.kind === graphql.Kind.SCALAR_TYPE_DEFINITION ? "definition" : "extension";
2079
- const record = ensureRecord(scalars, definition.name.value, (name) => ({
2080
- name,
2081
- directives: []
2082
- }));
2083
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2084
- break;
2085
- }
2086
- case graphql.Kind.DIRECTIVE_DEFINITION: {
2087
- const name = definition.name.value;
2088
- if (name === "skip" || name === "include" || name === "deprecated" || name === "specifiedBy") {
2089
- break;
2090
- }
2091
- const args = new Map();
2092
- addDirectiveArgs(args, definition.arguments);
2093
- directives.set(name, {
2094
- name,
2095
- locations: definition.locations.map((loc) => loc.value),
2096
- args,
2097
- isRepeatable: definition.repeatable
2098
- });
2099
- break;
2100
- }
2101
- case graphql.Kind.SCHEMA_DEFINITION:
2102
- case graphql.Kind.SCHEMA_EXTENSION:
2103
- updateOperationTypes(operationTypes, definition);
2104
- break;
2105
- default: break;
2106
- }
2107
- }
2108
- if (!operationTypes.query && objects.has("Query")) {
2109
- operationTypes.query = "Query";
2110
- }
2111
- if (!operationTypes.mutation && objects.has("Mutation")) {
2112
- operationTypes.mutation = "Mutation";
2113
- }
2114
- if (!operationTypes.subscription && objects.has("Subscription")) {
2115
- operationTypes.subscription = "Subscription";
2116
- }
2117
- return {
2118
- objects,
2119
- inputs,
2120
- enums,
2121
- unions,
2122
- scalars,
2123
- directives,
2124
- operationTypes
2125
- };
2126
- };
2127
1948
  const collectTypeLevels = (type, nonNull = false, levels = []) => {
2128
1949
  if (type.kind === graphql.Kind.NON_NULL_TYPE) {
2129
1950
  return collectTypeLevels(type.type, true, levels);
@@ -2609,7 +2430,7 @@ const generateMultiSchemaModule = (schemas, options) => {
2609
2430
  unions: 0
2610
2431
  };
2611
2432
  for (const [name, document] of schemas.entries()) {
2612
- const schema = createSchemaIndex(document);
2433
+ const schema = (0, __soda_gql_core.createSchemaIndex)(document);
2613
2434
  const typeFilterConfig = options?.typeFilters?.get(name);
2614
2435
  const typeFilter = compileTypeFilter(typeFilterConfig);
2615
2436
  const allTypeNames = new Map([
@@ -3143,6 +2964,10 @@ const extractValue = (node) => {
3143
2964
  //#endregion
3144
2965
  //#region packages/codegen/src/graphql-compat/transformer.ts
3145
2966
  /**
2967
+ * Transformer for enriching parsed GraphQL operations with schema information.
2968
+ * @module
2969
+ */
2970
+ /**
3146
2971
  * Built-in GraphQL scalar types.
3147
2972
  */
3148
2973
  const builtinScalarTypes = new Set([
@@ -3588,7 +3413,7 @@ const isEnumName = (schema, name) => schema.enums.has(name);
3588
3413
  * fragment dependencies, and infers variables for fragments.
3589
3414
  */
3590
3415
  const transformParsedGraphql = (parsed, options) => {
3591
- const schema = createSchemaIndex(options.schemaDocument);
3416
+ const schema = (0, __soda_gql_core.createSchemaIndex)(options.schemaDocument);
3592
3417
  const sortResult = sortFragmentsByDependency(parsed.fragments);
3593
3418
  if (sortResult.isErr()) {
3594
3419
  return (0, neverthrow.err)(sortResult.error);
@@ -3744,7 +3569,7 @@ const getRootTypeName = (schema, kind) => {
3744
3569
  */
3745
3570
  const emitOperation = (operation, options) => {
3746
3571
  const lines = [];
3747
- const schema = options.schemaDocument ? createSchemaIndex(options.schemaDocument) : null;
3572
+ const schema = options.schemaDocument ? (0, __soda_gql_core.createSchemaIndex)(options.schemaDocument) : null;
3748
3573
  const exportName = `${operation.name}Compat`;
3749
3574
  const operationType = operation.kind;
3750
3575
  lines.push(`export const ${exportName} = gql.${options.schemaName}(({ ${operationType}, $var }) =>`);
@@ -3770,7 +3595,7 @@ const emitOperation = (operation, options) => {
3770
3595
  */
3771
3596
  const emitFragment = (fragment, options) => {
3772
3597
  const lines = [];
3773
- const schema = options.schemaDocument ? createSchemaIndex(options.schemaDocument) : null;
3598
+ const schema = options.schemaDocument ? (0, __soda_gql_core.createSchemaIndex)(options.schemaDocument) : null;
3774
3599
  const hasVariables = fragment.variables.length > 0;
3775
3600
  const exportName = `${fragment.name}Fragment`;
3776
3601
  const destructure = hasVariables ? "fragment, $var" : "fragment";
@@ -4175,7 +4000,7 @@ const addEdge = (graph, from, to) => {
4175
4000
  edges.add(to);
4176
4001
  };
4177
4002
  const buildTypeGraph = (document) => {
4178
- const schema = createSchemaIndex(document);
4003
+ const schema = (0, __soda_gql_core.createSchemaIndex)(document);
4179
4004
  const forward = new Map();
4180
4005
  const reverse = new Map();
4181
4006
  const addBidirectional = (from, to) => {
@@ -4764,7 +4589,7 @@ const runCodegen = async (options) => {
4764
4589
  const schemaNames = Object.keys(options.schemas);
4765
4590
  const allFieldNames = new Map();
4766
4591
  for (const [name, document] of schemas.entries()) {
4767
- const schemaIndex = createSchemaIndex(document);
4592
+ const schemaIndex = (0, __soda_gql_core.createSchemaIndex)(document);
4768
4593
  const fieldNameSet = new Set();
4769
4594
  for (const [objectName, record] of schemaIndex.objects.entries()) {
4770
4595
  if (objectName.startsWith("__")) continue;