Skip to content
Merged
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
11 changes: 7 additions & 4 deletions packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@
"node": ">=20.0.0"
},
"dependencies": {
"@alloy-js/core": "^0.11.0",
"@alloy-js/typescript": "^0.11.0",
"@alloy-js/core": "^0.22.0",
"@alloy-js/graphql": "link:../../alloy/packages/graphql",
"@alloy-js/typescript": "^0.22.0",
"change-case": "^5.4.4",
"graphql": "^16.9.0"
},
"scripts": {
"clean": "rimraf ./dist ./temp",
"build": "tsc -p .",
"watch": "tsc --watch",
"build": "alloy build",
"watch": "alloy build --watch",
"test": "vitest run",
"test:watch": "vitest -w",
"lint": "eslint . --max-warnings=0",
Expand All @@ -56,6 +57,8 @@
"@typespec/mutator-framework": "workspace:~"
},
"devDependencies": {
"@alloy-js/cli": "^0.22.0",
"@alloy-js/rollup-plugin": "^0.1.0",
"@types/node": "~22.13.13",
"@typespec/compiler": "workspace:~",
"@typespec/emitter-framework": "workspace:~",
Expand Down
72 changes: 72 additions & 0 deletions packages/graphql/src/components/fields/field.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { type ModelProperty, getDeprecationDetails, isArrayModelType } from "@typespec/compiler";
import * as gql from "@alloy-js/graphql";
import { useTsp } from "@typespec/emitter-framework";
import { resolveGraphQLTypeName } from "../../lib/graphql-type-name.js";
import { hasNullableElements, isNullable } from "../../lib/nullable.js";

export interface FieldProps {
property: ModelProperty;
isInput: boolean;
}

export function Field(props: FieldProps) {
const { $, program } = useTsp();

const doc = $.type.getDoc(props.property);
const deprecation = getDeprecationDetails(program, props.property);
const nullable = isNullable(props.property) || props.property.optional;
const type = props.property.type;

if (type.kind === "Model" && isArrayModelType(type)) {
const elemNullable = hasNullableElements(props.property);
const typeName = resolveGraphQLTypeName(type.indexer.value);

if (props.isInput) {
return (
<gql.InputField
name={props.property.name}
type={typeName}
nonNull={!elemNullable}
description={doc}
deprecated={deprecation?.message}
>
<gql.InputField.List nonNull={!nullable} />
</gql.InputField>
);
}

return (
<gql.Field
name={props.property.name}
type={typeName}
nonNull={!elemNullable}
description={doc}
deprecated={deprecation?.message}
>
<gql.Field.List nonNull={!nullable} />
</gql.Field>
);
}

if (props.isInput) {
return (
<gql.InputField
name={props.property.name}
type={resolveGraphQLTypeName(type)}
nonNull={!nullable}
description={doc}
deprecated={deprecation?.message}
/>
);
}

return (
<gql.Field
name={props.property.name}
type={resolveGraphQLTypeName(type)}
nonNull={!nullable}
description={doc}
deprecated={deprecation?.message}
/>
);
}
2 changes: 2 additions & 0 deletions packages/graphql/src/components/fields/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Field, type FieldProps } from "./field.js";
export { OperationField, type OperationFieldProps } from "./operation-field.js";
58 changes: 58 additions & 0 deletions packages/graphql/src/components/fields/operation-field.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { type Operation, getDeprecationDetails, isArrayModelType } from "@typespec/compiler";
import * as gql from "@alloy-js/graphql";
import { useTsp } from "@typespec/emitter-framework";
import { resolveGraphQLTypeName } from "../../lib/graphql-type-name.js";
import { hasNullableElements, isNullable } from "../../lib/nullable.js";

export interface OperationFieldProps {
operation: Operation;
}

