File tree Expand file tree Collapse file tree 4 files changed +156
-2
lines changed Expand file tree Collapse file tree 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' ;
22import { Parser } from '@/Parser' ;
33import { isExtensionNode } from '@/TreeOperations/shared' ;
44import { TreeToGraphQL } from '@/TreeToGraphQL' ;
@@ -86,7 +86,9 @@ export const mergeTrees = (tree1: ParserTree, tree2: ParserTree) => {
8686export const mergeSDLs = ( sdl1 : string , sdl2 : string ) => {
8787 const t1 = Parser . parse ( sdl1 ) ;
8888 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+ } ) ;
9092 if ( mergeResult . __typename === 'success' ) {
9193 const sdl = TreeToGraphQL . parse ( mergeResult ) ;
9294 return {
Original file line number Diff line number Diff line change 11import {
22 createParserField ,
33 createPlainDirectiveImplementation ,
4+ createPlainField ,
45 createRootDirectiveField ,
56 createSchemaDefinition ,
67 createSchemaExtension ,
@@ -274,6 +275,69 @@ describe('Schema base operations', () => {
274275 } ;
275276 expect ( tree . nodes ) . toEqual ( expect . arrayContaining ( treeMock . nodes ) ) ;
276277 } ) ;
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+ } ) ;
277341 test ( `empty query` , ( ) => {
278342 const schema = `
279343 type Query
Original file line number Diff line number Diff line change @@ -154,4 +154,39 @@ describe('Merging GraphQL Schemas', () => {
154154 }` ,
155155 ) ;
156156 } ) ;
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+ } ) ;
157192} ) ;
Original file line number Diff line number Diff line change 11import {
22 createParserField ,
33 createPlainDirectiveImplementation ,
4+ createPlainField ,
45 createRootDirectiveField ,
56 createSchemaDefinition ,
67} from '@/shared' ;
@@ -56,6 +57,58 @@ describe('Schema base operations in TreeToGraphQL', () => {
5657 const graphql = trimGraphQL ( TreeToGraphQL . parse ( treeMock ) ) ;
5758 expect ( graphql ) . toContain ( trimGraphQL ( `schema{ query: Query}` ) ) ;
5859 } ) ;
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+ } ) ;
59112 test ( `query with directives` , ( ) => {
60113 const treeMock : ParserTree = {
61114 nodes : [
You can’t perform that action at this time.
0 commit comments