Neo4j / GraphQL增强架构

时间:2018-07-24 15:41:14

标签: neo4j graphql apollo-server

我试图学习一些有关Neo4j的GraphQL集成的信息。我正在使用GrandStack启动器,可以在这里找到。 Grand Stack Starter。入门程序不会使用您在其他GrapQL应用程序中看到的大量样板文件,因为据我所知,Neo4j集成可以避免重复声明Schema和Resolvers的需要。它使用Apollo Server和“ augmentedSchema”代替。使用入门代码,我尝试向架构添加一些突变,在这种情况下,为deleteUser突变。我不断收到错误消息,说我只将Mutation ID放在一个位置的代码中,所以多次定义了Mutation ID。这是我的schema.js:

static void Main()
    {
        // Want to access Instance Name here
        HostFactory.Run(x =>
        {
            x.Service<Webserver>(s =>
            {
                s.ConstructUsing(name => {
                    return new Webserver(serviceName, name.InstanceName); });
                s.WhenStarted(tc => tc.Start());
                s.WhenStopped(tc => tc.Stop());
            });
            x.RunAsLocalSystem();
            x.SetServiceName(serviceName);
            x.SetDisplayName(serviceName);
            x.SetDescription("Evolution data access service");
        });
    }

这是我的索引:

    import { neo4jgraphql } from "neo4j-graphql-js";

export const typeDefs = `
type User {
  id: ID!
  name: String
  friends(first: Int = 10, offset: Int = 0): [User] @relation(name: "FRIENDS", direction: "BOTH")
  reviews(first: Int = 10, offset: Int = 0): [Review] @relation(name: "WROTE", direction: "OUT")
  avgStars: Float @cypher(statement: "MATCH (this)-[:WROTE]->(r:Review) RETURN toFloat(avg(r.stars))")
  numReviews: Int @cypher(statement: "MATCH (this)-[:WROTE]->(r:Review) RETURN COUNT(r)")
}

type Business {
  id: ID!
  name: String
  address: String
  city: String
  state: String
  reviews(first: Int = 10, offset: Int = 0): [Review] @relation(name: "REVIEWS", direction: "IN")
  categories(first: Int = 10, offset: Int =0): [Category] @relation(name: "IN_CATEGORY", direction: "OUT")
}

type Review {
  id: ID!
  stars: Int
  text: String
  business: Business @relation(name: "REVIEWS", direction: "OUT")
  user: User @relation(name: "WROTE", direction: "IN")
}

type Category {
  name: ID!
  businesses(first: Int = 10, offset: Int = 0): [Business] @relation(name: "IN_CATEGORY", direction: "IN")
}

type Query {
    users(id: ID, name: String, first: Int = 10, offset: Int = 0): [User]
    businesses(id: ID, name: String, first: Int = 10, offset: Int = 0): [Business]
    reviews(id: ID, stars: Int, first: Int = 10, offset: Int = 0): [Review]
    category(name: ID!): Category
    usersBySubstring(substring: String, first: Int = 10, offset: Int = 0): [User] @cypher(statement: "MATCH (u:User) WHERE u.name CONTAINS $substring RETURN u")
}

type Mutation {

    deleteUser(id: ID!): User
}

`;

export const resolvers = {
    Query: {
        users: neo4jgraphql,
        businesses: neo4jgraphql,
        reviews: neo4jgraphql,
        category: neo4jgraphql,
        usersBySubstring: neo4jgraphql
    },

    Mutation: {
        deleteUser: neo4jgraphql
    }
};

错误消息: enter image description here

启动器的其他代码均未更改。我无法在文档中找到关于增强模式的更多信息。如果有人能指出正确的方向,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

问题是由扩增架构引起的,该架构会为每种类型自动创建默认突变,在这种情况下,它已经创建了删除用户,更新用户和添加用户。您在运行GraphiQL服务器时可以看到此信息。因此,我曾两次宣布删除用户突变。