1
如何從回調函數返回值並將其傳遞給Graphql中的解析函數?如何在Graphql解析函數中從回調中返回值?
這裏的虛擬代碼來顯示概念:
此函數運行SQL查詢:
function runQuery(query, cb){
....
var value = "Something";
cb(null, value);
}
這需要從回調函數的值傳遞它graphql解決功能:
function getTitle() {
return runQuery("...", function(err, value){
return value;
});
}
Graphql模式:
var SampleType = new GraphQLObjectType({
name: 'Sample',
fields:() => ({
title: { type: GraphQLString },
}),
});
query: new GraphQLObjectType({
name: 'Query',
fields:() => ({
sample: {
type: SampleType,
resolve:() => getTitle(),
},
}),
}),
謝謝..它工作:) – Shaz