Skip to content

Commit 180d92e

Browse files
committed
Schema conversion
1 parent abab757 commit 180d92e

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

src/cli.ts

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import {
22
buildSchema,
3+
GraphQLFieldConfigMap,
4+
GraphQLFieldMap,
35
GraphQLInterfaceType,
6+
GraphQLList,
47
GraphQLNamedType,
8+
GraphQLNonNull,
59
GraphQLObjectType,
610
GraphQLSchema,
11+
GraphQLSemanticNonNull,
12+
GraphQLType,
13+
GraphQLUnionType,
714
printSchema,
815
validateSchema,
916
} from "graphql";
1017
import type { Maybe } from "graphql/jsutils/Maybe";
18+
import { ObjMap } from "graphql/jsutils/ObjMap";
1119
import { readFile, writeFile } from "node:fs/promises";
1220
import { parseArgs } from "node:util";
1321

@@ -48,6 +56,9 @@ export async function main(toStrict = false) {
4856
query: convertType(config.query),
4957
mutation: convertType(config.mutation),
5058
subscription: convertType(config.subscription),
59+
types: config.types
60+
.filter((t) => !t.name.startsWith("__"))
61+
.map((t) => convertType(t)),
5162
});
5263

5364
const newSdl = printSchema(derivedSchema);
@@ -58,14 +69,64 @@ export async function main(toStrict = false) {
5869
function makeConvertType(toStrict: boolean) {
5970
const cache = new Map<string, GraphQLNamedType>();
6071

72+
function convertFields(fields: GraphQLFieldConfigMap<any, any>) {
73+
return () => {
74+
return Object.fromEntries(
75+
Object.entries(fields).map(([fieldName, spec]) => [
76+
fieldName,
77+
{
78+
...spec,
79+
type: convertType(spec.type),
80+
},
81+
]),
82+
) as any;
83+
};
84+
}
85+
86+
function convertTypes(
87+
types: readonly GraphQLInterfaceType[] | null | undefined,
88+
): undefined | (() => readonly GraphQLInterfaceType[]);
89+
function convertTypes(
90+
types: readonly GraphQLObjectType[],
91+
): () => readonly GraphQLObjectType[];
92+
function convertTypes(
93+
types: readonly GraphQLNamedType[],
94+
): undefined | (() => readonly GraphQLNamedType[]);
95+
function convertTypes(
96+
types: readonly GraphQLNamedType[] | undefined,
97+
): undefined | (() => readonly GraphQLNamedType[]);
98+
function convertTypes(
99+
types: readonly GraphQLNamedType[] | null | undefined,
100+
): undefined | (() => readonly GraphQLNamedType[]) {
101+
if (!types) return undefined;
102+
return () => types.map((t) => convertType(t));
103+
}
104+
61105
function convertType(type: null | undefined): null | undefined;
62106
function convertType(type: GraphQLObjectType): GraphQLObjectType;
63107
function convertType(
64108
type: Maybe<GraphQLObjectType>,
65109
): Maybe<GraphQLObjectType>;
66110
function convertType(type: GraphQLNamedType): GraphQLNamedType;
67-
function convertType(type: GraphQLNamedType | null | undefined) {
111+
function convertType(type: GraphQLType): GraphQLType;
112+
function convertType(type: GraphQLType | null | undefined) {
68113
if (!type) return type;
114+
if (type instanceof GraphQLSemanticNonNull) {
115+
const unwrapped = convertType(type.ofType);
116+
// Here's where we do our thing!
117+
if (toStrict) {
118+
return new GraphQLNonNull(unwrapped);
119+
} else {
120+
return unwrapped;
121+
}
122+
} else if (type instanceof GraphQLNonNull) {
123+
return new GraphQLNonNull(convertType(type.ofType));
124+
} else if (type instanceof GraphQLList) {
125+
return new GraphQLList(convertType(type.ofType));
126+
}
127+
if (type.name.startsWith("__")) {
128+
return null;
129+
}
69130
if (cache.has(type.name)) {
70131
return cache.get(type.name);
71132
}
@@ -74,11 +135,21 @@ function makeConvertType(toStrict: boolean) {
74135
const config = type.toConfig();
75136
return new GraphQLObjectType({
76137
...config,
138+
fields: convertFields(config.fields),
139+
interfaces: convertTypes(config.interfaces),
77140
});
78141
} else if (type instanceof GraphQLInterfaceType) {
79142
const config = type.toConfig();
80143
return new GraphQLInterfaceType({
81144
...config,
145+
fields: convertFields(config.fields),
146+
interfaces: convertTypes(config.interfaces),
147+
});
148+
} else if (type instanceof GraphQLUnionType) {
149+
const config = type.toConfig();
150+
return new GraphQLUnionType({
151+
...config,
152+
types: convertTypes(config.types),
82153
});
83154
} else {
84155
return type;

0 commit comments

Comments
 (0)