Skip to content

Commit 41cac0e

Browse files
committed
Fix logic to match new tests
1 parent f39985c commit 41cac0e

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/index.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import * as graphql from "graphql";
2323
type Maybe<T> = null | undefined | T;
2424

2525
// If GraphQL doesn't have this helper function, then it doesn't natively support GraphQLSemanticNonNull
26-
const isSemanticNonNullType = graphql.isSemanticNonNullType ?? (() => false);
26+
const isSemanticNonNullType =
27+
(graphql as any).isSemanticNonNullType ?? (() => false);
2728

2829
function convertSchema(schema: GraphQLSchema, toStrict: boolean) {
2930
const config = schema.toConfig();
@@ -102,7 +103,9 @@ function makeConvertType(toStrict: boolean) {
102103
return type;
103104
}
104105
if (isSemanticNonNullType(type)) {
105-
const unwrapped = convertType(type.ofType as GraphQLNullableType);
106+
const unwrapped = convertType(
107+
(type as any).ofType as GraphQLNullableType,
108+
);
106109
// Here's where we do our thing!
107110
if (toStrict) {
108111
return new GraphQLNonNull(unwrapped);
@@ -180,16 +183,6 @@ export function convertFieldConfig(
180183
),
181184
};
182185

183-
if (!toStrict) {
184-
// If we're not converting to strict, we can simply strip this directive
185-
return {
186-
...spec,
187-
astNode: filteredAstNode,
188-
};
189-
}
190-
191-
// Otherwise, convert semantic non-null positions to strict non-null
192-
193186
const levelsArg = directive.arguments?.find((a) => a.name.value === "levels");
194187
const levels =
195188
levelsArg?.value?.kind === Kind.LIST
@@ -202,25 +195,27 @@ export function convertFieldConfig(
202195
// Strip semantic-non-null types; this should never happen but if someone
203196
// uses both semantic-non-null and the `@semanticNonNull` directive, we
204197
// want the directive to win (I guess?)
205-
return recurse(type.ofType, level);
198+
return recurse(
199+
(type as any).ofType as GraphQLNullableType & GraphQLOutputType,
200+
level,
201+
);
206202
} else if (isNonNullType(type)) {
207203
const inner = recurse(type.ofType, level);
208-
if (levels.includes(level)) {
209-
// Semantic non-null from `inner` replaces our GraphQLNonNull wrapper
204+
if (isNonNullType(inner)) {
210205
return inner;
211206
} else {
212-
// Keep non-null wrapper; no semantic-non-null was added to `inner`
207+
// Carry the non-null through no matter what semantic says
213208
return new GraphQLNonNull(inner);
214209
}
215210
} else if (isListType(type)) {
216211
const inner = new GraphQLList(recurse(type.ofType, level + 1));
217-
if (levels.includes(level)) {
212+
if (toStrict && levels.includes(level)) {
218213
return new GraphQLNonNull(inner);
219214
} else {
220215
return inner;
221216
}
222217
} else {
223-
if (levels.includes(level)) {
218+
if (toStrict && levels.includes(level)) {
224219
return new GraphQLNonNull(type);
225220
} else {
226221
return type;

0 commit comments

Comments
 (0)