From 6e3b8cce302c047a4bf9fa6c90cd1d25076d4a8d Mon Sep 17 00:00:00 2001 From: lorefnon Date: Sun, 24 Dec 2023 23:01:10 +0530 Subject: [PATCH] Warn against name collisions when defining types --- src/index.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index c670611..06c4d61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -597,6 +597,7 @@ class GArgs extends Type { export class GarphSchema { types: AnyType[] = [] + nodeType = this.interface('Node', { id: this.id() }) @@ -615,19 +616,29 @@ export class GarphSchema { after: this.id().optional() } + registerType(type: AnyType) { + const name = type.typeDef.name + if (name) { + if (this.types.find(t => t.typeDef.name === name)) { + throw new Error(`Detected multiple types with name: ${name}`) + } + } + this.types.push(type) + } + constructor ({ types }: { types: AnyType[] } = { types: [] }) { - this.types.push(...types) + types.forEach(t => this.registerType(t)) } type(name: N, shape: T) { const t = new GType(name, shape) - this.types.push(t) + this.registerType(t) return t } node(name: N, shape: T) { const t = new GType(name, shape).implements(this.nodeType) - this.types.push(t) + this.registerType(t) return t } @@ -637,7 +648,7 @@ export class GarphSchema { pageInfo: this.pageInfoType }) - this.types.push(t) + this.registerType(t) return t } @@ -650,37 +661,37 @@ export class GarphSchema { cursor: g.string() }) - this.types.push(t) + this.registerType(t) return t } inputType(name: N, shape: T) { const t = new GInput(name, shape) - this.types.push(t) + this.registerType(t) return t } enumType(name: N, args: T) { const t = new GEnum(name, args) - this.types.push(t) + this.registerType(t) return t } unionType(name: N, args: T) { const t = new GUnion(name, args) - this.types.push(t) + this.registerType(t) return t } scalarType(name: string, options?: ScalarOptions) { const t = new GScalar(name, options) - this.types.push(t) + this.registerType(t) return t } interface(name: N, shape: T) { const t = new GInterface(name, shape) - this.types.push(t) + this.registerType(t) return t }