forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
@schema
decorator to mark namespaces as GraphQL schemas
Using the `TypeSpec.GraphQL.@schema` decorator on a namespace indicates that the decorated namespace represents a GraphQL schema that should be generated by the GraphQL emitter. Because this allows for multiple schemas to be specified in a TypeSpec source, our test host is reworked to provide a `GraphQLSchemaRecord` corresponding to each schema produced. This commit does not actually implement any emitter functionality, but populates a state map that will be used by the emitter in the future.
- Loading branch information
Showing
15 changed files
with
301 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import "./schema.tsp"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import "../dist/src/lib/schema.js"; | ||
|
||
using TypeSpec.Reflection; | ||
|
||
namespace TypeSpec.GraphQL; | ||
|
||
namespace Schema { | ||
model SchemaOptions { | ||
name?: string; | ||
} | ||
} | ||
|
||
/** | ||
* Mark this namespace as describing a GraphQL schema and configure schema properties. | ||
* | ||
* @example | ||
* | ||
* ```typespec | ||
* @schema(name: "MySchema") | ||
* namespace MySchema {}; | ||
* ``` | ||
*/ | ||
extern dec schema(target: Namespace, options?: Schema.SchemaOptions); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { $onEmit } from "./emitter.js"; | ||
export { $lib } from "./lib.js"; | ||
export { $decorators } from "./tsp-index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
type DecoratorContext, | ||
type DecoratorFunction, | ||
type Namespace, | ||
type Program, | ||
type Type, | ||
getTypeName, | ||
validateDecoratorUniqueOnNode, | ||
} from "@typespec/compiler"; | ||
|
||
import { createStateSymbol, NAMESPACE, reportDiagnostic } from "../lib.js"; | ||
import { useStateMap } from "./state-map.js"; | ||
|
||
// This will set the namespace for decorators implemented in this file | ||
export const namespace = NAMESPACE; | ||
|
||
export interface SchemaDetails { | ||
name?: string; | ||
} | ||
|
||
export interface Schema extends SchemaDetails { | ||
type: Namespace; | ||
} | ||
|
||
const [getSchema, setSchema, getSchemaMap] = useStateMap<Namespace, Schema>( | ||
createStateSymbol("schemas"), | ||
); | ||
|
||
/** | ||
* List all the schemas defined in the TypeSpec program | ||
* @param program Program | ||
* @returns List of schemas. | ||
*/ | ||
export function listSchemas(program: Program): Schema[] { | ||
return [...getSchemaMap(program).values()]; | ||
} | ||
|
||
export { | ||
/** | ||
* Get the schema information for the given namespace. | ||
* @param program Program | ||
* @param namespace Schema namespace | ||
* @returns Schema information or undefined if namespace is not a schema namespace. | ||
*/ | ||
getSchema, | ||
}; | ||
|
||
/** | ||
* Check if the namespace is defined as a schema. | ||
* @param program Program | ||
* @param namespace Namespace | ||
* @returns Boolean | ||
*/ | ||
export function isSchema(program: Program, namespace: Namespace): boolean { | ||
return getSchemaMap(program).has(namespace); | ||
} | ||
|
||
/** | ||
* Mark the given namespace as a schema. | ||
* @param program Program | ||
* @param namespace Namespace | ||
* @param details Schema details | ||
*/ | ||
export function addSchema( | ||
program: Program, | ||
namespace: Namespace, | ||
details: SchemaDetails = {}, | ||
): void { | ||
const schemaMap = getSchemaMap(program); | ||
const existing = schemaMap.get(namespace) ?? {}; | ||
setSchema(program, namespace, { ...existing, ...details, type: namespace }); | ||
} | ||
|
||
export const $schema: DecoratorFunction = ( | ||
context: DecoratorContext, | ||
target: Namespace, | ||
options: Type, | ||
) => { | ||
validateDecoratorUniqueOnNode(context, target, $schema); | ||
|
||
if (options && options.kind !== "Model") { | ||
reportDiagnostic(context.program, { | ||
code: "invalid-argument", | ||
format: { value: options.kind, expected: "Model" }, | ||
target: context.getArgumentTarget(0)!, | ||
}); | ||
return; | ||
} | ||
|
||
const schemaDetails: SchemaDetails = {}; | ||
const name = options?.properties.get("name")?.type; | ||
if (name) { | ||
if (name.kind === "String") { | ||
schemaDetails.name = name.value; | ||
} else { | ||
reportDiagnostic(context.program, { | ||
code: "unassignable", | ||
format: { sourceType: getTypeName(name), targetType: "String" }, | ||
target: context.getArgumentTarget(0)!, | ||
}); | ||
} | ||
} | ||
|
||
addSchema(context.program, target, schemaDetails); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Type } from "@typespec/compiler"; | ||
import { unsafe_useStateMap, unsafe_useStateSet } from "@typespec/compiler/experimental"; | ||
|
||
/** | ||
* This is a copy of the experimental state-accessor lib from @typespec/compiler | ||
*/ | ||
|
||
function createStateSymbol(name: string) { | ||
return Symbol.for(`TypeSpec.${name}`); | ||
} | ||
|
||
export function useStateMap<K extends Type, V>(key: string | symbol) { | ||
return unsafe_useStateMap<K, V>(typeof key === "string" ? createStateSymbol(key) : key); | ||
} | ||
|
||
export function useStateSet<K extends Type>(key: string | symbol) { | ||
return unsafe_useStateSet<K>(typeof key === "string" ? createStateSymbol(key) : key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { GraphQLSchema } from "graphql"; | ||
|
||
export const EMPTY_SCHEMA = new GraphQLSchema({}); | ||
|
||
export const EMPTY_SCHEMA_OUTPUT = `#graphql | ||
type Query { | ||
""" | ||
A placeholder field. If you are seeing this, it means no operations were defined that could be emitted. | ||
""" | ||
_: Boolean | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { DecoratorImplementations } from "@typespec/compiler"; | ||
import { NAMESPACE } from "./lib.js"; | ||
import { $schema } from "./lib/schema.js"; | ||
|
||
export const $decorators: DecoratorImplementations = { | ||
[NAMESPACE]: { | ||
schema: $schema, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Diagnostic } from "@typespec/compiler"; | ||
import type { GraphQLSchema } from "graphql"; | ||
import type { Schema } from "./lib/schema.ts"; | ||
|
||
/** | ||
* A record containing the GraphQL schema corresponding to | ||
* a particular schema definition. | ||
*/ | ||
export interface GraphQLSchemaRecord { | ||
/** The declared schema that generated this GraphQL schema */ | ||
readonly schema: Schema; | ||
|
||
/** The GraphQLSchema */ | ||
readonly graphQLSchema: GraphQLSchema; | ||
|
||
/** The diagnostics created for this schema */ | ||
readonly diagnostics: readonly Diagnostic[]; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { Namespace } from "@typespec/compiler"; | ||
import { expectDiagnosticEmpty } from "@typespec/compiler/testing"; | ||
import { describe, expect, it } from "vitest"; | ||
import { getSchema } from "../src/lib/schema.js"; | ||
import { compileAndDiagnose } from "./test-host.js"; | ||
|
||
describe("@schema", () => { | ||
it("Creates a schema with no name", async () => { | ||
const [program, { TestNamespace }, diagnostics] = await compileAndDiagnose<{ | ||
TestNamespace: Namespace; | ||
}>(` | ||
@schema | ||
@test namespace TestNamespace {} | ||
`); | ||
expectDiagnosticEmpty(diagnostics); | ||
|
||
const schema = getSchema(program, TestNamespace); | ||
|
||
expect(schema?.type).toBe(TestNamespace); | ||
expect(schema?.name).toBeUndefined(); | ||
}); | ||
|
||
it("Creates a schema with a specified name", async () => { | ||
const [program, { TestNamespace }, diagnostics] = await compileAndDiagnose<{ | ||
TestNamespace: Namespace; | ||
}>(` | ||
@schema({name: "MySchema"}) | ||
@test namespace TestNamespace {} | ||
`); | ||
expectDiagnosticEmpty(diagnostics); | ||
|
||
const schema = getSchema(program, TestNamespace); | ||
|
||
expect(schema?.type).toBe(TestNamespace); | ||
expect(schema?.name).toBe("MySchema"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.