@@ -8083,80 +8083,47 @@ gql.disableFragmentWarnings = disableFragmentWarnings;
8083
8083
8084
8084
var src = gql ;
8085
8085
8086
- var Logger = /** @class */ ( function ( ) {
8087
- function Logger ( enabled ) {
8088
- this . enabled = enabled ;
8089
- }
8090
- Logger . prototype . group = function ( ) {
8091
- var messages = [ ] ;
8092
- for ( var _i = 0 ; _i < arguments . length ; _i ++ ) {
8093
- messages [ _i ] = arguments [ _i ] ;
8094
- }
8095
- if ( this . enabled ) {
8096
- console . group . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8097
- }
8098
- } ;
8099
- Logger . prototype . groupEnd = function ( ) {
8100
- if ( this . enabled )
8101
- console . groupEnd ( ) ;
8102
- } ;
8103
- Logger . prototype . log = function ( ) {
8104
- var messages = [ ] ;
8105
- for ( var _i = 0 ; _i < arguments . length ; _i ++ ) {
8106
- messages [ _i ] = arguments [ _i ] ;
8107
- }
8108
- if ( this . enabled ) {
8109
- console . log . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8110
- }
8111
- } ;
8112
- Logger . prototype . logQuery = function ( query ) {
8113
- if ( this . enabled ) {
8114
- try {
8115
- this . group ( 'Sending query:' ) ;
8116
- if ( typeof query === 'object' && query . loc ) {
8117
- console . log ( this . prettify ( query . loc . source . body ) ) ;
8118
- }
8119
- else {
8120
- console . log ( this . prettify ( query ) ) ;
8121
- }
8122
- this . groupEnd ( ) ;
8123
- }
8124
- catch ( e ) {
8125
- console . error ( '[Vuex-ORM-Apollo] There is a syntax error in the query!' , e , query ) ;
8126
- }
8127
- }
8128
- } ;
8129
- Logger . prototype . prettify = function ( query ) {
8130
- return printer_1 ( parser_1 ( query ) ) ;
8131
- } ;
8132
- return Logger ;
8133
- } ( ) ) ;
8134
-
8135
8086
var inflection$1 = require ( 'inflection' ) ;
8087
+ /**
8088
+ * This class takes care of everything GraphQL query related, especially the generation of queries out of models
8089
+ */
8136
8090
var QueryBuilder = /** @class */ ( function ( ) {
8091
+ /**
8092
+ * Constructor.
8093
+ * @param {Logger } logger
8094
+ * @param {(name: (Model | string)) => Model } getModel
8095
+ */
8137
8096
function QueryBuilder ( logger , getModel ) {
8138
8097
this . logger = logger ;
8139
8098
this . getModel = getModel ;
8140
8099
}
8100
+ /**
8101
+ * Takes a string with a graphql query and formats it
8102
+ * @param {string } query
8103
+ * @returns {string }
8104
+ */
8105
+ QueryBuilder . prettify = function ( query ) {
8106
+ return printer_1 ( parser_1 ( query ) ) ;
8107
+ } ;
8141
8108
/**
8142
8109
* Generates the arguments string for a graphql query based on a given map.
8143
8110
*
8144
8111
* There are three types of arguments:
8145
8112
*
8146
- * 1) Signatures with attributes (signature = true)
8113
+ * 1) Signatures with simple types (signature = true)
8147
8114
* mutation createUser($name: String!)
8148
8115
*
8149
- * 2) Signatures with object (signature = true, args = { user: { __type: 'User' }})
8150
- * mutation createUser($user: User !)
8116
+ * 2) Signatures with object types (signature = true, args = { user: { __type: 'User' }})
8117
+ * mutation createUser($user: UserInput !)
8151
8118
*
8152
- * 3) Field with values (signature = false, valuesAsVariables = false)
8153
- * user(id: 15)
8119
+ * 3) Fields with values (signature = false, valuesAsVariables = false)
8120
+ * query user(id: 15)
8154
8121
*
8155
- * 4) Field with variables (signature = false, valuesAsVariables = true)
8156
- * user(id: $id)
8122
+ * 4) Fields with variables (signature = false, valuesAsVariables = true)
8123
+ * query user(id: $id)
8157
8124
*
8158
- * 5) Field with object value (signature = false, valuesAsVariables = false, args = { user: { __type: 'User' }})
8159
- * createUser(user: {...})
8125
+ * 5) Fields with object value (signature = false, valuesAsVariables = false, args = { user: { __type: 'User' }})
8126
+ * mutation createUser(user: {...})
8160
8127
*
8161
8128
* @param {Arguments | undefined } args
8162
8129
* @param {boolean } signature When true, then this method generates a query signature instead of key/value pairs
@@ -8169,6 +8136,7 @@ var QueryBuilder = /** @class */ (function () {
8169
8136
if ( valuesAsVariables === void 0 ) { valuesAsVariables = false ; }
8170
8137
var returnValue = '' ;
8171
8138
var any = false ;
8139
+ var first = true ;
8172
8140
if ( args ) {
8173
8141
Object . keys ( args ) . forEach ( function ( key ) {
8174
8142
var value = args [ key ] ;
@@ -8193,27 +8161,38 @@ var QueryBuilder = /** @class */ (function () {
8193
8161
else {
8194
8162
if ( typeof value === 'object' && value . __type ) {
8195
8163
// Case 3 ({name: 'Helga Hufflepuff"})
8196
- typeOrValue = value ;
8164
+ typeOrValue = JSON . stringify ( value ) ;
8197
8165
}
8198
8166
else {
8199
8167
// Case 3 ("someValue")
8200
8168
typeOrValue = typeof value === 'number' ? value : "\"" + value + "\"" ;
8201
8169
}
8202
8170
}
8203
- returnValue = returnValue + " " + ( ( signature ? '$' : '' ) + key ) + ": " + typeOrValue ;
8171
+ returnValue = "" + returnValue + ( first ? '' : ', ' ) + ( ( signature ? '$' : '' ) + key ) + ": " + typeOrValue ;
8172
+ first = false ;
8204
8173
}
8205
8174
} ) ;
8206
8175
if ( any )
8207
8176
returnValue = "(" + returnValue + ")" ;
8208
8177
}
8209
8178
return returnValue ;
8210
8179
} ;
8180
+ /**
8181
+ * Transforms outgoing data. Use for variables param.
8182
+ *
8183
+ * Omits relations and id fields.
8184
+ *
8185
+ * @param {Data } data
8186
+ * @returns {Data }
8187
+ */
8211
8188
QueryBuilder . prototype . transformOutgoingData = function ( data ) {
8189
+ var model = this . getModel ( data . $self ( ) . entity ) ;
8190
+ var relations = model . getRelations ( ) ;
8212
8191
var returnValue = { } ;
8213
8192
Object . keys ( data ) . forEach ( function ( key ) {
8214
8193
var value = data [ key ] ;
8215
8194
// Ignore IDs and connections
8216
- if ( ! ( value instanceof Array || key === 'id' ) ) {
8195
+ if ( ! relations . has ( key ) && key !== 'id' ) {
8217
8196
returnValue [ key ] = value ;
8218
8197
}
8219
8198
} ) ;
@@ -8318,6 +8297,52 @@ var QueryBuilder = /** @class */ (function () {
8318
8297
return QueryBuilder ;
8319
8298
} ( ) ) ;
8320
8299
8300
+ var Logger = /** @class */ ( function ( ) {
8301
+ function Logger ( enabled ) {
8302
+ this . enabled = enabled ;
8303
+ }
8304
+ Logger . prototype . group = function ( ) {
8305
+ var messages = [ ] ;
8306
+ for ( var _i = 0 ; _i < arguments . length ; _i ++ ) {
8307
+ messages [ _i ] = arguments [ _i ] ;
8308
+ }
8309
+ if ( this . enabled ) {
8310
+ console . group . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8311
+ }
8312
+ } ;
8313
+ Logger . prototype . groupEnd = function ( ) {
8314
+ if ( this . enabled )
8315
+ console . groupEnd ( ) ;
8316
+ } ;
8317
+ Logger . prototype . log = function ( ) {
8318
+ var messages = [ ] ;
8319
+ for ( var _i = 0 ; _i < arguments . length ; _i ++ ) {
8320
+ messages [ _i ] = arguments [ _i ] ;
8321
+ }
8322
+ if ( this . enabled ) {
8323
+ console . log . apply ( console , [ '[Vuex-ORM-Apollo]' ] . concat ( messages ) ) ;
8324
+ }
8325
+ } ;
8326
+ Logger . prototype . logQuery = function ( query ) {
8327
+ if ( this . enabled ) {
8328
+ try {
8329
+ this . group ( 'Sending query:' ) ;
8330
+ if ( typeof query === 'object' && query . loc ) {
8331
+ console . log ( QueryBuilder . prettify ( query . loc . source . body ) ) ;
8332
+ }
8333
+ else {
8334
+ console . log ( QueryBuilder . prettify ( query ) ) ;
8335
+ }
8336
+ this . groupEnd ( ) ;
8337
+ }
8338
+ catch ( e ) {
8339
+ console . error ( '[Vuex-ORM-Apollo] There is a syntax error in the query!' , e , query ) ;
8340
+ }
8341
+ }
8342
+ } ;
8343
+ return Logger ;
8344
+ } ( ) ) ;
8345
+
8321
8346
/**
8322
8347
* Capitalizes the first letter of the given string.
8323
8348
*
0 commit comments