/**
 * Contains parsed GraphQL query object and helper methods
 */ export class QueryObject { /**
     * Query fields specified in client side query
     */ public fields : string [ ] ; /**
     * All relations that are part of the query
     */ public relations : { [ relationName ] : any ; } ; constructor ( fields , relations ) { this . fields = fields ; this . relations = relations ; } /**
     * Check if object has relations
     */ public hasRelations ( ) { return Object . keys ( this . relations ) . length !== 0 ; } /**
     * Checks if object has specified relation
     */ public hasRelation ( name ) { return ! ! this . relations [ name ] ; } param separator public getRootFields ( separator = ',' ) { return this . fields . map ( ( field ) => { return `${ field }` ; } ) . join ( separator ) ; } param mapper param separator getRelationFields ( relation , mapper = 'as' , separator = ',' ) { if ( this . relations [ relation ] ) { return this . relations [ relation ] . map ( ( field ) => { return `${ field } ${ mapper } ${ relation }__${ field }` ; } ) . join ( separator ) ; } } /**
     * Expands single key structure returned from database to graph that can
     * be returned by resolver. Method picks all fields that starts with relation name.
     * For example 'relation__field' and puts them into nested relation structure.
     */ expandToGraph ( data ) { const relationKeys = Object . keys ( this . relations ) ; for ( const relation of relationKeys ) { for ( const element of data ) { element [ relation ] = { } ; for ( const key in element ) { if ( key . startsWith ( `${ relation }__` ) ) { const originalKey = key . replace ( `${ relation }__` , '' ) ; element [ relation ] [ originalKey ] = element [ key ] ; } } } } return data ; } }