Skip to content

Commit 7628d99

Browse files
committed
Adapt components to TypeGraph context
- ScalarType: take specificationUrl as prop instead of from context - component-test-utils: use minimal TypeGraph context - scalar-type.test: pass specificationUrl as prop
1 parent 390fba6 commit 7628d99

3 files changed

Lines changed: 8 additions & 25 deletions

File tree

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
import { type Scalar, getDoc } from "@typespec/compiler";
22
import * as gql from "@alloy-js/graphql";
33
import { useTsp } from "@typespec/emitter-framework";
4-
import { useGraphQLSchema } from "../../context/index.js";
54

65
export interface ScalarTypeProps {
76
/** The scalar type to render */
87
type: Scalar;
8+
/** Optional @specifiedBy URL for the scalar */
9+
specificationUrl?: string;
910
}
1011

1112
/**
1213
* Renders a GraphQL scalar type declaration with optional @specifiedBy directive
1314
*/
1415
export function ScalarType(props: ScalarTypeProps) {
1516
const { program } = useTsp();
16-
const { scalarSpecifications } = useGraphQLSchema();
1717
const doc = getDoc(program, props.type);
18-
const specificationUrl = scalarSpecifications.get(props.type.name);
1918

2019
return (
2120
<gql.ScalarType
2221
name={props.type.name}
2322
description={doc}
24-
specifiedByUrl={specificationUrl}
23+
specifiedByUrl={props.specificationUrl}
2524
/>
2625
);
2726
}

packages/graphql/test/components/component-test-utils.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,9 @@ export function renderComponentToSDL(
1919
contextOverrides?: Partial<GraphQLSchemaContextValue>,
2020
): string {
2121
const contextValue: GraphQLSchemaContextValue = {
22-
classifiedTypes: {
23-
interfaces: [],
24-
outputModels: [],
25-
inputModels: [],
26-
enums: [],
27-
scalars: [],
28-
scalarVariants: [],
29-
unions: [],
30-
queries: [],
31-
mutations: [],
32-
subscriptions: [],
22+
typeGraph: {
23+
globalNamespace: program.getGlobalNamespaceType(),
3324
},
34-
modelVariants: { outputModels: new Map(), inputModels: new Map() },
35-
scalarSpecifications: new Map(),
3625
...contextOverrides,
3726
};
3827

packages/graphql/test/components/scalar-type.test.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("ScalarType component", () => {
5454
`);
5555
});
5656

57-
it("renders a scalar with @specifiedBy from context", async () => {
57+
it("renders a scalar with @specifiedBy from prop", async () => {
5858
const { MyScalar } = await tester.compile(
5959
t.code`
6060
@specifiedBy("https://example.com/spec")
@@ -65,17 +65,12 @@ describe("ScalarType component", () => {
6565
const engine = createGraphQLMutationEngine(tester.program);
6666
const mutation = engine.mutateScalar(MyScalar);
6767

68-
// Build scalarSpecifications map like the emitter does
68+
// Get spec URL like the emitter does
6969
const specUrl = getSpecifiedBy(tester.program, mutation.mutatedType);
70-
const scalarSpecifications = new Map<string, string>();
71-
if (specUrl) {
72-
scalarSpecifications.set(mutation.mutatedType.name, specUrl);
73-
}
7470

7571
const sdl = renderComponentToSDL(
7672
tester.program,
77-
<ScalarType type={mutation.mutatedType} />,
78-
{ scalarSpecifications },
73+
<ScalarType type={mutation.mutatedType} specificationUrl={specUrl} />,
7974
);
8075

8176
expect(sdl).toMatchInlineSnapshot(`

0 commit comments

Comments
 (0)