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
14 changed files
with
256 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?: valueof 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,77 @@ | ||
import { | ||
type DecoratorContext, | ||
type DecoratorFunction, | ||
type Namespace, | ||
type Program, | ||
validateDecoratorUniqueOnNode, | ||
} from "@typespec/compiler"; | ||
|
||
import { GraphQLKeys, NAMESPACE } 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>(GraphQLKeys.schema); | ||
|
||
/** | ||
* 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: SchemaDetails = {}, | ||
) => { | ||
validateDecoratorUniqueOnNode(context, target, $schema); | ||
addSchema(context.program, target, options); | ||
}; |
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,10 @@ | ||
import type { Type } from "@typespec/compiler"; | ||
import { unsafe_useStateMap, unsafe_useStateSet } from "@typespec/compiler/experimental"; | ||
|
||
export function useStateMap<K extends Type, V>(key: symbol) { | ||
return unsafe_useStateMap<K, V>(key); | ||
} | ||
|
||
export function useStateSet<K extends Type>(key: symbol) { | ||
return unsafe_useStateSet<K>(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,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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.