forked from neo4j/graphql
-
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.
Merge pull request neo4j#3889 from mjfwebb/schema-model-abstract-types-2
Implement interfaces and unions
- Loading branch information
Showing
21 changed files
with
819 additions
and
180 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
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
106 changes: 106 additions & 0 deletions
106
packages/graphql/src/schema-model/entity/InterfaceEntity.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,106 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Neo4jGraphQLSchemaValidationError } from "../../classes"; | ||
import type { Annotation, Annotations } from "../annotation/Annotation"; | ||
import { annotationToKey } from "../annotation/Annotation"; | ||
import type { Attribute } from "../attribute/Attribute"; | ||
import type { Relationship } from "../relationship/Relationship"; | ||
import type { CompositeEntity } from "./CompositeEntity"; | ||
import type { ConcreteEntity } from "./ConcreteEntity"; | ||
|
||
export class InterfaceEntity implements CompositeEntity { | ||
public readonly name: string; | ||
public readonly concreteEntities: ConcreteEntity[]; | ||
public readonly attributes: Map<string, Attribute> = new Map(); | ||
public readonly relationships: Map<string, Relationship> = new Map(); | ||
public readonly annotations: Partial<Annotations> = {}; | ||
|
||
constructor({ | ||
name, | ||
concreteEntities, | ||
attributes = [], | ||
annotations = [], | ||
relationships = [], | ||
}: { | ||
name: string; | ||
concreteEntities: ConcreteEntity[]; | ||
attributes?: Attribute[]; | ||
annotations?: Annotation[]; | ||
relationships?: Relationship[]; | ||
}) { | ||
this.name = name; | ||
this.concreteEntities = concreteEntities; | ||
for (const attribute of attributes) { | ||
this.addAttribute(attribute); | ||
} | ||
|
||
for (const annotation of annotations) { | ||
this.addAnnotation(annotation); | ||
} | ||
|
||
for (const relationship of relationships) { | ||
this.addRelationship(relationship); | ||
} | ||
} | ||
|
||
isConcreteEntity(): this is ConcreteEntity { | ||
return false; | ||
} | ||
isCompositeEntity(): this is CompositeEntity { | ||
return true; | ||
} | ||
|
||
private addAttribute(attribute: Attribute): void { | ||
if (this.attributes.has(attribute.name)) { | ||
throw new Neo4jGraphQLSchemaValidationError(`Attribute ${attribute.name} already exists in ${this.name}`); | ||
} | ||
this.attributes.set(attribute.name, attribute); | ||
} | ||
|
||
private addAnnotation(annotation: Annotation): void { | ||
const annotationKey = annotationToKey(annotation); | ||
const existingAnnotation = this.annotations[annotationKey]; | ||
|
||
if (existingAnnotation) { | ||
throw new Neo4jGraphQLSchemaValidationError(`Annotation ${annotationKey} already exists in ${this.name}`); | ||
} | ||
|
||
// We cast to any because we aren't narrowing the Annotation type here. | ||
// There's no reason to narrow either, since we care more about performance. | ||
this.annotations[annotationKey] = annotation as any; | ||
} | ||
|
||
public addRelationship(relationship: Relationship): void { | ||
if (this.relationships.has(relationship.name)) { | ||
throw new Neo4jGraphQLSchemaValidationError( | ||
`Attribute ${relationship.name} already exists in ${this.name}` | ||
); | ||
} | ||
this.relationships.set(relationship.name, relationship); | ||
} | ||
|
||
public findAttribute(name: string): Attribute | undefined { | ||
return this.attributes.get(name); | ||
} | ||
|
||
public findRelationship(name: string): Relationship | undefined { | ||
return this.relationships.get(name); | ||
} | ||
} |
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
64 changes: 64 additions & 0 deletions
64
packages/graphql/src/schema-model/entity/model-adapters/InterfaceEntityAdapter.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,64 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import type { Annotations } from "../../annotation/Annotation"; | ||
import type { Attribute } from "../../attribute/Attribute"; | ||
import { AttributeAdapter } from "../../attribute/model-adapters/AttributeAdapter"; | ||
import { RelationshipAdapter } from "../../relationship/model-adapters/RelationshipAdapter"; | ||
import type { Relationship } from "../../relationship/Relationship"; | ||
import type { ConcreteEntity } from "../ConcreteEntity"; | ||
import type { InterfaceEntity } from "../InterfaceEntity"; | ||
import { ConcreteEntityAdapter } from "./ConcreteEntityAdapter"; | ||
|
||
export class InterfaceEntityAdapter { | ||
public readonly name: string; | ||
public concreteEntities: ConcreteEntityAdapter[]; | ||
public readonly attributes: Map<string, AttributeAdapter> = new Map(); | ||
public readonly relationships: Map<string, RelationshipAdapter> = new Map(); | ||
public readonly annotations: Partial<Annotations>; | ||
|
||
constructor(entity: InterfaceEntity) { | ||
this.name = entity.name; | ||
this.concreteEntities = []; | ||
this.annotations = entity.annotations; | ||
this.initAttributes(entity.attributes); | ||
this.initRelationships(entity.relationships); | ||
this.initConcreteEntities(entity.concreteEntities); | ||
} | ||
|
||
private initConcreteEntities(entities: ConcreteEntity[]) { | ||
for (const entity of entities) { | ||
const entityAdapter = new ConcreteEntityAdapter(entity); | ||
this.concreteEntities.push(entityAdapter); | ||
} | ||
} | ||
|
||
private initAttributes(attributes: Map<string, Attribute>) { | ||
for (const [attributeName, attribute] of attributes.entries()) { | ||
const attributeAdapter = new AttributeAdapter(attribute); | ||
this.attributes.set(attributeName, attributeAdapter); | ||
} | ||
} | ||
|
||
private initRelationships(relationships: Map<string, Relationship>) { | ||
for (const [relationshipName, relationship] of relationships.entries()) { | ||
this.relationships.set(relationshipName, new RelationshipAdapter(relationship, this)); | ||
} | ||
} | ||
} |
Oops, something went wrong.