Apollo Graphql客户端-如何将新节点附加到现有节点



am试图使用Apollo客户端在graphql中持久化一个新节点。此新节点应连接到现有节点。不太确定如何进行。假设我有以下类型定义:

type Person{
personId: ID!
name: String!
user: User @relationship(type: "LOGS_IN_AS", direction: OUT)
}
type User{
userId: ID!
username: String!
password: String!
person: Person!
}

现在假设我已经在数据库中有一个人如下:

{
personId: 1,
name: "John Doe"
}

我如何为这个人突变相应的用户节点,并确保使用Apollo的自动生成突变创建必要的关系?顺便说一句,我使用的是neo4j后端。

提前感谢

不知道如何设置neo4j数据库,但假设User应该有一个personId来与Person链接。

您可能应该为突变定义一个单独的模式类型,称为UpsertUserInput或类似于下面的内容,这样neo4j就可以使用它来链接

type UpsertUserInput{
userId: ID!
personId: ID!
username: String!
password: String!
}

Appollo模式不负责定义数据的来源或存储方式。它完全与实现无关。https://www.apollographql.com/docs/apollo-server/schema/schema/#the-模式定义语言

我看到有一个解决方案,只需要更深入地研究工作室中自动生成的突变。沿着…路线的解决方案。。。

useMutation(CREATE_USER, { 
variables:{ 
input:{
userName: "johndoe@example.com", 
password: "jdoespassword", 
person:{ 
connect:{ 
where:{ 
node:{ 
personId: 1 
} 
} 
} 
} 
}
} 
}) 

最新更新