gpt4 book ai didi

neo4j-graphql-js:使用@cypher 满足类型而不返回具体节点

转载 作者:行者123 更新时间:2023-12-04 08:40:28 25 4
gpt4 key购买 nike

我经常想创建一个 GraphQL 类型,它表示跨越一个节点的 Cypher 查询的结果。我无法从 @cypher 返回具体节点在这种情况下,因为不存在这样的节点。我试图从顶级 @cypher 返回适当命名的字段查询,但这种方法不起作用。

import { makeAugmentedSchema, neo4jgraphql } from 'neo4j-graphql-js';
import { ApolloServer } from 'apollo-server';
import neo4j from 'neo4j-driver';

const typeDefs = `
type Person {
name: String
age: Int
}

type Query {
persons: [Person] @cypher(
statement: """
WITH [["foo", 42], ["bar", 43]] AS x UNWIND x AS y
RETURN y[0] AS name, y[1] AS age
"""
)
}
`;

const driver = neo4j.driver(
'bolt://localhost:7687',
neo4j.auth.basic('neo4j', 'password')
);


const resolvers = {
};

const schema = makeAugmentedSchema({ typeDefs, resolvers });


const server = new ApolloServer(
{
schema,
resolvers,
context: {driver}
}
)


server.listen(4000, '0.0.0.0').then(({ url }) => {
console.log(`GraphQL API ready at ${url}`);
});
询问:
{
persons {
name
age
}
}
产生错误:
{
"errors": [
{
"message": "String(\"foo\") (of class org.neo4j.values.storable.StringWrappingStringValue)",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"persons"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"code": "Neo.DatabaseError.General.UnknownError",
"name": "Neo4jError",
"stacktrace": [
"Neo4jError: String(\"foo\") (of class org.neo4j.values.storable.StringWrappingStringValue)",
"",
" at captureStacktrace (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/result.js:277:15)",
" at new Result (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/result.js:68:19)",
" at newCompletedResult (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/transaction.js:449:10)",
" at Object.run (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/transaction.js:287:14)",
" at Transaction.run (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-driver/lib/transaction.js:123:32)",
" at _callee2$ (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/neo4j-graphql-js/dist/index.js:222:35)",
" at tryCatch (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/regenerator-runtime/runtime.js:63:40)",
" at Generator.invoke [as _invoke] (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/regenerator-runtime/runtime.js:293:22)",
" at Generator.next (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/regenerator-runtime/runtime.js:118:21)",
" at asyncGeneratorStep (/home/amoe/dev/neo4j-graphql-js-return-aggregate-type/node_modules/@babel/runtime-corejs2/helpers/asyncToGenerator.js:5:24)"
]
}
}
}
],
"data": {
"persons": null
}
}
我知道我可以通过每个字段来满足单个字段 @cypher注释,这不适合这种情况。这个问题是关于满足整个结果类型。
如果答案需要在 resolvers 中使用自定义处理程序数组,这也很好,只要我可以在单个查询中收集满足类型所需的数据。或者,如果这是不可能的,那也是有用的信息。

最佳答案

问题是根据Person定义,查询输出处必须有一个对象。所以试试这个查询

WITH [["foo", 42], ["bar", 43]] AS x UNWIND x AS y
RETURN { name: y[0], age: y[1] } as Person

关于neo4j-graphql-js:使用@cypher 满足类型而不返回具体节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64594151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com