Skip to content

Commit f38868f

Browse files
committed
Refactor
1 parent 759bba1 commit f38868f

File tree

1 file changed

+69
-69
lines changed

1 file changed

+69
-69
lines changed

src/cli.ts

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
buildSchema,
3+
GraphQLFieldConfig,
34
GraphQLFieldConfigMap,
4-
GraphQLFieldMap,
55
GraphQLInterfaceType,
66
GraphQLList,
77
GraphQLNamedType,
@@ -17,7 +17,6 @@ import {
1717
validateSchema,
1818
} from "graphql";
1919
import type { Maybe } from "graphql/jsutils/Maybe";
20-
import { ObjMap } from "graphql/jsutils/ObjMap";
2120
import { readFile, writeFile } from "node:fs/promises";
2221
import { parseArgs } from "node:util";
2322

@@ -75,84 +74,20 @@ function makeConvertType(toStrict: boolean) {
7574
function convertFields(fields: GraphQLFieldConfigMap<any, any>) {
7675
return () => {
7776
return Object.fromEntries(
78-
Object.entries(fields).map(([fieldName, spec]) => {
79-
const directive = spec.astNode?.directives?.find(
80-
(d) => d.name.value === "semanticNonNull",
81-
);
82-
const levelsArg = directive?.arguments?.find(
83-
(a) => a.name.value === "levels",
84-
);
85-
const levels =
86-
levelsArg?.value?.kind === Kind.LIST
87-
? levelsArg.value.values
88-
.filter((v) => v.kind === Kind.INT)
89-
.map((v) => Number(v.value))
90-
: [0];
91-
const type = directive
92-
? applySemanticNonNull(spec.type, levels)
93-
: spec.type;
77+
Object.entries(fields).map(([fieldName, inSpec]) => {
78+
const spec = applySemanticNonNullDirective(inSpec);
9479
return [
9580
fieldName,
9681
{
9782
...spec,
98-
type: convertType(type),
99-
astNode: spec.astNode
100-
? {
101-
...spec.astNode,
102-
directives: spec.astNode.directives?.filter(
103-
(d) => d.name.value !== "semanticNonNull",
104-
),
105-
}
106-
: undefined,
83+
type: convertType(spec.type),
10784
},
10885
];
10986
}),
11087
) as any;
11188
};
11289
}
11390

114-
/**
115-
* Takes a GraphQL type along with levels at which to apply
116-
* semantic-non-null, and returns a converted type with these levels applied.
117-
*/
118-
function applySemanticNonNull(type: GraphQLOutputType, levels: number[]) {
119-
function recurse(
120-
type: GraphQLOutputType,
121-
level: number,
122-
): GraphQLOutputType {
123-
if (type instanceof GraphQLSemanticNonNull) {
124-
// Strip semantic-non-null types; this should never happen but if someone
125-
// uses both semantic-non-null and the `@semanticNonNull` directive, we
126-
// want the directive to win (I guess?)
127-
return recurse(type.ofType, level);
128-
} else if (type instanceof GraphQLNonNull) {
129-
const inner = recurse(type.ofType, level);
130-
if (levels.includes(level)) {
131-
// Semantic non-null from `inner` replaces our GrpahQLNonNull wrapper
132-
return inner;
133-
} else {
134-
// Keep non-null wrapper; no semantic-non-null was added to `inner`
135-
return new GraphQLNonNull(inner);
136-
}
137-
} else if (type instanceof GraphQLList) {
138-
const inner = new GraphQLList(recurse(type.ofType, level + 1));
139-
if (levels.includes(level)) {
140-
return new GraphQLSemanticNonNull(inner);
141-
} else {
142-
return inner;
143-
}
144-
} else {
145-
if (levels.includes(level)) {
146-
return new GraphQLSemanticNonNull(type);
147-
} else {
148-
return type;
149-
}
150-
}
151-
}
152-
153-
return recurse(type, 0);
154-
}
155-
15691
function convertTypes(
15792
types: readonly GraphQLInterfaceType[] | null | undefined,
15893
): undefined | (() => readonly GraphQLInterfaceType[]);
@@ -231,3 +166,68 @@ function makeConvertType(toStrict: boolean) {
231166

232167
return convertType;
233168
}
169+
170+
/**
171+
* Takes a GraphQL type along with levels at which to apply
172+
* semantic-non-null, and returns a converted type with these levels applied.
173+
*/
174+
function applySemanticNonNullDirective(
175+
spec: GraphQLFieldConfig<any, any, any>,
176+
): GraphQLFieldConfig<any, any, any> {
177+
const directive = spec.astNode?.directives?.find(
178+
(d) => d.name.value === "semanticNonNull",
179+
);
180+
if (!directive) {
181+
return spec;
182+
}
183+
const levelsArg = directive.arguments?.find((a) => a.name.value === "levels");
184+
const levels =
185+
levelsArg?.value?.kind === Kind.LIST
186+
? levelsArg.value.values
187+
.filter((v) => v.kind === Kind.INT)
188+
.map((v) => Number(v.value))
189+
: [0];
190+
function recurse(type: GraphQLOutputType, level: number): GraphQLOutputType {
191+
if (type instanceof GraphQLSemanticNonNull) {
192+
// Strip semantic-non-null types; this should never happen but if someone
193+
// uses both semantic-non-null and the `@semanticNonNull` directive, we
194+
// want the directive to win (I guess?)
195+
return recurse(type.ofType, level);
196+
} else if (type instanceof GraphQLNonNull) {
197+
const inner = recurse(type.ofType, level);
198+
if (levels.includes(level)) {
199+
// Semantic non-null from `inner` replaces our GrpahQLNonNull wrapper
200+
return inner;
201+
} else {
202+
// Keep non-null wrapper; no semantic-non-null was added to `inner`
203+
return new GraphQLNonNull(inner);
204+
}
205+
} else if (type instanceof GraphQLList) {
206+
const inner = new GraphQLList(recurse(type.ofType, level + 1));
207+
if (levels.includes(level)) {
208+
return new GraphQLSemanticNonNull(inner);
209+
} else {
210+
return inner;
211+
}
212+
} else {
213+
if (levels.includes(level)) {
214+
return new GraphQLSemanticNonNull(type);
215+
} else {
216+
return type;
217+
}
218+
}
219+
}
220+
221+
return {
222+
...spec,
223+
type: recurse(spec.type, 0),
224+
astNode: spec.astNode
225+
? {
226+
...spec.astNode,
227+
directives: spec.astNode.directives?.filter(
228+
(d) => d.name.value !== "semanticNonNull",
229+
),
230+
}
231+
: undefined,
232+
};
233+
}

0 commit comments

Comments
 (0)