我正在尝试在非常基本的事情上玩 arroud APOLO SERVER、GRAPHQL PRISMA 和 struct .. 我无法从简单查询中返回关系数据..
当我尝试添加用户以及角色 ID 时.. 我无法取回角色信息
我在用什么?
"dependencies": {
"apollo-server": "^2.9.3",
"bcryptjs": "^2.4.3",
"graphql": "^14.5.5",
"graphql-import": "^0.7.1",
"jsonwebtoken": "^8.5.1",
"prisma-client-lib": "^1.34.8"
}
对于我的简单查询;
mutation{
createUser(name:"tst",
roleId:"ck0lx425n00z50782jgcl4qg8")
{
name
role{
code}
Graphql返回错误说
{
"errors": [
{
"message": "Cannot return null for non-nullable field User.role.",
"locations": [
{
"line": 4,
"column": 5
}
],
"path": [
"createUser",
"role"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"exception": {
"stacktrace": [
"Error: Cannot return null for non-nullable field User.role.",
GAPHQL SHCHEMA
scalar DateTime
type User {
id: ID!
name: String!
role:Role!
createdAt:DateTime
}
type Role{
id:ID!,
code:String!
users:[User!]!
}
type Query {
users:[User!]!
user:User!
roles:[Role!]!
role:Role!
}
type Mutation {
createRole(code:String!):Role!
createUser(name:String!,roleId:String!):User!
}
询问
function users(parent, args, context) {
return context.prisma.uses()
}
function roles(parent,args,context){
return context.prisma.roles()
}
突变
function createRole(parent,args,context,info){
return context.prisma.createRole({
code:args.code
});
function createUser(parent,args,context,info){
return context.prisma.createUser({
name:args.name,
role:{
connect:{id:args.roleId}
}
},info);
}
棱镜数据模型
type User {
id: ID! @id
name:String!
createdAt:DateTime @createdAt
role:Role! @relation(Name:"Role Assinged To User")
}
type Role{
id:ID! @id
code:String!
users:[User!]! @relation(Name:"Role Assinged To User")
}
有人可以帮助我吗,如何解决这个问题..我知道这将是我错过的基本问题..我很难理解?我错过了一些非常基本的东西吗?