使用Facebook的參考圖書館,我發現了一種方法破解泛型類型是這樣的:如何使用模式語言創建泛型?
type PagedResource<Query, Item> = (pagedQuery: PagedQuery<Query>) => PagedResponse<Item>
interface PagedQuery<Query> {
query: Query;
take: number;
skip: number;
}
interface PagedResponse<Item> {
items: Array<Item>;
total: number;
}
function pagedResource({type, resolve, args}) {
return {
type: pagedType(type),
args: Object.assign(args, {
page: { type: new GraphQLNonNull(pageQueryType()) }
}),
resolve
};
function pageQueryType() {
return new GraphQLInputObjectType({
name: 'PageQuery',
fields: {
skip: { type: new GraphQLNonNull(GraphQLInt) },
take: { type: new GraphQLNonNull(GraphQLInt) }
}
});
}
function pagedType(type) {
return new GraphQLObjectType({
name: 'Paged' + type.toString(),
fields: {
items: { type: new GraphQLNonNull(new GraphQLList(type)) },
total: { type: new GraphQLNonNull(GraphQLInt) }
}
});
}
}
但我喜歡如何與阿波羅服務器,我可以聲明創建模式。所以問題是,你們如何利用模式語言來創建泛型類型?
這是否意味着客戶需要弄清楚返回哪種類型? – Birowsky
是的,客戶端將不得不使用內聯片段。不知道是否有一種很好的方式來執行您正在嘗試完成的任務,而無需編程生成模式。看到我的編輯一些額外的想法。 –
欣賞努力 – Birowsky