当我在 prisma admin 中查看文档时,我看到了一个location
字段,但是当我对该数据库运行查询时,这些值在前端始终为空,并且解析器根本不会返回。
我看过Why does a GraphQL query return null? 但据我所知,这些案例都没有匹配或说明根本问题。
我一直在尝试理解这篇文章,但无法实现这样我可以编写一个实际触发的查询: https ://www.prisma.io/tutorials/a-guide-to-common-解析器模式-ct08/
该设置与 Web Bos 高级反应课程相匹配,如下所示:
创建 React 应用程序 -> Apollo-client -> Yoga 服务器 -> Prisma Server (sql)
我反复使用输入类型,将解析器简化到最低限度,只查询单个文档而不是文档数组。我宁愿不追求的选项是使用 Json 标量类型并跳过嵌套结构以支持平面结构。
前端:
const CREATE_MISSION = gql`
mutation CREATE_MISSION($title: String!, $description: String!, $bounty: Int!, $image: String, $postedBy: String!, $location: LocationInput) {
createMission(title: $title, description: $description, bounty: $bounty, image: $image, postedBy: $postedBy, location: $location) {
id
title
bounty
image
description
postedBy
location {
id
lat
lng
}
}
}
`;
架构.graphql:
input LocationInput {
lat: Float!
lng: Float!
}
type Mutation {
createMission(
title: String!,
description: String!,
bounty: Int!,
image: String,
postedBy: String!,
location: LocationInput
): Mission!
}
数据模型.graphql:
type Location {
id: ID! @id
lat: Float!
lng: Float!
}
type Mission {
id: ID! @id
title: String!
description: String!
bounty: Int!
image: String
postedBy: String!
location: Location @relation(name: "MissionStart")
end: Location @relation(name: "MissionEnd")
dispatcher: User @relation(name: "Dispatcher")
activeUser: User @relation(name: "Active")
appliedUsers: [User] @relation(name: "Applicants")
}
突变.js
async createMission (parent, args, ctx, info) {
console.log(`Create Mission server side`)
// let { input } = args
console.dir(args)
const mission = await ctx.db.mutation.createMission({
data: {
...args,
dispatcher: {
connect: {
id: args.postedBy
},
},
location: {
create: {
lat: args.location.lat,
lng: args.location.lng
}
}
},
}, info);
return mission
},
游乐场查询:
{
mission (id:"someid") {
title
description
location {
lat
}
}
location(id:"id_asPerTheDB ") {
lat
}
}
返回:
{
"data": {
"mission": {
"title": "Take me the airport",
"description": "so I can fly",
"location": null
},
"location": {
"lat": 30.2255867
}
}
}
根据操场上的返回值,我无法通过简单的嵌套查询将该位置从数据库中取出。我试过使用以下解析器(后端查询),但这总是null
有m.location
async missions (parent, args, ctx, info) {
const missions = await ctx.db.query.missions()
return Promise.all(missions.map(async m => {
if (m.location) {
const location = await ctx.db.query.location({where: { id: m.location.id }})
return Object.assign(m, { location })
} else return m
}));
},