Neo4j/GraphQL 增强架构

Mic*_*ter 4 neo4j graphql apollo-server

我正在尝试了解 Neo4j 的 GraphQL 集成。我正在使用可以在此处找到的 GrandStack starter。大堆栈启动器。starter 没有使用您在其他 GrapQL 应用程序中看到的很多样板,因为据我了解,Neo4j 集成应该绕过双重声明 Schema 和 Resolvers 的需要。它使用 Apollo Server 和“augmentedSchema”代替。使用入门代码,我尝试向架构添加一些更改,在本例中为 deleteUser 更改。我一直收到一个错误,说 Mutation id 定义了不止一次,当我知道我只将它放在代码中的一个地方时。这是我的 schema.js:

    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
    }
};
Run Code Online (Sandbox Code Playgroud)

这是我的索引:

import { typeDefs, resolvers } from "./graphql-schema";
import { ApolloServer, gql, makeExecutableSchema } from "apollo-server";
import { v1 as neo4j } from "neo4j-driver";
import { augmentSchema } from "neo4j-graphql-js";
import dotenv from "dotenv";

dotenv.config();

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

// augmentSchema will add autogenerated mutations based on types in schema
const augmentedSchema = augmentSchema(schema);

const driver = neo4j.driver(
    process.env.NEO4J_URI || "bolt://localhost:7687",
    neo4j.auth.basic(
    process.env.NEO4J_USER || "neo4j",
    process.env.NEO4J_PASSWORD || "neo4j"
    )
);

if (driver){
    console.log("Database Connected")
} else{
    console.log("Database Connection Error")
}

const server = new ApolloServer({
  // using augmentedSchema (executable GraphQLSchemaObject) instead of typeDefs and resolvers
  //typeDefs,
  //resolvers,
  context: { driver },
  // remove schema and uncomment typeDefs and resolvers above to use original (unaugmented) schema
  schema: augmentedSchema
});

server.listen(process.env.GRAPHQL_LISTEN_PORT, '0.0.0.0').then(({ url }) => {
  console.log(`GraphQL API ready at ${url}`);
});
Run Code Online (Sandbox Code Playgroud)

错误信息: 在此处输入图片说明

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

Mic*_*ter 5

该问题是由增强架构引起的,它自动为每种类型创建默认突变,即在这种情况下,它已经创建了删除用户、更新用户和添加用户。您可以在运行 GraphiQL 服务器时看到这一点。所以我双重声明了删除用户突变。