[英]how can I fetch data from graphql in my resolver
在我的解析器中,我似乎無法獲取連接的數據
這適用於 graphql 游樂場(prisma),但我不確定有關如何在 apollo 服務器中形成解析器的語法
// my typedef for activity is
type Activity {
id: ID! @id
ActivityType: ActivityType!
title: String!
date: DateTime
user: User!
distance: Float!
distance_unit: Unit!
duration: Float!
elevation: Float
elevation_unit: Unit
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
// and my resolver currently looks like this
async activity(parent, args, ctx, info) {
const foundActivity = await ctx.db.query.activity({
where: {
id: args.id
}
});
// todo fetch user data from query
console.log(foundActivity);
}
// where db has been placed onto the ctx (context)
// the CL gives all of the data for Activity apart from the user
// in the playground I would do something like this
query activity {
activity(where: {
id: "cjxxow7c8si4o0b5314f9ibek"
}){
title
user {
id
name
}
}
}
// but I do not know how to specify what is returned in my resolver.
console.log(foundActivity) gives:
{ id: 'cjxxpuh1bsq750b53psd2c77d',
ActivityType: 'CYCLING',
title: 'Test Activity',
date: '2019-07-10T20:21:27.681Z',
distance: 13.4,
distance_unit: 'KM',
duration: 90030,
elevation: 930,
elevation_unit: 'METERS',
createdAt: '2019-07-10T20:48:50.879Z',
updatedAt: '2019-07-10T20:48:50.879Z' }
Prisma 是 DB ORM,然后我有一個 Apollo-Server 2 服務器運行在它之上。 不幸的是,堆棧溢出也認為這篇文章中有太多代碼,所以我將不得不因為他們的系統無法處理這些無關緊要的胡言亂語而胡思亂想。
您必須為Activity.user
實現一個解析器。 不幸的是,您的實體似乎不包含對用戶的引用。 首先,將用戶連接添加到您的 Prisma 數據模型。 然后為Activity.user
實現一個解析器。 我對 Prisma 1 不是很熟悉,但是這個簡單的實現應該已經可以做你想做的了:
let resolvers = {
Query: {
// ...
},
Activity: {
user(parent, args, ctx) {
return ctx.db.query.activity({ id: parent.id }).user();
}
}
}
在此處了解有關解決 Prisma 關系的更多信息
所以答案非常簡單:我只是向查詢添加第二個參數(在“where”之后,帶有要返回的數據形狀的 gql 標記,因此我的代碼現在看起來像:
const foundActivity = await ctx.db.query.activity(
{
where: {
id: args.id
}
},
`{id title user { id name }}`
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.