graphql-data-generator 0.4.6 → 0.4.7-alpha.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/esm/codegen.js CHANGED
@@ -5,6 +5,38 @@ import { join, relative } from "node:path";
5
5
  import { raise } from "./util.js";
6
6
  import process from "node:process";
7
7
  import { convertFactory, } from "@graphql-codegen/visitor-plugin-common";
8
+ const mergeSerializable = (existing, incoming) => {
9
+ if (!existing)
10
+ return incoming;
11
+ if (existing.kind === "Object" && incoming.kind === "Object") {
12
+ const merged = { ...existing.value };
13
+ for (const [k, v] of Object.entries(incoming.value)) {
14
+ merged[k] = mergeSerializable(merged[k], v);
15
+ }
16
+ const typenameDesc = Object.getOwnPropertyDescriptor(existing.value, "__typename") ??
17
+ Object.getOwnPropertyDescriptor(incoming.value, "__typename");
18
+ if (typenameDesc)
19
+ Object.defineProperty(merged, "__typename", typenameDesc);
20
+ return {
21
+ kind: "Object",
22
+ value: merged,
23
+ conditionals: [
24
+ ...(existing.conditionals ?? []),
25
+ ...(incoming.conditionals ?? []),
26
+ ],
27
+ nonExhaustive: existing.nonExhaustive ?? incoming.nonExhaustive,
28
+ optional: existing.optional && incoming.optional,
29
+ };
30
+ }
31
+ if (existing.kind === "List" && incoming.kind === "List") {
32
+ return {
33
+ kind: "List",
34
+ value: mergeSerializable(existing.value, incoming.value),
35
+ optional: existing.optional && incoming.optional,
36
+ };
37
+ }
38
+ return incoming;
39
+ };
8
40
  const getType = ({ type, ...props }) => {
9
41
  if (type.kind === "NamedType") {
10
42
  if (props.selections) {
@@ -40,12 +72,21 @@ const getType = ({ type, ...props }) => {
40
72
  },
41
73
  });
42
74
  }
43
- union[group][name] = value;
75
+ union[group][name] = mergeSerializable(union[group][name], value);
44
76
  const def = props.definitions[actualTypeName];
45
77
  // Use originalName for tracking field usage when field is aliased
46
78
  const fieldNameForTracking = originalName ?? name;
