@apollo/client-ai-apps 0.5.2 → 0.5.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.
Files changed (101) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/core/ApolloClient.d.ts +1 -1
  3. package/dist/core/ApolloClient.d.ts.map +1 -1
  4. package/dist/core/ApolloClient.js +1 -1
  5. package/dist/core/ApolloClient.js.map +1 -1
  6. package/dist/mcp/core/ApolloClient.d.ts +6 -1
  7. package/dist/mcp/core/ApolloClient.d.ts.map +1 -1
  8. package/dist/mcp/core/ApolloClient.js +36 -3
  9. package/dist/mcp/core/ApolloClient.js.map +1 -1
  10. package/dist/mcp/core/McpAppManager.d.ts +2 -2
  11. package/dist/mcp/core/McpAppManager.d.ts.map +1 -1
  12. package/dist/mcp/core/McpAppManager.js +3 -3
  13. package/dist/mcp/core/McpAppManager.js.map +1 -1
  14. package/dist/mcp/react/hooks/createHydrationUtils.d.ts +15 -0
  15. package/dist/mcp/react/hooks/createHydrationUtils.d.ts.map +1 -0
  16. package/dist/mcp/react/hooks/createHydrationUtils.js +113 -0
  17. package/dist/mcp/react/hooks/createHydrationUtils.js.map +1 -0
  18. package/dist/mcp/react/index.d.ts +1 -0
  19. package/dist/mcp/react/index.d.ts.map +1 -1
  20. package/dist/mcp/react/index.js +1 -0
  21. package/dist/mcp/react/index.js.map +1 -1
  22. package/dist/openai/core/ApolloClient.d.ts +6 -1
  23. package/dist/openai/core/ApolloClient.d.ts.map +1 -1
  24. package/dist/openai/core/ApolloClient.js +37 -3
  25. package/dist/openai/core/ApolloClient.js.map +1 -1
  26. package/dist/openai/core/McpAppManager.d.ts +2 -2
  27. package/dist/openai/core/McpAppManager.d.ts.map +1 -1
  28. package/dist/openai/core/McpAppManager.js +5 -5
  29. package/dist/openai/core/McpAppManager.js.map +1 -1
  30. package/dist/openai/react/hooks/createHydrationUtils.d.ts +15 -0
  31. package/dist/openai/react/hooks/createHydrationUtils.d.ts.map +1 -0
  32. package/dist/openai/react/hooks/createHydrationUtils.js +113 -0
  33. package/dist/openai/react/hooks/createHydrationUtils.js.map +1 -0
  34. package/dist/openai/react/index.d.ts +1 -0
  35. package/dist/openai/react/index.d.ts.map +1 -1
  36. package/dist/openai/react/index.js +1 -0
  37. package/dist/openai/react/index.js.map +1 -1
  38. package/dist/react/ApolloProvider.js +1 -1
  39. package/dist/react/ApolloProvider.js.map +1 -1
  40. package/dist/react/index.d.ts +4 -0
  41. package/dist/react/index.d.ts.map +1 -1
  42. package/dist/react/index.js +3 -0
  43. package/dist/react/index.js.map +1 -1
  44. package/dist/react/index.mcp.d.ts +1 -1
  45. package/dist/react/index.mcp.d.ts.map +1 -1
  46. package/dist/react/index.mcp.js +1 -1
  47. package/dist/react/index.mcp.js.map +1 -1
  48. package/dist/react/index.openai.d.ts +1 -1
  49. package/dist/react/index.openai.d.ts.map +1 -1
  50. package/dist/react/index.openai.js +1 -1
  51. package/dist/react/index.openai.js.map +1 -1
  52. package/dist/react/reactive.d.ts +9 -0
  53. package/dist/react/reactive.d.ts.map +1 -0
  54. package/dist/react/reactive.js +11 -0
  55. package/dist/react/reactive.js.map +1 -0
  56. package/dist/utilities/getToolNamesFromDocument.d.ts +3 -0
  57. package/dist/utilities/getToolNamesFromDocument.d.ts.map +1 -0
  58. package/dist/utilities/getToolNamesFromDocument.js +12 -0
  59. package/dist/utilities/getToolNamesFromDocument.js.map +1 -0
  60. package/dist/utilities/getVariableNamesFromDocument.d.ts +3 -0
  61. package/dist/utilities/getVariableNamesFromDocument.d.ts.map +1 -0
  62. package/dist/utilities/getVariableNamesFromDocument.js +6 -0
  63. package/dist/utilities/getVariableNamesFromDocument.js.map +1 -0
  64. package/dist/utilities/index.d.ts +3 -0
  65. package/dist/utilities/index.d.ts.map +1 -1
  66. package/dist/utilities/index.js +3 -0
  67. package/dist/utilities/index.js.map +1 -1
  68. package/dist/utilities/warnOnVariableMismatch.d.ts +3 -0
  69. package/dist/utilities/warnOnVariableMismatch.d.ts.map +1 -0
  70. package/dist/utilities/warnOnVariableMismatch.js +10 -0
  71. package/dist/utilities/warnOnVariableMismatch.js.map +1 -0
  72. package/dist/vite/apolloClientAiApps.d.ts.map +1 -1
  73. package/dist/vite/apolloClientAiApps.js +35 -31
  74. package/dist/vite/apolloClientAiApps.js.map +1 -1
  75. package/package.json +2 -1
  76. package/src/core/ApolloClient.ts +1 -1
  77. package/src/mcp/core/ApolloClient.ts +67 -2
  78. package/src/mcp/core/McpAppManager.ts +3 -3
  79. package/src/mcp/core/__tests__/ApolloClient.test.ts +109 -6
  80. package/src/mcp/link/__tests__/ToolCallLink.test.ts +1 -1
  81. package/src/mcp/react/hooks/__tests__/createHydrationUtils.test.tsx +1228 -0
  82. package/src/mcp/react/hooks/createHydrationUtils.ts +182 -0
  83. package/src/mcp/react/index.ts +1 -0
  84. package/src/openai/core/ApolloClient.ts +68 -2
  85. package/src/openai/core/McpAppManager.ts +5 -5
  86. package/src/openai/core/__tests__/ApolloClient.test.ts +113 -7
  87. package/src/openai/react/hooks/__tests__/createHydrationUtils.test.tsx +1333 -0
  88. package/src/openai/react/hooks/createHydrationUtils.ts +182 -0
  89. package/src/openai/react/index.ts +1 -0
  90. package/src/react/ApolloProvider.tsx +1 -1
  91. package/src/react/index.mcp.ts +1 -0
  92. package/src/react/index.openai.ts +1 -0
  93. package/src/react/index.ts +7 -0
  94. package/src/react/reactive.ts +19 -0
  95. package/src/testing/internal/mcp/graphqlToolResult.ts +5 -5
  96. package/src/utilities/getToolNamesFromDocument.ts +15 -0
  97. package/src/utilities/getVariableNamesFromDocument.ts +9 -0
  98. package/src/utilities/index.ts +3 -0
  99. package/src/utilities/warnOnVariableMismatch.ts +20 -0
  100. package/src/vite/__tests__/apolloClientAiApps.test.ts +183 -2
  101. package/src/vite/apolloClientAiApps.ts +46 -40
