我想了解一点Neo4j的GraphQL集成。我使用的是GrandStack starter,可以在这里找到。Grand Stack Starter。starter没有使用您在其他GrapQL应用程序中看到的大量样板,因为据我所知,Neo4j集成应该绕过双重声明模式和解析器的需要。它使用的是Apollo Server和一个"augmentedSchema“。使用启动代码,我尝试向模式中添加一些突变,在本例中,就是deleteUser突变。我一直收到一个错误,说突变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
}
};这是我的索引:
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}`);
});错误消息:

starter中的任何其他代码都没有更改。我在文档中找不到太多关于augmentedSchema的信息。如果有人能给我指出正确的方向,我将不胜感激。
发布于 2018-08-10 05:00:42
这个问题是由augmentedSchema引起的,它会自动为每种类型创建缺省突变,即在本例中它已经创建了delete user、update user和add user。您可以在运行GraphiQL服务器时看到这一点。所以我双重声明了delete用户突变。
https://stackoverflow.com/questions/51502712
复制相似问题