首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ne4j-graphql-js:使用@cypher满足类型,而不返回具体节点

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

Stack Overflow用户
提问于 2020-10-29 23:22:49
回答 1查看 130关注 0票数 2

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

代码语言:javascript
复制
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}`);
});

查询:

代码语言:javascript
复制
{
  persons {
    name
    age
  }
}

产生以下错误:

代码语言:javascript
复制
{
  "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数组中使用自定义处理程序,也可以,只要我可以收集满足单个查询中的类型所需的数据。或者,如果这是不可能的,那也是有用的信息。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-31 18:37:58

问题在于,根据Person定义,查询输出处必须有一个对象。因此,请尝试以下查询

代码语言:javascript
复制
WITH [["foo", 42], ["bar", 43]] AS x UNWIND x AS y
RETURN { name: y[0], age: y[1] } as Person
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64594151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档