-
Notifications
You must be signed in to change notification settings - Fork 153
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 #5271 from neo4j/global-id-v6
Global id v6
- Loading branch information
Showing
11 changed files
with
366 additions
and
9 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...ages/graphql/src/api-v6/queryIRFactory/resolve-tree-parser/GlobalNodeResolveTreeParser.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,68 @@ | ||
/* | ||
* 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 { ResolveTree } from "graphql-parse-resolve-info"; | ||
import type { ConcreteEntity } from "../../../schema-model/entity/ConcreteEntity"; | ||
import { TopLevelResolveTreeParser } from "./TopLevelResolveTreeParser"; | ||
import type { GraphQLTreeReadOperation } from "./graphql-tree"; | ||
|
||
export class GlobalNodeResolveTreeParser extends TopLevelResolveTreeParser { | ||
constructor({ entity }: { entity: ConcreteEntity }) { | ||
super({ entity: entity }); | ||
} | ||
|
||
/** Parse a resolveTree into a Neo4j GraphQLTree */ | ||
public parseOperation(resolveTree: ResolveTree): GraphQLTreeReadOperation { | ||
const entityTypes = this.targetNode.typeNames; | ||
resolveTree.fieldsByTypeName[entityTypes.node] = { | ||
...resolveTree.fieldsByTypeName["Node"], | ||
...resolveTree.fieldsByTypeName[entityTypes.node], | ||
}; | ||
const node = resolveTree ? this.parseNode(resolveTree) : undefined; | ||
|
||
return { | ||
alias: resolveTree.alias, | ||
args: { | ||
where: { | ||
edges: { | ||
node: { | ||
id: { equals: resolveTree.args.id as any }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
name: resolveTree.name, | ||
fields: { | ||
connection: { | ||
alias: "connection", | ||
args: {}, | ||
fields: { | ||
edges: { | ||
alias: "edges", | ||
args: {}, | ||
fields: { | ||
node, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
packages/graphql/src/api-v6/resolvers/global-node-resolver.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,51 @@ | ||
import type { GraphQLResolveInfo } from "graphql"; | ||
import type { ConcreteEntity } from "../../schema-model/entity/ConcreteEntity"; | ||
import type { GlobalNodeArgs } from "../../types"; | ||
import type { Neo4jGraphQLTranslationContext } from "../../types/neo4j-graphql-translation-context"; | ||
import { execute } from "../../utils"; | ||
import getNeo4jResolveTree from "../../utils/get-neo4j-resolve-tree"; | ||
import { fromGlobalId } from "../../utils/global-ids"; | ||
import { parseGlobalNodeResolveInfoTree } from "../queryIRFactory/resolve-tree-parser/parse-resolve-info-tree"; | ||
import { translateReadOperation } from "../translators/translate-read-operation"; | ||
|
||
/** Maps the database id field to globalId */ | ||
export function generateGlobalNodeResolver({ globalEntities }: { globalEntities: ConcreteEntity[] }) { | ||
return async function resolve( | ||
_source, | ||
args: GlobalNodeArgs, | ||
context: Neo4jGraphQLTranslationContext, | ||
info: GraphQLResolveInfo | ||
) { | ||
const resolveTree = getNeo4jResolveTree(info, { args }); | ||
context.resolveTree = resolveTree; | ||
|
||
const { typeName, field, id } = fromGlobalId(args.id); | ||
if (!typeName || !field || !id) return null; | ||
|
||
const entity = globalEntities.find((n) => n.name === typeName); | ||
if (!entity) return null; | ||
|
||
const graphQLTree = parseGlobalNodeResolveInfoTree({ resolveTree: context.resolveTree, entity }); | ||
const { cypher, params } = translateReadOperation({ | ||
context: context, | ||
graphQLTree, | ||
entity, | ||
}); | ||
const executeResult = await execute({ | ||
cypher, | ||
params, | ||
defaultAccessMode: "READ", | ||
context, | ||
info, | ||
}); | ||
|
||
let obj = null; | ||
|
||
const thisValue = executeResult.records[0]?.this.connection.edges[0].node; | ||
|
||
if (executeResult.records.length && thisValue) { | ||
obj = { ...thisValue, id: args.id, __resolveType: entity.name }; | ||
} | ||
return obj; | ||
}; | ||
} |
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
Oops, something went wrong.