我如何使用一个查询来解决 graphQL 中的许多 Rest 往返?
How can I use one query to resolve many Rest round trips in graphQL?
我正在尝试使用 GraphQL 和 Apollo 解决 REST 查询。
我的其余数据是这样的
将基金 ID 传递给拆分解析器,其中 returns a -> [拆分数组] -> 每个拆分都有一个 donation_id -> 我想使用该 ID 来获取{捐赠} 来自单独的休息端点的对象
这是我的架构
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-tools 创建架构。此错误消息告诉您无法成功构建架构,因为 Splits
上的 donation
字段需要解析函数。你定义了 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;
}
},
};
我正在尝试使用 GraphQL 和 Apollo 解决 REST 查询。
我的其余数据是这样的
将基金 ID 传递给拆分解析器,其中 returns a -> [拆分数组] -> 每个拆分都有一个 donation_id -> 我想使用该 ID 来获取{捐赠} 来自单独的休息端点的对象
这是我的架构
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-tools 创建架构。此错误消息告诉您无法成功构建架构,因为 Splits
上的 donation
字段需要解析函数。你定义了 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;
}
},
};