@@ -11,6 +11,7 @@ import {
1111 GraphQLNamedType ,
1212 isNonNullType ,
1313 isListType ,
14+ isWrappingType ,
1415 GraphQLFieldMap ,
1516 GraphQLEnumType ,
1617 GraphQLType ,
@@ -58,6 +59,10 @@ function renderMainMethod(
5859 constructor({ endpoint, secret, fragmentReplacements, debug }: BaseGraphcoolOptions) {
5960 super({ typeDefs, endpoint, secret, fragmentReplacements, debug });
6061 }
62+
63+ exists = {
64+ ${ renderExistsFields ( queryType . getFields ( ) ) }
65+ }
6166
6267 query: Query = {
6368${ renderMainMethodFields ( 'query' , queryType . getFields ( ) ) }
@@ -73,6 +78,27 @@ ${renderMainMethodFields('mutation', mutationType.getFields())}
7378}`
7479}
7580
81+ export function renderExistsFields ( fields : GraphQLFieldMap < any , any > ) : string {
82+ return Object . keys ( fields )
83+ . map ( f => {
84+ const field = fields [ f ]
85+ let type = field . type
86+ let foundList = false
87+ // Traverse the wrapping types (if any)
88+ while ( isWrappingType ( type ) ) {
89+ type = type . ofType
90+ // One of those wrappings need to be a GraphQLList for this field to qualify
91+ foundList = foundList || isListType ( type )
92+ }
93+ if ( foundList ) {
94+ const whereType = ( field . args . find ( a => a . name === 'where' ) ! . type as GraphQLInputObjectType ) . name
95+ return ` ${ type . name } : (where: ${ whereType } ): Promise<boolean> => super.existsDelegate('query', '${ field . name } ', { where }, {}, '{ id }')`
96+ }
97+ } )
98+ . filter ( f => f )
99+ . join ( ',\n' )
100+ }
101+
76102export function renderMainMethodFields ( operation : string , fields : GraphQLFieldMap < any , any > ) : string {
77103 return Object . keys ( fields )
78104 . map ( f => {
0 commit comments