export function OperationField(props: OperationFieldProps) {
const { $, program } = useTsp();

const doc = $.type.getDoc(props.operation);
const deprecation = getDeprecationDetails(program, props.operation);
const returnType = props.operation.returnType;
const nullable = isNullable(props.operation);
const params = Array.from(props.operation.parameters.properties.values());

const isList = returnType.kind === "Model" && isArrayModelType(returnType);
const typeName = isList
? resolveGraphQLTypeName(returnType.indexer.value)
: resolveGraphQLTypeName(returnType);
const elemNullable = isList && hasNullableElements(props.operation);

return (
<gql.Field
name={props.operation.name}
type={typeName}
nonNull={isList ? !elemNullable : !nullable}
description={doc}
deprecated={deprecation?.message}
>
{isList ? <gql.Field.List nonNull={!nullable} /> : undefined}
{params.map((param) => {
const paramNullable = isNullable(param) || param.optional;
const paramType = param.type;
const paramIsList = paramType.kind === "Model" && isArrayModelType(paramType);
const paramElemNullable = paramIsList && hasNullableElements(param);
const paramTypeName = paramIsList
? resolveGraphQLTypeName(paramType.indexer.value)
: resolveGraphQLTypeName(paramType);

return (
<gql.InputValue
name={param.name}
type={paramTypeName}
nonNull={paramIsList ? !paramElemNullable : !paramNullable}
description={$.type.getDoc(param)}
deprecated={getDeprecationDetails(program, param)?.message}
>
{paramIsList ? <gql.InputValue.List nonNull={!paramNullable} /> : undefined}
</gql.InputValue>
);
})}
</gql.Field>
);
}
25 changes: 25 additions & 0 deletions packages/graphql/src/components/types/enum-type.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type Enum, getDoc, getDeprecationDetails } from "@typespec/compiler";
import * as gql from "@alloy-js/graphql";
import { useTsp } from "@typespec/emitter-framework";

export interface EnumTypeProps {
type: Enum;
}

export function EnumType(props: EnumTypeProps) {
const { program } = useTsp();
const doc = getDoc(program, props.type);
const members = [...props.type.members.values()];

return (
<gql.EnumType name={props.type.name} description={doc}>
{members.map((member) => (
<gql.EnumValue
name={member.name}
description={getDoc(program, member)}
deprecated={getDeprecationDetails(program, member)?.message}
/>
))}
</gql.EnumType>
);
}
3 changes: 3 additions & 0 deletions packages/graphql/src/components/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { EnumType, type EnumTypeProps } from "./enum-type.js";
export { ScalarType, type ScalarTypeProps } from "./scalar-type.js";
export { UnionType, type UnionTypeProps, type GraphQLUnion } from "./union-type.js";
21 changes: 21 additions & 0 deletions packages/graphql/src/components/types/scalar-type.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type Scalar, getDoc } from "@typespec/compiler";
import * as gql from "@alloy-js/graphql";
import { useTsp } from "@typespec/emitter-framework";

export interface ScalarTypeProps {
type: Scalar;
specificationUrl?: string;
}

export function ScalarType(props: ScalarTypeProps) {
const { program } = useTsp();
const doc = getDoc(program, props.type);

return (
<gql.ScalarType
name={props.type.name}
description={doc}
specifiedByUrl={props.specificationUrl}
/>
);
}
24 changes: 24 additions & 0 deletions packages/graphql/src/components/types/union-type.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { type Model, type Union, getDoc } from "@typespec/compiler";
import * as gql from "@alloy-js/graphql";
import { useTsp } from "@typespec/emitter-framework";

/**
* A Union guaranteed to have a name after mutation.
* The mutation engine ensures this: anonymous unions get derived names.
*/
export interface GraphQLUnion extends Union {
name: string;
}

export interface UnionTypeProps {
type: GraphQLUnion;
}

export function UnionType(props: UnionTypeProps) {
const { program } = useTsp();
const doc = getDoc(program, props.type);
const variants = [...props.type.variants.values()];
const members = variants.map((v) => (v.type as Model).name);

return <gql.UnionType name={props.type.name} description={doc} members={members} />;
}
21 changes: 21 additions & 0 deletions packages/graphql/src/context/graphql-schema-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type ComponentContext, createNamedContext, useContext } from "@alloy-js/core";
import type { TypeGraph } from "../mutation-engine/type-graph.js";

