Neo4j/GraphQL 增强模式
Posted
技术标签:
【中文标题】Neo4j/GraphQL 增强模式【英文标题】:Neo4j/GraphQL augmented Schema 【发布时间】:2019-01-01 07:37:19 【问题描述】:我正在尝试了解 Neo4j 的 GraphQL 集成。我正在使用可以在此处找到的 GrandStack 启动器。 Grand Stack 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
;
这是我的索引:
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`);
);
错误信息:
启动器中没有其他代码被更改。我无法在文档中找到有关 augmentedSchema 的太多信息。如果有人能指出我正确的方向,我将不胜感激。
【问题讨论】:
【参考方案1】:问题是由augmentedSchema 引起的,它会自动为每种类型创建默认突变,即在这种情况下,它已经创建了删除用户、更新用户和添加用户。您可以在运行 GraphiQL 服务器时看到这一点。所以我双重声明删除用户突变。
【讨论】:
以上是关于Neo4j/GraphQL 增强模式的主要内容,如果未能解决你的问题,请参考以下文章
React Native 上的 GraphQL Apollo 类型生成