@@ -14,7 +14,7 @@ import { glob } from "glob";
14
14
  import { print } from "@apollo/client/utilities";
15
15
  import { removeDirectivesFromDocument } from "@apollo/client/utilities/internal";
16
16
  import { of } from "rxjs";
17
- import { Kind, parse, type OperationDefinitionNode } from "graphql";
17
+ import { Kind, OperationTypeNode, parse } from "graphql";
18
18
  import {
19
19
  getArgumentValue,
20
20
  getDirectiveArgument,
@@ -30,6 +30,7 @@ import { explorer } from "./utilities/config.js";
30
30
  import type { ApolloClientAiAppsConfig } from "../config/index.js";
31
31
  import { ApolloClientAiAppsConfigSchema } from "../config/schema.js";
32
32
  import { z } from "zod";
33
+ import { createFragmentRegistry } from "@apollo/client/cache";
33
34
 
34
35
  export declare namespace apolloClientAiApps {
35
36
  export type Target = ApolloClientAiAppsConfig.AppTarget;
@@ -66,7 +67,7 @@ export function devTarget(target: string | undefined) {
66
67
  interface FileCache {
67
68
  file: string;
68
69
  hash: string;
69
- operations: ManifestOperation[];
70
+ sources: DocumentNode[];
70
71
  }
71
72
 
72
73
  export function apolloClientAiApps(
@@ -79,6 +80,8 @@ export function apolloClientAiApps(
79
80
  let packageJson!: Record<string, any>;
80
81
  let config!: ResolvedConfig;
81
82
 
83
+ const fragments = createFragmentRegistry();
84
+
82
85
  invariant(
83
86
  Array.isArray(targets) && targets.length > 0,
84
87
  "The `targets` option must be a non-empty array"
@@ -90,7 +93,7 @@ export function apolloClientAiApps(
90
93
  );
91
94
 
92
95
  const client = new ApolloClient({
93
- cache: new InMemoryCache(),
96
+ cache: new InMemoryCache({ fragments }),
94
97
  link: processQueryLink,
95
98
  });
96
99
 
@@ -106,52 +109,57 @@ export function apolloClientAiApps(
106
109
  { name: "graphql-tag", identifier: "gql" },
107
110
  { name: "@apollo/client", identifier: "gql" },
108
111
  ],
109
- }).map((source) => ({
110
- node: parse(source.body),
111
- file,
112
- location: source.locationOffset,
113
- }));
112
+ }).map((source) => parse(source.body));
114
113
 
115
- const operations: ManifestOperation[] = [];
116
- for (const source of sources) {
117
- const type = (
118
- source.node.definitions.find(
119
- (d) => d.kind === "OperationDefinition"
120
- ) as OperationDefinitionNode
121
- ).operation;
122
-
123
- let result;
124
- if (type === "query") {
125
- result = await client.query({
126
- query: source.node,
127
- fetchPolicy: "no-cache",
128
- });
129
- } else if (type === "mutation") {
130
- result = await client.mutate({
131
- mutation: source.node,
132
- fetchPolicy: "no-cache",
133
- });
134
- } else {
135
- throw new Error(
136
- "Found an unsupported operation type. Only Query and Mutation are supported."
137
- );
138
- }
139
- operations.push(result.data as ManifestOperation);
140
- }
114
+ fragments.register(...sources);
141
115
 
142
116
  cache.set(file, {
143
117
  file: file,
144
118
  hash: fileHash,
145
- operations,
119
+ sources,
146
120
  });
147
121
  }
148
122
 
149
123
  async function generateManifest(environment?: Environment) {
150
124
  const appsConfig = await getAppsConfig();
151
- const operations = Array.from(cache.values()).flatMap(
152
- (entry) => entry.operations
125
+ const sources = Array.from(cache.values()).flatMap(
126
+ (entry) => entry.sources
153
127
  );
154
128
 
129
+ const operations: ManifestOperation[] = [];
130
+ for (const source of sources) {
131
+ const operationDef = source.definitions.find(
132
+ (d) => d.kind === Kind.OPERATION_DEFINITION
133
+ );
134
+
135
+ if (!operationDef) continue;
136
+
137
+ switch (operationDef.operation) {
138
+ case OperationTypeNode.QUERY: {
139
+ const result = await client.query<ManifestOperation>({
140
+ query: source,
141
+ fetchPolicy: "no-cache",
142
+ });
143
+
144
+ operations.push(result.data!);
145
+ break;
146
+ }
147
+ case OperationTypeNode.MUTATION: {
148
+ const result = await client.mutate<ManifestOperation>({
149
+ mutation: source,
150
+ fetchPolicy: "no-cache",
151
+ });
152
+
153
+ operations.push(result.data!);
154
+ break;
155
+ }
156
+ default:
157
+ throw new Error(
158
+ `Found unsupported operation type '${operationDef.operation}'. Only queries and mutations are supported.`
159
+ );
160
+ }
161
+ }
162
+
155
163
  invariant(
156
164
  operations.filter((o) => o.prefetch).length <= 1,
157
165
  "Found multiple operations marked as `@prefetch`. You can only mark 1 operation with `@prefetch`."
@@ -192,9 +200,7 @@ export function apolloClientAiApps(
192
200
  name: appsConfig.name ?? packageJson.name,
193
201
  description: appsConfig.description ?? packageJson.description,
194
202
  hash: createHash("sha256").update(Date.now().toString()).digest("hex"),
195
- operations: Array.from(cache.values()).flatMap(
196
- (entry) => entry.operations
197
- ),
203
+ operations,
198
204
  resource,
199
205
  csp: {
200
206
  connectDomains: appsConfig.csp?.connectDomains ?? [],