1- import { isInput , isNonNullType , isListType , isNamedType } from './../graphql' ;
1+ import { isInput , isNonNullType , isListType , isNamedType , ObjectTypeDefinitionBuilder } from './../graphql' ;
22import { ValidationSchemaPluginConfig } from '../config' ;
33import {
44 InputValueDefinitionNode ,
@@ -41,8 +41,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
4141 . withName ( `${ name } Schema(): yup.SchemaOf<${ name } >` )
4242 . withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
4343 } ,
44- ObjectTypeDefinition : ( node : ObjectTypeDefinitionNode ) => {
45- if ( ! config . useObjectTypes ) return ;
44+ ObjectTypeDefinition : ObjectTypeDefinitionBuilder ( config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
4645 const name = tsVisitor . convertName ( node . name . value ) ;
4746 importTypes . push ( name ) ;
4847
@@ -55,11 +54,12 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
5554 . withBlock (
5655 [
5756 indent ( `return yup.object({` ) ,
58- ` __typename: yup.mixed().oneOf(['${ node . name . value } ', undefined]),\n${ shape } ` ,
57+ indent ( `__typename: yup.mixed().oneOf(['${ node . name . value } ', undefined]),` , 2 ) ,
58+ shape ,
5959 indent ( '})' ) ,
6060 ] . join ( '\n' )
6161 ) . string ;
62- } ,
62+ } ) ,
6363 EnumTypeDefinition : ( node : EnumTypeDefinitionNode ) => {
6464 const enumname = tsVisitor . convertName ( node . name . value ) ;
6565 importTypes . push ( enumname ) ;
@@ -146,7 +146,12 @@ const generateFieldTypeYupSchema = (
146146 return maybeLazy ( type . type , nonNullGen ) ;
147147 }
148148 if ( isNamedType ( type ) ) {
149- return generateNameNodeYupSchema ( config , tsVisitor , schema , type . name ) ;
149+ const gen = generateNameNodeYupSchema ( config , tsVisitor , schema , type . name ) ;
150+ const typ = schema . getType ( type . name . value ) ;
151+ if ( typ ?. astNode ?. kind === 'ObjectTypeDefinition' ) {
152+ return `${ gen } .optional()` ;
153+ }
154+ return gen ;
150155 }
151156 console . warn ( 'unhandled type:' , type ) ;
152157 return '' ;
@@ -160,12 +165,17 @@ const generateNameNodeYupSchema = (
160165) : string => {
161166 const typ = schema . getType ( node . value ) ;
162167
163- if ( typ && typ . astNode ?. kind === 'InputObjectTypeDefinition' ) {
168+ if ( typ ?. astNode ?. kind === 'InputObjectTypeDefinition' ) {
169+ const enumName = tsVisitor . convertName ( typ . astNode . name . value ) ;
170+ return `${ enumName } Schema()` ;
171+ }
172+
173+ if ( typ ?. astNode ?. kind === 'ObjectTypeDefinition' ) {
164174 const enumName = tsVisitor . convertName ( typ . astNode . name . value ) ;
165175 return `${ enumName } Schema()` ;
166176 }
167177
168- if ( typ && typ . astNode ?. kind === 'EnumTypeDefinition' ) {
178+ if ( typ ? .astNode ?. kind === 'EnumTypeDefinition' ) {
169179 const enumName = tsVisitor . convertName ( typ . astNode . name . value ) ;
170180 return `${ enumName } Schema` ;
171181 }
0 commit comments