-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: supports unions and adds validation for duplicate union and int…
…erface.
- Loading branch information
1 parent
94427fe
commit 1a814ce
Showing
7 changed files
with
164 additions
and
18 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
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 |
---|---|---|
@@ -1,14 +1,19 @@ | ||
import gql from 'graphql-tag'; | ||
import validateUniqueName from './validateUniqueName'; | ||
|
||
export default (graphqlString: string) => { | ||
const graphqlAST = gql` | ||
${graphqlString} | ||
`; | ||
|
||
return ( | ||
graphqlAST.definitions | ||
.filter((d) => ['InterfaceTypeDefinition'].indexOf(d.kind) > -1) | ||
// @ts-ignore | ||
.map((f) => f.name.value) | ||
); | ||
const interfacesTypeDefs = graphqlAST.definitions | ||
.filter((d) => ['InterfaceTypeDefinition'].indexOf(d.kind) > -1) | ||
// @ts-ignore | ||
.map((f) => f.name.value); | ||
|
||
validateUniqueName(interfacesTypeDefs, (name: string) => { | ||
throw new Error(`Duplicate interface name found: ${name}`); | ||
}); | ||
|
||
return interfacesTypeDefs; | ||
}; |
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,51 @@ | ||
import getUnions from './getUnions'; | ||
|
||
const gql = (a: TemplateStringsArray) => a[0]; | ||
|
||
test('get the unions', () => { | ||
const schemaString = gql` | ||
type Query { | ||
homes: [Home] | ||
} | ||
type Cottage { | ||
id: ID! | ||
address: String! | ||
} | ||
type Villa { | ||
id: ID! | ||
address: String! | ||
owner: String! | ||
} | ||
union Home = Cottage | Villa | ||
`; | ||
|
||
const res = getUnions(schemaString); | ||
|
||
expect(res).toEqual(['Home']); | ||
}); | ||
test('should throw exception if duplicate union names are found', () => { | ||
const schemaString = gql` | ||
type Query { | ||
homes: [Home] | ||
} | ||
type Cottage { | ||
id: ID! | ||
address: String! | ||
} | ||
type Villa { | ||
id: ID! | ||
address: String! | ||
owner: String! | ||
} | ||
union Home = Cottage | Villa | ||
union Home = Cottage | Villa | ||
`; | ||
|
||
expect(() => getUnions(schemaString)).toThrow('Duplicate union name found: Home'); | ||
}); |
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,18 @@ | ||
import gql from 'graphql-tag'; | ||
import validateUniqueName from './validateUniqueName'; | ||
|
||
export default (graphqlString: string) => { | ||
const graphqlAST = gql` | ||
${graphqlString} | ||
`; | ||
|
||
const unionTypeDefinitions = graphqlAST.definitions | ||
.filter((d) => ['UnionTypeDefinition'].indexOf(d.kind) > -1) | ||
// @ts-ignore | ||
.map((f) => f.name.value); | ||
|
||
validateUniqueName(unionTypeDefinitions, (name: string) => { | ||
throw new Error(`Duplicate union name found: ${name}`); | ||
}); | ||
return unionTypeDefinitions; | ||
}; |
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,19 @@ | ||
import validateUniqueName from './validateUniqueName'; | ||
|
||
test('should throw error if duplicate entry found', () => { | ||
const names = ['apple', 'banana', 'orange', 'banana']; | ||
expect(() => | ||
validateUniqueName(names, (name: any) => { | ||
throw new Error(`duplicate record found: ${name}`); | ||
}), | ||
).toThrow('duplicate record found: banana'); | ||
}); | ||
|
||
test('should not throw error if no duplicate entry found', () => { | ||
const names = ['apple', 'banana', 'orange']; | ||
expect(() => | ||
validateUniqueName(names, () => { | ||
throw new Error('duplicate record found'); | ||
}), | ||
).not.toThrow(); | ||
}); |
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,8 @@ | ||
export default function (names: string[], exceptionHandler: Function) { | ||
const nameCounts: Map<string, number> = new Map(); | ||
names.forEach((name) => { | ||
const val = (nameCounts.get(name) ?? 0) + 1; | ||
if (val > 1) exceptionHandler(name); | ||
nameCounts.set(name, val); | ||
}); | ||
} |