import { GraphQLResolveInfo } from 'graphql' ; import graphqFields from 'graphql-fields' ; const fieldsToRelations = ( info , options = { depth : undefined , root : '' , excludeFields : [ ] , } ) : string [ ] => { const paths = [ ] ; const nested = ( field , key = undefined as any , deep = 0 , parent = [ ] ) => { if ( Object . values ( field ) . length === 0 ) { return ; } if ( deep > 0 || ! ! options . root ) { parent . push ( key ) ; if ( parent . slice ( ! options . root ? 0 : options . root ?. split ( '.' ) . length ) . length > 0 && parent . slice ( 0 , ! options . root ? 0 : options . root ?. split ( '.' ) . length ) . toString ( ) === ( ! options . root ? '' : options . root ?. split ( '.' ) . toString ( ) ) ) { const path = parent . slice ( ! options . root ? 0 : options . root ?. split ( '.' ) . length ) ; paths . push ( path ) ; } } Object . keys ( field ) . forEach ( ( key ) => { if ( Object . values ( field [ key ] ) . length > 0 && ! ! options . depth ? deep < options . depth : true ) { nested ( field [ key ] , key , deep + 1 , [ ... parent ] ) ; } } ) ; } ; const value = ! options . root ? graphqFields ( info , { } , { excludedFields : options . excludeFields } ) : options . root . split ( '.' ) . reduce ( function ( p , prop ) { return p [ prop ] ; } , graphqFields ( info , { } , { excludedFields : options . excludeFields } ) ) ; nested ( value , ! ! options . root ? options . root . split ( '.' ) . pop ( ) : undefined ) ; return paths . map ( ( list ) => list . join ( '.' ) ) ; } ; export = fieldsToRelations ;