47
79
  if (def) {
48
- if (implementations.length) {
80
+ // Propagate to all implementations only when the selection comes
81
+ // from the outer type itself or from an interface that every
82
+ // implementation satisfies (e.g., a fragment on a shared
83
+ // interface). Selections from `... on ConcreteType` should be
84
+ // tracked only on that concrete type.
85
+ const shouldPropagate = implementations.length > 0 && (actualTypeName === type.name.value ||
86
+ (def[0].kind === "InterfaceTypeDefinition" &&
87
+ implementations.every((impl) => impl[0].kind === "ObjectTypeDefinition" &&
88
+ (impl[0].interfaces?.some((i) => i.name.value === actualTypeName) ?? false))));
89
+ if (shouldPropagate) {
49
90
  for (const implementation of implementations) {
50
91
  implementation[1].add(fieldNameForTracking);
51
92
  }
@@ -59,6 +100,13 @@ const getType = ({ type, ...props }) => {
59
100
  delete groupedValues[type.name.value];
60
101
  if (def?.[0].kind === "UnionTypeDefinition") {
61
102
  def[1].add(type.name.value);
103
+ // Ensure every union member gets emitted (even if no fields were
104
+ // selected on it), since the union declaration references it by name.
105
+ for (const memberType of def[0].types ?? []) {
106
+ const member = props.definitions[memberType.name.value];
107
+ if (member)
108
+ member[1].add(memberType.name.value);
109
+ }
62
110
  // This is a terrible solution and we should instead produce a tree that
63
111
  // is simplified
64
112
  let iface = props.definitions[def[0].types?.[0]?.name.value ?? ""]?.[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-data-generator",
3
- "version": "0.4.6",
3
+ "version": "0.4.7-alpha.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/voces/graphql-data-generator.git"
package/script/codegen.js CHANGED
@@ -11,6 +11,38 @@ const node_path_1 = require("node:path");
11
11
  const util_js_1 = require("./util.js");
12
12
  const node_process_1 = __importDefault(require("node:process"));
13
13
  const visitor_plugin_common_1 = require("@graphql-codegen/visitor-plugin-common");
14
+ const mergeSerializable = (existing, incoming) => {
15
+ if (!existing)
16
+ return incoming;
17
+ if (existing.kind === "Object" && incoming.kind === "Object") {
18
+ const merged = { ...existing.value };
19
+ for (const [k, v] of Object.entries(incoming.value)) {
20
+ merged[k] = mergeSerializable(merged[k], v);
21
+ }
22
+ const typenameDesc = Object.getOwnPropertyDescriptor(existing.value, "__typename") ??
23
+ Object.getOwnPropertyDescriptor(incoming.value, "__typename");
24
+ if (typenameDesc)
25
+ Object.defineProperty(merged, "__typename", typenameDesc);
26
+ return {
27
+ kind: "Object",
28
+ value: merged,
29
+ conditionals: [
30
+ ...(existing.conditionals ?? []),
31
+ ...(incoming.conditionals ?? []),
32
+ ],
33
+ nonExhaustive: existing.nonExhaustive ?? incoming.nonExhaustive,
34
+ optional: existing.optional && incoming.optional,
35
+ };
36
+ }
37
+ if (existing.kind === "List" && incoming.kind === "List") {
38
+ return {
39
+ kind: "List",
40
+ value: mergeSerializable(existing.value, incoming.value),
41
+ optional: existing.optional && incoming.optional,
42
+ };
43
+ }
44
+ return incoming;
45
+ };
14
46
  const getType = ({ type, ...props }) => {
15
47
  if (type.kind === "NamedType") {
16
48
  if (props.selections) {
@@ -46,12 +78,21 @@ const getType = ({ type, ...props }) => {
46
78
  },
47
79
  });
48
80
  }
49
- union[group][name] = value;
81
+ union[group][name] = mergeSerializable(union[group][name], value);
50
82
  const def = props.definitions[actualTypeName];
51
83
  // Use originalName for tracking field usage when field is aliased
52
84
  const fieldNameForTracking = originalName ?? name;
53
85
  if (def) {
54
- if (implementations.length) {
86
+ // Propagate to all implementations only when the selection comes
87
+ // from the outer type itself or from an interface that every
88
+ // implementation satisfies (e.g., a fragment on a shared
89
+ // interface). Selections from `... on ConcreteType` should be
90
+ // tracked only on that concrete type.
91
+ const shouldPropagate = implementations.length > 0 && (actualTypeName === type.name.value ||
92
+ (def[0].kind === "InterfaceTypeDefinition" &&
93
+ implementations.every((impl) => impl[0].kind === "ObjectTypeDefinition" &&
94
+ (impl[0].interfaces?.some((i) => i.name.value === actualTypeName) ?? false))));
95
+ if (shouldPropagate) {
55
96
  for (const implementation of implementations) {
56
97
  implementation[1].add(fieldNameForTracking);
57
98
  }
@@ -65,6 +106,13 @@ const getType = ({ type, ...props }) => {
65
106
  delete groupedValues[type.name.value];
66
107
  if (def?.[0].kind === "UnionTypeDefinition") {
67
108
  def[1].add(type.name.value);
109
+ // Ensure every union member gets emitted (even if no fields were
110
+ // selected on it), since the union declaration references it by name.
111
+ for (const memberType of def[0].types ?? []) {
112
+ const member = props.definitions[memberType.name.value];
113
+ if (member)
114
+ member[1].add(memberType.name.value);
115
+ }
68
116
  // This is a terrible solution and we should instead produce a tree that
69
117
  // is simplified
70
118
  let iface = props.definitions[def[0].types?.[0]?.name.value ?? ""]?.[0];