Skip to content

Commit 863e4ce

Browse files
committed
Add library exports and tests
1 parent 5fecb49 commit 863e4ce

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

__tests__/index.test.mjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// @ts-check
2+
3+
import { test } from "node:test";
4+
import * as assert from "node:assert";
5+
import { semanticToStrict, semanticToNullable } from "../dist/index.js";
6+
import { buildSchema, printSchema } from "graphql";
7+
import { readdir, readFile } from "node:fs/promises";
8+
9+
const TEST_DIR = import.meta.dirname;
10+
const files = await readdir(TEST_DIR);
11+
12+
for (const file of files) {
13+
if (file.endsWith(".test.graphql") && !file.startsWith(".")) {
14+
test(file.replace(/\.test\.graphql$/, ""), async () => {
15+
const sdl = await readFile(TEST_DIR + "/" + file, "utf8");
16+
const schema = buildSchema(sdl);
17+
await test("semantic-to-strict", async () => {
18+
const expectedSdl = await readFile(
19+
TEST_DIR + "/snapshots/" + file.replace(".test.", ".strict."),
20+
"utf8",
21+
);
22+
const converted = semanticToStrict(schema);
23+
assert.equal(
24+
printSchema(converted).trim(),
25+
expectedSdl.trim(),
26+
"Expected semantic-to-strict to match",
27+
);
28+
});
29+
await test("semantic-to-nullable", async () => {
30+
const expectedSdl = await readFile(
31+
TEST_DIR + "/snapshots/" + file.replace(".test.", ".nullable."),
32+
"utf8",
33+
);
34+
const converted = semanticToNullable(schema);
35+
assert.equal(
36+
printSchema(converted).trim(),
37+
expectedSdl.trim(),
38+
"Expected semantic-to-nullable to match",
39+
);
40+
});
41+
});
42+
}
43+
}

__tests__/schema-with-directive.test.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
directive @semanticNonNull(levels: [Int!]) on FIELD_DEFINITION
2+
13
type Query {
24
allThings(includingArchived: Boolean, first: Int!): ThingConnection
35
@semanticNonNull

src/cli.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ export async function main(toStrict = false) {
5050
throw new Error("Invalid schema");
5151
}
5252

53+
const derivedSchema = convertSchema(schema, toStrict);
54+
55+
const newSdl = printSchema(derivedSchema);
56+
await writeFile(output, newSdl + "\n");
57+
}
58+
59+
function convertSchema(schema: GraphQLSchema, toStrict: boolean) {
5360
const config = schema.toConfig();
5461
const convertType = makeConvertType(toStrict);
5562
const derivedSchema = new GraphQLSchema({
@@ -62,10 +69,15 @@ export async function main(toStrict = false) {
6269
.map((t) => convertType(t)),
6370
directives: config.directives.filter((d) => d.name !== "semanticNonNull"),
6471
});
72+
return derivedSchema;
73+
}
6574

66-
const newSdl = printSchema(derivedSchema);
75+
export function semanticToNullable(schema: GraphQLSchema) {
76+
return convertSchema(schema, false);
77+
}
6778

68-
await writeFile(output, newSdl + "\n");
79+
export function semanticToStrict(schema: GraphQLSchema) {
80+
return convertSchema(schema, true);
6981
}
7082

7183
function makeConvertType(toStrict: boolean) {
@@ -75,7 +87,7 @@ function makeConvertType(toStrict: boolean) {
7587
return () => {
7688
return Object.fromEntries(
7789
Object.entries(fields).map(([fieldName, inSpec]) => {
78-
const spec = applySemanticNonNullDirective(inSpec);
90+
const spec = applySemanticNonNullDirectiveToFieldConfig(inSpec);
7991
return [
8092
fieldName,
8193
{
@@ -174,7 +186,7 @@ function makeConvertType(toStrict: boolean) {
174186
*
175187
* @see {@url https://www.apollographql.com/docs/kotlin/advanced/nullability/#semanticnonnull}
176188
*/
177-
function applySemanticNonNullDirective(
189+
export function applySemanticNonNullDirectiveToFieldConfig(
178190
spec: GraphQLFieldConfig<any, any, any>,
179191
): GraphQLFieldConfig<any, any, any> {
180192
const directive = spec.astNode?.directives?.find(

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export {
2+
semanticToNullable,
3+
semanticToStrict,
4+
applySemanticNonNullDirectiveToFieldConfig,
5+
} from "./cli.js";

0 commit comments

Comments
 (0)