History

Emit named GraphQL operations for generated tools

Name generated GraphQL queries and mutations from their root field so servers that reject anonymous operations can invoke generated tools successfully.

Oscar Nord committed 5 months ago

Commit 5e1dfc21 parent 93876f53 files · +44 −2Browse files

Unified · Split

3 files
  1. .changeset/named-graphql-operations.md +5 −0
  2. packages/plugins/graphql/src/sdk/plugin.test.ts +30 −0
  3. packages/plugins/graphql/src/sdk/plugin.ts +9 −2
.changeset/named-graphql-operations.md+5−0
@@ -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.
packages/plugins/graphql/src/sdk/plugin.test.ts+30−0
@@ -358,6 +358,36 @@ describe("graphqlPlugin real protocol server", () => { · 357 unmodified lines
358358 }),
359359 );
360360
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+
361391 it.effect("surfaces non-2xx invocation responses as ToolResult.fail", () =>
362392 Effect.gen(function* () {
363393 const server = yield* serveTestHttpApp((request) =>
packages/plugins/graphql/src/sdk/plugin.ts+9−2
@@ -233,12 +233,19 @@ const buildSelectionSet = ( · 232 unmodified lines
233233 return subFields.length > 0 ? `{ ${subFields.join(" ")} }` : "";
234234 };
235235
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+
236242 const buildOperationStringForField = (
237243 kind: GraphqlOperationKind,
238244 field: IntrospectionField,
239245 types: ReadonlyMap<string, IntrospectionType>,
240246 ): string => {
241247 const opType = kind === "query" ? "query" : "mutation";
248+ const opName = operationNameForField(field.name);
242249
243250 const varDefs = field.args.map((arg) => {
244251 const typeName = formatTypeRef(arg.type);
@@ -251,7 +258,7 @@ const buildOperationStringForField = ( · 6 unmodified lines
251258 const varDefsStr = varDefs.length > 0 ? `(${varDefs.join(", ")})` : "";
252259 const argPassStr = argPasses.length > 0 ? `(${argPasses.join(", ")})` : "";
253260
254− return `${opType}${varDefsStr} { ${field.name}${argPassStr}${selectionSet ? ` ${selectionSet}` : ""} }`;
261+ return `${opType} ${opName}${varDefsStr} { ${field.name}${argPassStr}${selectionSet ? ` ${selectionSet}` : ""} }`;
255262 };
256263
257264 interface PreparedOperation {
@@ -299,7 +306,7 @@ const prepareOperations = ( · 41 unmodified lines
299306 const entry = fieldMap.get(key);
300307 const operationString = entry
301308 ? buildOperationStringForField(entry.kind, entry.field, typeMap)
302− : `${extracted.kind} { ${extracted.fieldName} }`;
309+ : `${extracted.kind} ${operationNameForField(extracted.fieldName)} { ${extracted.fieldName} }`;
303310
304311 const binding = OperationBinding.make({
305312 kind: extracted.kind,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t