Skip to content

Commit d0245de

Browse files
committed
make optionl to optional array schema
1 parent dd25766 commit d0245de

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/graphql.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
TypeNode,
66
} from "graphql";
77

8-
export const isListType = (typ: TypeNode): typ is ListTypeNode =>
9-
typ.kind === "ListType";
10-
export const isNonNullType = (typ: TypeNode): typ is NonNullTypeNode =>
11-
typ.kind === "NonNullType";
12-
export const isNamedType = (typ: TypeNode): typ is NamedTypeNode =>
13-
typ.kind === "NamedType";
8+
export const isListType = (typ?: TypeNode): typ is ListTypeNode =>
9+
typ?.kind === "ListType";
10+
export const isNonNullType = (typ?: TypeNode): typ is NonNullTypeNode =>
11+
typ?.kind === "NonNullType";
12+
export const isNamedType = (typ?: TypeNode): typ is NamedTypeNode =>
13+
typ?.kind === "NamedType";
1414

1515
export const isInput = (kind: string) => kind.includes("Input");

src/yup/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,21 +128,27 @@ const generateInputObjectFieldYupSchema = (
128128
const generateInputObjectFieldTypeYupSchema = (
129129
tsVisitor: TsVisitor,
130130
schema: GraphQLSchema,
131-
type: TypeNode
131+
type: TypeNode,
132+
parentType?: TypeNode
132133
): string => {
133134
if (isListType(type)) {
134135
const gen = generateInputObjectFieldTypeYupSchema(
135136
tsVisitor,
136137
schema,
137-
type.type
138+
type.type,
139+
type,
138140
);
141+
if (!isNonNullType(parentType)) {
142+
return `yup.array().of(${maybeLazy(type.type, gen)}).optional()`;
143+
}
139144
return `yup.array().of(${maybeLazy(type.type, gen)})`;
140145
}
141146
if (isNonNullType(type)) {
142147
const gen = generateInputObjectFieldTypeYupSchema(
143148
tsVisitor,
144149
schema,
145-
type.type
150+
type.type,
151+
type,
146152
);
147153
return maybeLazy(type.type, `${gen}.required()`);
148154
}

test.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ input PageInput {
1616
attributes: [AttributeInput!]
1717
pageType: PageType!
1818
date: Date
19+
postIDs: [ID!]
1920
}
2021

2122
input AttributeInput {

0 commit comments

Comments
 (0)