Skip to content

Commit 9cdee24

Browse files
authored
Merge pull request #85 from lorefnon/warn-name-collision
Warn against name collisions when defining types
2 parents 512faf1 + 6e3b8cc commit 9cdee24

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

src/index.ts

+21-10
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ class GArgs<T extends AnyType, X extends Args> extends Type<T, 'Args'> {
597597

598598
export class GarphSchema {
599599
types: AnyType[] = []
600+
600601
nodeType = this.interface('Node', {
601602
id: this.id()
602603
})
@@ -615,19 +616,29 @@ export class GarphSchema {
615616
after: this.id().optional()
616617
}
617618

619+
registerType(type: AnyType) {
620+
const name = type.typeDef.name
621+
if (name) {
622+
if (this.types.find(t => t.typeDef.name === name)) {
623+
throw new Error(`Detected multiple types with name: ${name}`)
624+
}
625+
}
626+
this.types.push(type)
627+
}
628+
618629
constructor ({ types }: { types: AnyType[] } = { types: [] }) {
619-
this.types.push(...types)
630+
types.forEach(t => this.registerType(t))
620631
}
621632

622633
type<N extends string, T extends AnyTypes>(name: N, shape: T) {
623634
const t = new GType<N, T>(name, shape)
624-
this.types.push(t)
635+
this.registerType(t)
625636
return t
626637
}
627638

628639
node<N extends string, T extends AnyTypes>(name: N, shape: T) {
629640
const t = new GType<N, T>(name, shape).implements(this.nodeType)
630-
this.types.push(t)
641+
this.registerType(t)
631642
return t
632643
}
633644

@@ -637,7 +648,7 @@ export class GarphSchema {
637648
pageInfo: this.pageInfoType
638649
})
639650

640-
this.types.push(t)
651+
this.registerType(t)
641652
return t
642653
}
643654

@@ -650,37 +661,37 @@ export class GarphSchema {
650661
cursor: g.string()
651662
})
652663

653-
this.types.push(t)
664+
this.registerType(t)
654665
return t
655666
}
656667

657668
inputType<N extends string, T extends AnyTypes>(name: N, shape: T) {
658669
const t = new GInput<N, T>(name, shape)
659-
this.types.push(t)
670+
this.registerType(t)
660671
return t
661672
}
662673

663674
enumType<N extends string, T extends readonly string[] | TSEnumType>(name: N, args: T) {
664675
const t = new GEnum<N, T>(name, args)
665-
this.types.push(t)
676+
this.registerType(t)
666677
return t
667678
}
668679

669680
unionType<N extends string, T extends AnyObjects>(name: N, args: T) {
670681
const t = new GUnion<N, T>(name, args)
671-
this.types.push(t)
682+
this.registerType(t)
672683
return t
673684
}
674685

675686
scalarType<I, O>(name: string, options?: ScalarOptions<I, O>) {
676687
const t = new GScalar<I, O>(name, options)
677-
this.types.push(t)
688+
this.registerType(t)
678689
return t
679690
}
680691

681692
interface<N extends string, T extends AnyTypes>(name: N, shape: T) {
682693
const t = new GInterface<N, T>(name, shape)
683-
this.types.push(t)
694+
this.registerType(t)
684695
return t
685696
}
686697

0 commit comments

Comments
 (0)