@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/dist/index.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import "node:module";
2
2
  import { err, ok } from "neverthrow";
3
+ import { createSchemaIndex } from "@soda-gql/core";
3
4
  import { Kind, concatAST, parse, print } from "graphql";
4
5
  import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
5
6
  import { basename, dirname, extname, join, relative, resolve } from "node:path";
@@ -1945,186 +1946,6 @@ const builtinScalarTypes$1 = new Map([
1945
1946
  output: "boolean"
1946
1947
  }]
1947
1948
  ]);
1948
- const ensureRecord = (collection, key, factory) => {
1949
- const existing = collection.get(key);
1950
- if (existing) {
1951
- return existing;
1952
- }
1953
- const created = factory(key);
1954
- collection.set(key, created);
1955
- return created;
1956
- };
1957
- const addObjectFields = (target, fields) => {
1958
- if (!fields) {
1959
- return;
1960
- }
1961
- for (const field of fields) {
1962
- target.set(field.name.value, field);
1963
- }
1964
- };
1965
- const addInputFields = (target, fields) => {
1966
- if (!fields) {
1967
- return;
1968
- }
1969
- for (const field of fields) {
1970
- target.set(field.name.value, field);
1971
- }
1972
- };
1973
- const addEnumValues = (target, values) => {
1974
- if (!values) {
1975
- return;
1976
- }
1977
- for (const value of values) {
1978
- target.set(value.name.value, value);
1979
- }
1980
- };
1981
- const addUnionMembers = (target, members) => {
1982
- if (!members) {
1983
- return;
1984
- }
1985
- for (const member of members) {
1986
- target.set(member.name.value, member);
1987
- }
1988
- };
1989
- const mergeDirectives = (existing, incoming, precedence) => {
1990
- const current = existing ?? [];
1991
- const next = incoming ? Array.from(incoming) : [];
1992
- return precedence === "definition" ? [...next, ...current] : [...current, ...next];
1993
- };
1994
- const updateOperationTypes = (operationTypes, definition) => {
1995
- for (const operation of definition.operationTypes ?? []) {
1996
- const typeName = operation.type.name.value;
1997
- switch (operation.operation) {
1998
- case "query":
1999
- operationTypes.query = typeName;
2000
- break;
2001
- case "mutation":
2002
- operationTypes.mutation = typeName;
2003
- break;
2004
- case "subscription":
2005
- operationTypes.subscription = typeName;
2006
- break;
2007
- default: break;
2008
- }
2009
- }
2010
- };
2011
- const addDirectiveArgs = (target, args) => {
2012
- if (!args) {
2013
- return;
2014
- }
2015
- for (const arg of args) {
2016
- target.set(arg.name.value, arg);
2017
- }
2018
- };
2019
- const createSchemaIndex = (document) => {
2020
- const objects = new Map();
2021
- const inputs = new Map();
2022
- const enums = new Map();
2023
- const unions = new Map();
2024
- const scalars = new Map();
2025
- const directives = new Map();
2026
- const operationTypes = {};
2027
- for (const definition of document.definitions) {
2028
- switch (definition.kind) {
2029
- case Kind.OBJECT_TYPE_DEFINITION:
2030
- case Kind.OBJECT_TYPE_EXTENSION: {
2031
- const precedence = definition.kind === Kind.OBJECT_TYPE_DEFINITION ? "definition" : "extension";
2032
- const record = ensureRecord(objects, definition.name.value, (name) => ({
2033
- name,
2034
- fields: new Map(),
2035
- directives: []
2036
- }));
2037
- addObjectFields(record.fields, definition.fields);
2038
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2039
- break;
2040
- }
2041
- case Kind.INPUT_OBJECT_TYPE_DEFINITION:
2042
- case Kind.INPUT_OBJECT_TYPE_EXTENSION: {
2043
- const precedence = definition.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ? "definition" : "extension";
2044
- const record = ensureRecord(inputs, definition.name.value, (name) => ({
2045
- name,
2046
- fields: new Map(),
2047
- directives: []
2048
- }));
2049
- addInputFields(record.fields, definition.fields);
2050
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2051
- break;
2052
- }
2053
- case Kind.ENUM_TYPE_DEFINITION:
2054
- case Kind.ENUM_TYPE_EXTENSION: {
2055
- const precedence = definition.kind === Kind.ENUM_TYPE_DEFINITION ? "definition" : "extension";
2056
- const record = ensureRecord(enums, definition.name.value, (name) => ({
2057
- name,
2058
- values: new Map(),
2059
- directives: []
2060
- }));
2061
- addEnumValues(record.values, definition.values);
2062
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2063
- break;
2064
- }
2065
- case Kind.UNION_TYPE_DEFINITION:
2066
- case Kind.UNION_TYPE_EXTENSION: {
2067
- const precedence = definition.kind === Kind.UNION_TYPE_DEFINITION ? "definition" : "extension";
2068
- const record = ensureRecord(unions, definition.name.value, (name) => ({
2069
- name,
2070
- members: new Map(),
2071
- directives: []
2072
- }));
2073
- addUnionMembers(record.members, definition.types);
2074
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2075
- break;
2076
- }
2077
- case Kind.SCALAR_TYPE_DEFINITION:
2078
- case Kind.SCALAR_TYPE_EXTENSION: {
2079
- const precedence = definition.kind === Kind.SCALAR_TYPE_DEFINITION ? "definition" : "extension";
2080
- const record = ensureRecord(scalars, definition.name.value, (name) => ({
2081
- name,
2082
- directives: []
2083
- }));
2084
- record.directives = mergeDirectives(record.directives, definition.directives, precedence);
2085
- break;
2086
- }
2087
- case Kind.DIRECTIVE_DEFINITION: {
2088
- const name = definition.name.value;
2089
- if (name === "skip" || name === "include" || name === "deprecated" || name === "specifiedBy") {
2090
- break;
2091
- }
2092
- const args = new Map();
2093
- addDirectiveArgs(args, definition.arguments);
2094
- directives.set(name, {
2095
- name,
2096
- locations: definition.locations.map((loc) => loc.value),
2097
- args,
2098
- isRepeatable: definition.repeatable
2099
- });
2100
- break;
2101
- }
2102
- case Kind.SCHEMA_DEFINITION:
2103
- case Kind.SCHEMA_EXTENSION:
2104
- updateOperationTypes(operationTypes, definition);
2105
- break;
2106
- default: break;
2107
- }
2108
- }
2109
- if (!operationTypes.query && objects.has("Query")) {
2110
- operationTypes.query = "Query";
2111
- }
2112
- if (!operationTypes.mutation && objects.has("Mutation")) {
2113
- operationTypes.mutation = "Mutation";
2114
- }
2115
- if (!operationTypes.subscription && objects.has("Subscription")) {
2116
- operationTypes.subscription = "Subscription";
2117
- }
2118
- return {
2119
- objects,
2120
- inputs,
2121
- enums,
2122
- unions,
2123
- scalars,
2124
- directives,
2125
- operationTypes
2126
- };
2127
- };
2128
1949
  const collectTypeLevels = (type, nonNull = false, levels = []) => {
2129
1950
  if (type.kind === Kind.NON_NULL_TYPE) {
2130
1951
  return collectTypeLevels(type.type, true, levels);
@@ -3144,6 +2965,10 @@ const extractValue = (node) => {
3144
2965
  //#endregion
3145
2966
  //#region packages/codegen/src/graphql-compat/transformer.ts
3146
2967
  /**
2968
+ * Transformer for enriching parsed GraphQL operations with schema information.
2969
+ * @module
2970
+ */
2971
+ /**
3147
2972
  * Built-in GraphQL scalar types.
3148
2973
  */
3149
2974
  const builtinScalarTypes = new Set([