如何使用一个查询来解析graphQL中的多个Rest往返?
我正在尝试使用GraphQL和Apollo解决REST查询 我的rest数据如下所示 将基金id传递给splits解析器,该解析器返回->[splits array]->每个拆分都有一个捐赠\u id->我想使用该id从单独的rest端点获取{捐赠}对象 这是我的模式如何使用一个查询来解析graphQL中的多个Rest往返?,rest,graphql,apollo-server,Rest,Graphql,Apollo Server,我正在尝试使用GraphQL和Apollo解决REST查询 我的rest数据如下所示 将基金id传递给splits解析器,该解析器返回->[splits array]->每个拆分都有一个捐赠\u id->我想使用该id从单独的rest端点获取{捐赠}对象 这是我的模式 export const schema = [` schema { query: RootQuery } type RootQuery { Splits(id: Int!): [Splits] } type Spli
export const schema = [`
schema {
query: RootQuery
}
type RootQuery {
Splits(id: Int!): [Splits]
}
type Splits {
amount_in_cents:Int
donation_id:Int
fund_id:Int
id:Int
memo:String
donation(donation_id: Int): Donation
}
type Donation {
amount_in_cents: Int
bank_name: String
}
`];
这是我的解析器文件
import rp from 'request-promise';
const DTBaseURL = 'https://restendpointdomainhere.com/';
const getFromDT = (getQuery) => {
const data = rp( DTBaseURL + getQuery, {
'auth': {
"user": Meteor.settings.endpoint.user,
"pass": Meteor.settings.endpoint.pass,
}
} )
.then( ( res ) => JSON.parse( res ) )
.then((res) =>{
return res;
});
return data;
};
export default resolveFunctions = {
RootQuery: {
Splits( root, args, context ){
let newValue = [];
const getQuery = 'funds/' + args.id + '/splits.json';
const data = getFromDT(getQuery)
.then( ( res ) => {
res.forEach( function ( donationSplit ) {
newValue.push( donationSplit.split );
} );
return newValue;
} );
return data;
},
donation( root, args, context ){
console.log( args );
const getQuery = 'donations/' + args.donation_id + '.json';
const data = getFromDT(getQuery)
.then((res) => {
return res.donation;
});
return data;
}
}
};
我执行的任何查询@/graphql都会收到此错误消息
{
"errors": [
{
"message": "Resolve function missing for \"Splits.donation\""
}
]
}
这里的任何帮助都是感激的 看起来您正在使用graphql工具创建模式。此错误消息告诉您无法成功构建架构,因为
拆分
上的捐赠
字段需要解析函数。您定义了resolve函数,但将其放在RootQuery
中,而不是它所属的Splits
中:
export default resolveFunctions = {
RootQuery: {
Splits( root, args, context ){
let newValue = [];
const getQuery = 'funds/' + args.id + '/splits.json';
const data = getFromDT(getQuery)
.then( ( res ) => {
res.forEach( function ( donationSplit ) {
newValue.push( donationSplit.split );
} );
return newValue;
} );
return data;
},
},
Splits: { // <<---- you forgot this ------
donation( root, args, context ){
console.log( args );
const getQuery = 'donations/' + args.donation_id + '.json';
const data = getFromDT(getQuery)
.then((res) => {
return res.donation;
});
return data;
}
},
};
导出默认解析函数={
根查询:{
拆分(根、参数、上下文){
设newValue=[];
const getQuery='funds/'+args.id+'/splits.json';
常量数据=getFromDT(getQuery)
。然后((res)=>{
res.forEach(功能(捐赠分割){
newValue.push(donationSplit.split);
} );
返回新值;
} );
返回数据;
},
},
Splits:{//谢谢。现在我如何允许捐赠id从查询响应传递到此解析器?以下是我在GraphiQL{Splits(id:71455){amount\u in\u cents捐赠id捐赠(捐赠id:捐赠id){amount\u in\u cents}generation
解析器将获取从Splits
解析器返回的对象作为第一个参数,以及所有GraphQL参数(包括generation\u id)作为第二个参数。从您的描述来看,您甚至不需要字段中的捐赠id参数,因为每个拆分都有一个捐赠,因此捐赠id可以是拆分对象的一部分,捐赠解析程序将其作为第一个参数获取。