-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
258 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ParserField, Options } from '@/Models'; | ||
import { MergeError } from '@/TreeOperations/merge/common'; | ||
|
||
export const mergeArguments = (parentName: string, args1: ParserField[], args2: ParserField[]) => { | ||
args2 | ||
.filter((a) => a.type.fieldType.type === Options.required) | ||
.forEach((a2) => { | ||
if (!args1.find((a1) => a1.name === a2.name)) | ||
throw new MergeError({ | ||
conflictingNode: parentName, | ||
conflictingField: a2.name, | ||
message: 'Cannot merge when required argument does not exist in correlated node', | ||
}); | ||
}); | ||
return args1 | ||
.map((a1) => { | ||
const equivalentA2 = args2.find((a2) => a2.name === a1.name); | ||
if (!equivalentA2 && a1.type.fieldType.type === Options.required) | ||
throw new MergeError({ | ||
conflictingNode: parentName, | ||
conflictingField: a1.name, | ||
message: 'Cannot merge when required argument does not exist in correlated node', | ||
}); | ||
if (!equivalentA2) return; | ||
if (a1.type.fieldType.type === Options.required) return a1; | ||
if (equivalentA2.type.fieldType.type === Options.required) return equivalentA2; | ||
}) | ||
.filter(<T>(v: T | undefined): v is T => !!v); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export class MergeError extends Error { | ||
constructor( | ||
public errorParams: { | ||
conflictingNode: string; | ||
conflictingField?: string; | ||
message?: string; | ||
}, | ||
) { | ||
super('Merging error'); | ||
} | ||
} | ||
|
||
export type ErrorConflict = { conflictingNode: string; conflictingField?: string }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { mergeSDLs } from '@/TreeOperations/merge/merge'; | ||
import { expectTrimmedEqual } from '@/__tests__/TestUtils'; | ||
|
||
describe('Merging GraphQL Inputs and field arguments', () => { | ||
it('Should merge inputs leaving only common fields.', () => { | ||
const baseSchema = ` | ||
input UserInput { | ||
name: String! | ||
age: Int # Not in Subgraph B | ||
} | ||
`; | ||
|
||
const mergingSchema = ` | ||
input UserInput { | ||
name: String! | ||
email: String # Not in Subgraph A | ||
} | ||
`; | ||
const t1 = mergeSDLs(baseSchema, mergingSchema); | ||
if (t1.__typename === 'error') throw new Error('Invalid parse'); | ||
expectTrimmedEqual( | ||
t1.sdl, | ||
` | ||
input UserInput{ | ||
name: String! | ||
}`, | ||
); | ||
}); | ||
it('Should merge inputs marking fields required.', () => { | ||
const baseSchema = ` | ||
input UserInput { | ||
name: String! | ||
age: Int | ||
} | ||
`; | ||
|
||
const mergingSchema = ` | ||
input UserInput { | ||
name: String | ||
age: Int! | ||
} | ||
`; | ||
const t1 = mergeSDLs(baseSchema, mergingSchema); | ||
if (t1.__typename === 'error') throw new Error('Invalid parse'); | ||
expectTrimmedEqual( | ||
t1.sdl, | ||
` | ||
input UserInput{ | ||
name: String! | ||
age: Int! | ||
}`, | ||
); | ||
}); | ||
it('Should not merge inputs', () => { | ||
const baseSchema = ` | ||
input UserInput { | ||
name: String! | ||
} | ||
`; | ||
|
||
const mergingSchema = ` | ||
input UserInput { | ||
name: String! | ||
email: String! | ||
} | ||
`; | ||
const t1 = mergeSDLs(baseSchema, mergingSchema); | ||
if (t1.__typename === 'success') console.log(t1.sdl); | ||
expect(t1.__typename).toEqual('error'); | ||
}); | ||
it('Should merge field arguments marking them required.', () => { | ||
const baseSchema = ` | ||
type Main{ | ||
getUsers(funny: Boolean, premium: String!): String! | ||
} | ||
`; | ||
|
||
const mergingSchema = ` | ||
type Main{ | ||
getUsers(funny: Boolean!, premium: String): String! | ||
} | ||
`; | ||
const t1 = mergeSDLs(baseSchema, mergingSchema); | ||
if (t1.__typename === 'error') throw new Error('Invalid parse'); | ||
expectTrimmedEqual( | ||
t1.sdl, | ||
` | ||
type Main{ | ||
getUsers(funny: Boolean! premium: String!): String! | ||
}`, | ||
); | ||
}); | ||
it('Should merge field arguments leaving only common fields.', () => { | ||
const baseSchema = ` | ||
type Main{ | ||
getUsers(premium: String!): String! | ||
} | ||
`; | ||
|
||
const mergingSchema = ` | ||
type Main{ | ||
getUsers(funny: Boolean, premium: String): String! | ||
} | ||
`; | ||
const t1 = mergeSDLs(baseSchema, mergingSchema); | ||
if (t1.__typename === 'error') throw new Error('Invalid parse'); | ||
expectTrimmedEqual( | ||
t1.sdl, | ||
` | ||
type Main{ | ||
getUsers(premium: String!): String! | ||
}`, | ||
); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
src/__tests__/TreeOperations/merge/merge.interfaces.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { mergeSDLs } from '@/TreeOperations/merge/merge'; | ||
|
||
// const mergingErrorSchema = ` | ||
// type Person{ | ||
// lastName: String | ||
// } | ||
// `; | ||
|
||
describe('Merging GraphQL Schemas', () => { | ||
it('should not merge interfaces and implementation of both nodes', () => { | ||
const baseSchema = ` | ||
type Person implements Node{ | ||
firstName: String | ||
health: String | ||
_id: String | ||
} | ||
interface Node { | ||
_id: String | ||
} | ||
`; | ||
|
||
const mergingSchema = ` | ||
type Person implements Dateable{ | ||
lastName: String | ||
createdAt: String | ||
} | ||
interface Dateable { | ||
createdAt: String | ||
} | ||
`; | ||
const t1 = mergeSDLs(baseSchema, mergingSchema); | ||
expect(t1.__typename).toEqual('error'); | ||
}); | ||
}); |
Oops, something went wrong.