@@ -11,6 +11,7 @@ permissions and limitations under the License.
11
11
*/
12
12
13
13
import { schemaStringify } from './schemaParser.js' ;
14
+ import { print } from 'graphql' ;
14
15
import { gql } from 'graphql-tag'
15
16
import { loggerInfo , yellow } from "./logger.js" ;
16
17
@@ -135,31 +136,10 @@ function injectChanges(schemaModel) {
135
136
136
137
function addNode ( def ) {
137
138
let name = def . name . value ;
138
-
139
- // Input fields
140
- let inputFields = '' ;
141
- inputFields += `\n _id: ID @id`
142
- def . fields . forEach ( field => {
143
- try {
144
- if ( field . name . value === 'id' ) {
145
- inputFields += `\n id: ID` ;
146
- }
147
-
148
- } catch { }
149
-
150
- try {
151
- if ( field . type . name . value === 'String' ||
152
- field . type . name . value === 'Int' ||
153
- field . type . name . value === 'Float' ||
154
- field . type . name . value === 'Boolean' ) {
155
-
156
- inputFields += `\n ${ field . name . value } : ${ field . type . name . value } ` ;
157
- }
158
- } catch { }
159
- } ) ;
139
+ const idField = getIdField ( def ) ;
160
140
161
141
// Create Input type
162
- typesToAdd . push ( `input ${ name } Input {${ inputFields } \n}` ) ;
142
+ typesToAdd . push ( `input ${ name } Input {\n ${ print ( getInputFields ( def ) ) } \n}` ) ;
163
143
164
144
// Create query
165
145
queriesToAdd . push ( `getNode${ name } (filter: ${ name } Input, options: Options): ${ name } \n` ) ;
@@ -168,7 +148,7 @@ function addNode(def) {
168
148
// Create mutation
169
149
mutationsToAdd . push ( `createNode${ name } (input: ${ name } Input!): ${ name } \n` ) ;
170
150
mutationsToAdd . push ( `updateNode${ name } (input: ${ name } Input!): ${ name } \n` ) ;
171
- mutationsToAdd . push ( `deleteNode${ name } (_id: ID! ): Boolean\n` ) ;
151
+ mutationsToAdd . push ( `deleteNode${ name } (${ print ( idFieldToInputValue ( idField ) ) } ): Boolean\n` ) ;
172
152
173
153
loggerInfo ( `Added input type: ${ yellow ( name + 'Input' ) } ` ) ;
174
154
loggerInfo ( `Added query: ${ yellow ( 'getNode' + name ) } ` ) ;
@@ -231,6 +211,47 @@ function addFilterOptionsArguments(field) {
231
211
}
232
212
233
213
214
+ function getIdField ( objTypeDef ) {
215
+ return objTypeDef . fields . find (
216
+ field =>
217
+ field . directives && field . directives . some ( directive => directive . name . value === 'id' )
218
+ ) ;
219
+ }
220
+
221
+
222
+ function createIdField ( ) {
223
+ return {
224
+ kind : 'FieldDefinition' ,
225
+ name : { kind : 'Name' , value : '_id' } ,
226
+ arguments : [ ] ,
227
+ type : { kind : 'NonNullType' , type : { kind : 'NamedType' , name : { kind : 'Name' , value : 'ID' } } } ,
228
+ directives : [
229
+ { kind : 'Directive' , name : { kind : 'Name' , value : 'id' } , arguments : [ ] }
230
+ ]
231
+ } ;
232
+ }
233
+
234
+
235
+ function idFieldToInputValue ( { name, type } ) {
236
+ return { kind : 'InputValueDefinition' , name, type } ;
237
+ }
238
+
239
+
240
+ function getInputFields ( objTypeDef ) {
241
+ return objTypeDef . fields . filter ( field => isScalar ( nullable ( field . type ) ) ) ;
242
+ }
243
+
244
+
245
+ function nullable ( type ) {
246
+ return type . kind === 'NonNullType' ? type . type : type ;
247
+ }
248
+
249
+
250
+ function isScalar ( type ) {
251
+ const scalarTypes = [ 'String' , 'Int' , 'Float' , 'Boolean' , 'ID' ] ;
252
+ return type . kind === 'NamedType' && scalarTypes . includes ( type . name . value ) ;
253
+ }
254
+
234
255
235
256
function inferGraphDatabaseDirectives ( schemaModel ) {
236
257
@@ -242,21 +263,15 @@ function inferGraphDatabaseDirectives(schemaModel) {
242
263
if ( def . kind == 'ObjectTypeDefinition' ) {
243
264
if ( ! ( def . name . value == 'Query' || def . name . value == 'Mutation' ) ) {
244
265
currentType = def . name . value ;
266
+
267
+ // Only add _id field to the object type if it doesn't have an ID field already
268
+ if ( ! getIdField ( def ) ) {
269
+ def . fields . unshift ( createIdField ( ) ) ;
270
+ }
271
+
245
272
addNode ( def ) ;
246
273
const edgesTypeToAdd = [ ] ;
247
274
248
- // Add _id field to the object type
249
- def . fields . unshift ( {
250
- kind : "FieldDefinition" , name : { kind : "Name" , value : "_id" } ,
251
- arguments : [ ] ,
252
- type : { kind : "NonNullType" , type : { kind : "NamedType" , name : { kind : "Name" , value : "ID" } } } ,
253
- directives : [
254
- { kind : "Directive" , name : { kind : "Name" , value : "id" } ,
255
- arguments : [ ]
256
- }
257
- ]
258
- } ) ;
259
-
260
275
// add relationships
261
276
def . fields . forEach ( field => {
262
277
if ( field . type . type !== undefined ) {
0 commit comments