Name generated GraphQL queries and mutations from their root field so servers that reject anonymous operations can invoke generated tools successfully.
Unified · Split
| @@ -0,0 +1,5 @@ | ||
| 1 | +--- | |
| 2 | +"@executor-js/plugin-graphql": patch | |
| 3 | +--- | |
| 4 | + | |
| 5 | +GraphQL sources now emit named operations (e.g. `query Hello { ... }`) instead of anonymous ones. This fixes invocation against servers that reject anonymous operations, and gives APM tooling that keys on the operation name a meaningful value. The operation name is derived from the root field name. | |
| @@ -358,6 +358,36 @@ describe("graphqlPlugin real protocol server", () => { · 357 unmodified lines | ||
| 358 | 358 | }), |
| 359 | 359 | ); |
| 360 | 360 | |
| 361 | + it.effect("sends named operations derived from the field name", () => | |
| 362 | + Effect.gen(function* () { | |
| 363 | + const server = yield* serveGreetingServer; | |
| 364 | + const executor = yield* makeExecutor(); | |
| 365 | + | |
| 366 | + yield* executor.graphql.addIntegration({ | |
| 367 | + endpoint: server.endpoint, | |
| 368 | + slug: "named_ops", | |
| 369 | + }); | |
| 370 | + yield* createOrgConnection(executor, { | |
| 371 | + integration: "named_ops", | |
| 372 | + name: "main", | |
| 373 | + template: "none", | |
| 374 | + value: "unused", | |
| 375 | + }); | |
| 376 | + | |
| 377 | + yield* executor.execute(toolAddr("named_ops", "main", "query.hello"), { name: "Ada" }); | |
| 378 | + yield* server.clearRequests; | |
| 379 | + | |
| 380 | + yield* executor.execute(toolAddr("named_ops", "main", "query.hello"), { name: "Ada" }); | |
| 381 | + yield* executor.execute(toolAddr("named_ops", "main", "mutation.setGreeting"), { | |
| 382 | + message: "hi", | |
| 383 | + }); | |
| 384 | + | |
| 385 | + const requests = yield* server.requests; | |
| 386 | + expect(requests[0]?.payload.query).toMatch(/^query Hello\b/); | |
| 387 | + expect(requests[1]?.payload.query).toMatch(/^mutation SetGreeting\b/); | |
| 388 | + }), | |
| 389 | + ); | |
| 390 | + | |
| 361 | 391 | it.effect("surfaces non-2xx invocation responses as ToolResult.fail", () => |
| 362 | 392 | Effect.gen(function* () { |
| 363 | 393 | const server = yield* serveTestHttpApp((request) => |
| @@ -233,12 +233,19 @@ const buildSelectionSet = ( · 232 unmodified lines | ||
| 233 | 233 | return subFields.length > 0 ? `{ ${subFields.join(" ")} }` : ""; |
| 234 | 234 | }; |
| 235 | 235 | |
| 236 | +// Name every generated operation: some servers reject anonymous operations, and | |
| 237 | +// APM tooling keys traces off the operation name. Field names are already valid | |
| 238 | +// GraphQL name tokens, so the upper-cased field name is a safe operation name. | |
| 239 | +const operationNameForField = (fieldName: string): string => | |
| 240 | + fieldName.charAt(0).toUpperCase() + fieldName.slice(1); | |
| 241 | + | |
| 236 | 242 | const buildOperationStringForField = ( |
| 237 | 243 | kind: GraphqlOperationKind, |
| 238 | 244 | field: IntrospectionField, |
| 239 | 245 | types: ReadonlyMap<string, IntrospectionType>, |
| 240 | 246 | ): string => { |
| 241 | 247 | const opType = kind === "query" ? "query" : "mutation"; |
| 248 | + const opName = operationNameForField(field.name); | |
| 242 | 249 | |
| 243 | 250 | const varDefs = field.args.map((arg) => { |
| 244 | 251 | const typeName = formatTypeRef(arg.type); |
| @@ -251,7 +258,7 @@ const buildOperationStringForField = ( · 6 unmodified lines | ||
| 251 | 258 | const varDefsStr = varDefs.length > 0 ? `(${varDefs.join(", ")})` : ""; |
| 252 | 259 | const argPassStr = argPasses.length > 0 ? `(${argPasses.join(", ")})` : ""; |
| 253 | 260 | |
| 254 | − return `${opType}${varDefsStr} { ${field.name}${argPassStr}${selectionSet ? ` ${selectionSet}` : ""} }`; | |
| 261 | + return `${opType} ${opName}${varDefsStr} { ${field.name}${argPassStr}${selectionSet ? ` ${selectionSet}` : ""} }`; | |
| 255 | 262 | }; |
| 256 | 263 | |
| 257 | 264 | interface PreparedOperation { |
| @@ -299,7 +306,7 @@ const prepareOperations = ( · 41 unmodified lines | ||
| 299 | 306 | const entry = fieldMap.get(key); |
| 300 | 307 | const operationString = entry |
| 301 | 308 | ? buildOperationStringForField(entry.kind, entry.field, typeMap) |
| 302 | − : `${extracted.kind} { ${extracted.fieldName} }`; | |
| 309 | + : `${extracted.kind} ${operationNameForField(extracted.fieldName)} { ${extracted.fieldName} }`; | |
| 303 | 310 | |
| 304 | 311 | const binding = OperationBinding.make({ |
| 305 | 312 | kind: extracted.kind, |