Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/named-graphql-operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"executor": patch
---

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.
23 changes: 23 additions & 0 deletions packages/plugins/graphql/src/sdk/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,29 @@ describe("graphqlPlugin real protocol server", () => {
}),
);

it.effect("sends named operations derived from the field name", () =>
Effect.gen(function* () {
const server = yield* serveGreetingServer;
const executor = yield* createExecutor(
makeTestConfig({ plugins: [graphqlPlugin()] as const }),
);

yield* executor.graphql.addSource({
endpoint: server.endpoint,
scope: TEST_SCOPE,
namespace: "named_ops",
});
yield* server.clearRequests;

yield* executor.tools.invoke("named_ops.query.hello", { name: "Ada" });
yield* executor.tools.invoke("named_ops.mutation.setGreeting", { message: "hi" });

const requests = yield* server.requests;
expect(requests[0]?.payload.query).toMatch(/^query Hello\b/);
expect(requests[1]?.payload.query).toMatch(/^mutation SetGreeting\b/);
}),
);

it.effect("surfaces non-2xx invocation responses as ToolResult.fail", () =>
Effect.gen(function* () {
const server = yield* serveTestHttpApp((request) =>
Expand Down
11 changes: 9 additions & 2 deletions packages/plugins/graphql/src/sdk/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,19 @@ const buildSelectionSet = (
return subFields.length > 0 ? `{ ${subFields.join(" ")} }` : "";
};

// Name every generated operation: some servers reject anonymous operations, and
// APM tooling keys traces off the operation name. Field names are already valid
// GraphQL name tokens, so the upper-cased field name is a safe operation name.
const operationNameForField = (fieldName: string): string =>
fieldName.charAt(0).toUpperCase() + fieldName.slice(1);

const buildOperationStringForField = (
kind: GraphqlOperationKind,
field: IntrospectionField,
types: ReadonlyMap<string, IntrospectionType>,
): string => {
const opType = kind === "query" ? "query" : "mutation";
const opName = operationNameForField(field.name);

const varDefs = field.args.map((arg) => {
const typeName = formatTypeRef(arg.type);
Expand All @@ -335,7 +342,7 @@ const buildOperationStringForField = (
const varDefsStr = varDefs.length > 0 ? `(${varDefs.join(", ")})` : "";
const argPassStr = argPasses.length > 0 ? `(${argPasses.join(", ")})` : "";

return `${opType}${varDefsStr} { ${field.name}${argPassStr}${selectionSet ? ` ${selectionSet}` : ""} }`;
return `${opType} ${opName}${varDefsStr} { ${field.name}${argPassStr}${selectionSet ? ` ${selectionSet}` : ""} }`;
};

interface PreparedOperation {
Expand Down Expand Up @@ -380,7 +387,7 @@ const prepareOperations = (
const entry = fieldMap.get(key);
const operationString = entry
? buildOperationStringForField(entry.kind, entry.field, typeMap)
: `${extracted.kind} { ${extracted.fieldName} }`;
: `${extracted.kind} ${operationNameForField(extracted.fieldName)} { ${extracted.fieldName} }`;

const binding = OperationBinding.make({
kind: extracted.kind,
Expand Down