File tree 4 files changed +156
-2
lines changed
4 files changed +156
-2
lines changed Original file line number Diff line number Diff line change 1
- import { ParserField , ParserTree } from '@/Models' ;
1
+ import { ParserField , ParserTree , TypeSystemDefinition } from '@/Models' ;
2
2
import { Parser } from '@/Parser' ;
3
3
import { isExtensionNode } from '@/TreeOperations/shared' ;
4
4
import { TreeToGraphQL } from '@/TreeToGraphQL' ;
@@ -86,7 +86,9 @@ export const mergeTrees = (tree1: ParserTree, tree2: ParserTree) => {
86
86
export const mergeSDLs = ( sdl1 : string , sdl2 : string ) => {
87
87
const t1 = Parser . parse ( sdl1 ) ;
88
88
const t2 = Parser . parse ( sdl2 ) ;
89
- const mergeResult = mergeTrees ( t1 , t2 ) ;
89
+ const mergeResult = mergeTrees ( t1 , {
90
+ nodes : t2 . nodes . filter ( ( n ) => n . data . type !== TypeSystemDefinition . SchemaDefinition ) ,
91
+ } ) ;
90
92
if ( mergeResult . __typename === 'success' ) {
91
93
const sdl = TreeToGraphQL . parse ( mergeResult ) ;
92
94
return {
Original file line number Diff line number Diff line change 1
1
import {
2
2
createParserField ,
3
3
createPlainDirectiveImplementation ,
4
+ createPlainField ,
4
5
createRootDirectiveField ,
5
6
createSchemaDefinition ,
6
7
createSchemaExtension ,
@@ -274,6 +275,69 @@ describe('Schema base operations', () => {
274
275
} ;
275
276
expect ( tree . nodes ) . toEqual ( expect . arrayContaining ( treeMock . nodes ) ) ;
276
277
} ) ;
278
+ test ( `don't override base DDD type with Query` , ( ) => {
279
+ const schema = `type Query{
280
+ status: ${ ScalarTypes . String }
281
+ }
282
+ type DDD {
283
+ name: String
284
+ }
285
+ schema{
286
+ query: DDD
287
+ }
288
+ ` ;
289
+ const tree = Parser . parse ( schema ) ;
290
+ const treeMock : ParserTree = {
291
+ nodes : [
292
+ createParserField ( {
293
+ name : 'Query' ,
294
+ type : {
295
+ fieldType : {
296
+ name : TypeDefinitionDisplayStrings . type ,
297
+ type : Options . name ,
298
+ } ,
299
+ } ,
300
+ data : {
301
+ type : TypeDefinition . ObjectTypeDefinition ,
302
+ } ,
303
+
304
+ args : [
305
+ createParserField ( {
306
+ name : 'status' ,
307
+ type : {
308
+ fieldType : {
309
+ name : ScalarTypes . String ,
310
+ type : Options . name ,
311
+ } ,
312
+ } ,
313
+ data : {
314
+ type : TypeSystemDefinition . FieldDefinition ,
315
+ } ,
316
+ } ) ,
317
+ ] ,
318
+ } ) ,
319
+ createParserField ( {
320
+ name : 'DDD' ,
321
+ type : {
322
+ fieldType : {
323
+ name : TypeDefinitionDisplayStrings . type ,
324
+ type : Options . name ,
325
+ } ,
326
+ } ,
327
+ data : {
328
+ type : TypeDefinition . ObjectTypeDefinition ,
329
+ } ,
330
+ args : [ createPlainField ( { name : 'name' , type : 'String' } ) ] ,
331
+ } ) ,
332
+ createSchemaDefinition ( {
333
+ operations : {
334
+ query : 'DDD' ,
335
+ } ,
336
+ } ) ,
337
+ ] ,
338
+ } ;
339
+ expect ( tree . nodes ) . toEqual ( expect . arrayContaining ( treeMock . nodes ) ) ;
340
+ } ) ;
277
341
test ( `empty query` , ( ) => {
278
342
const schema = `
279
343
type Query
Original file line number Diff line number Diff line change @@ -154,4 +154,39 @@ describe('Merging GraphQL Schemas', () => {
154
154
}` ,
155
155
) ;
156
156
} ) ;
157
+
158
+ it ( 'Should merge schemas but maintain original schema node' , ( ) => {
159
+ const baseSchema = `
160
+ type DDD{
161
+ firstName: String
162
+ health: String
163
+ }
164
+ schema{
165
+ query: DDD
166
+ }
167
+ ` ;
168
+
169
+ const mergingSchema = `
170
+ type Query{
171
+ lastName: String
172
+ }
173
+ ` ;
174
+ const t1 = mergeSDLs ( baseSchema , mergingSchema ) ;
175
+ if ( t1 . __typename === 'error' ) throw new Error ( 'Invalid parse' ) ;
176
+ expectTrimmedEqual (
177
+ t1 . sdl ,
178
+ `
179
+ type DDD{
180
+ firstName: String
181
+ health: String
182
+ }
183
+ schema{
184
+ query: DDD
185
+ }
186
+ type Query{
187
+ lastName: String
188
+ }
189
+ ` ,
190
+ ) ;
191
+ } ) ;
157
192
} ) ;
Original file line number Diff line number Diff line change 1
1
import {
2
2
createParserField ,
3
3
createPlainDirectiveImplementation ,
4
+ createPlainField ,
4
5
createRootDirectiveField ,
5
6
createSchemaDefinition ,
6
7
} from '@/shared' ;
@@ -56,6 +57,58 @@ describe('Schema base operations in TreeToGraphQL', () => {
56
57
const graphql = trimGraphQL ( TreeToGraphQL . parse ( treeMock ) ) ;
57
58
expect ( graphql ) . toContain ( trimGraphQL ( `schema{ query: Query}` ) ) ;
58
59
} ) ;
60
+ test ( `query with different name and 'Query' named node` , ( ) => {
61
+ const treeMock : ParserTree = {
62
+ nodes : [
63
+ createParserField ( {
64
+ name : 'Query' ,
65
+ type : {
66
+ fieldType : {
67
+ name : TypeDefinitionDisplayStrings . type ,
68
+ type : Options . name ,
69
+ } ,
70
+ } ,
71
+ data : {
72
+ type : TypeDefinition . ObjectTypeDefinition ,
73
+ } ,
74
+ args : [
75
+ createParserField ( {
76
+ name : 'status' ,
77
+ type : {
78
+ fieldType : {
79
+ name : ScalarTypes . String ,
80
+ type : Options . name ,
81
+ } ,
82
+ } ,
83
+ data : {
84
+ type : TypeSystemDefinition . FieldDefinition ,
85
+ } ,
86
+ } ) ,
87
+ ] ,
88
+ } ) ,
89
+ createParserField ( {
90
+ name : 'DDD' ,
91
+ type : {
92
+ fieldType : {
93
+ name : TypeDefinitionDisplayStrings . type ,
94
+ type : Options . name ,
95
+ } ,
96
+ } ,
97
+ data : {
98
+ type : TypeDefinition . ObjectTypeDefinition ,
99
+ } ,
100
+ args : [ createPlainField ( { name : 'name' , type : 'String' } ) ] ,
101
+ } ) ,
102
+ createSchemaDefinition ( {
103
+ operations : {
104
+ query : 'DDD' ,
105
+ } ,
106
+ } ) ,
107
+ ] ,
108
+ } ;
109
+ const graphql = trimGraphQL ( TreeToGraphQL . parse ( treeMock ) ) ;
110
+ expect ( graphql ) . toContain ( trimGraphQL ( `schema{ query: DDD}` ) ) ;
111
+ } ) ;
59
112
test ( `query with directives` , ( ) => {
60
113
const treeMock : ParserTree = {
61
114
nodes : [
You can’t perform that action at this time.
0 commit comments