export interface GraphQLSchemaContextValue {
typeGraph: TypeGraph;
}

export const GraphQLSchemaContext: ComponentContext<GraphQLSchemaContextValue> =
createNamedContext<GraphQLSchemaContextValue>("GraphQLSchema");

export function useGraphQLSchema(): GraphQLSchemaContextValue {
const context = useContext(GraphQLSchemaContext);

if (!context) {
throw new Error(
"useGraphQLSchema must be used within GraphQLSchemaContext.Provider.",
);
}

return context;
}
5 changes: 5 additions & 0 deletions packages/graphql/src/context/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export {
GraphQLSchemaContext,
useGraphQLSchema,
type GraphQLSchemaContextValue,
} from "./graphql-schema-context.js";
78 changes: 49 additions & 29 deletions packages/graphql/src/emitter.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
import type { EmitContext, NewLine } from "@typespec/compiler";
import { resolvePath } from "@typespec/compiler";
import { createGraphQLEmitter } from "./graphql-emitter.js";
import type { GraphQLEmitterOptions } from "./lib.js";

const defaultOptions = {
"new-line": "lf",
"omit-unreachable-types": false,
strict: false,
} as const;
import { type EmitContext, type Namespace } from "@typespec/compiler";
import { type GraphQLEmitterOptions, reportDiagnostic } from "./lib.js";
import { listSchemas } from "./lib/schema.js";
import { createGraphQLMutationEngine } from "./mutation-engine/index.js";
import { mutateSchema } from "./mutation-engine/schema-mutator.js";
import type { TypeGraph } from "./mutation-engine/type-graph.js";
import { resolveTypeUsage } from "./type-usage.js";

/**
* Main emitter entry point for GraphQL SDL generation.
*
* Pipeline: type-usage → mutation → buildTypeGraph → render.
* Rendering is a stub in this PR — will be implemented when the Schema
* orchestrator component is added.
*/
export async function $onEmit(context: EmitContext<GraphQLEmitterOptions>) {
const options = resolveOptions(context);
const emitter = createGraphQLEmitter(context, options);
await emitter.emitGraphQL();
}
const schemas = listSchemas(context.program);
if (schemas.length === 0) {
schemas.push({ type: context.program.getGlobalNamespaceType() });
}

export interface ResolvedGraphQLEmitterOptions {
outputFile: string;
newLine: NewLine;
omitUnreachableTypes: boolean;
strict: boolean;
for (const schema of schemas) {
const typeGraph = emitSchema(context, schema.type);
if (typeGraph) {
renderSchema(typeGraph, schema.name);
}
}
}

export function resolveOptions(
function emitSchema(
context: EmitContext<GraphQLEmitterOptions>,
): ResolvedGraphQLEmitterOptions {
const resolvedOptions = { ...defaultOptions, ...context.options };
const outputFile = resolvedOptions["output-file"] ?? "{schema-name}.graphql";
schema: Namespace,
): TypeGraph | undefined {
const program = context.program;
const omitUnreachable = context.options["omit-unreachable-types"] ?? false;

const typeUsage = resolveTypeUsage(schema, omitUnreachable);
const engine = createGraphQLMutationEngine(program);
const typeGraph = mutateSchema(program, engine, schema, typeUsage);

if (typeGraph.globalNamespace.operations.size === 0) {
reportDiagnostic(program, {
code: "empty-schema",
target: schema,
});
return undefined;
}

return typeGraph;
}

return {
outputFile: resolvePath(context.emitterOutputDir, outputFile),
newLine: resolvedOptions["new-line"],
omitUnreachableTypes: resolvedOptions["omit-unreachable-types"],
strict: resolvedOptions["strict"],
};
function renderSchema(_typeGraph: TypeGraph, _schemaName?: string): void {
// Stub — will be replaced with Alloy component rendering:
// <GraphQLSchemaContext.Provider value={{ typeGraph }}>
// <Schema />
// </GraphQLSchemaContext.Provider>
}
Loading