我是 GraphQL 的新手。我正在使用来自 Amazon 和 Itunes 的 API 来获取一本书的标题(和其他信息)。我正在返回一个这样的对象:
var data = [];
data.title = results[0].title;
data.author = results[0].author;
return data;
我可以调用 Amazon 和 Itunes API 并返回一本书的可用数据。但是,我希望能够插入 EAN/ISBN 数组,并从 Amazon 和 Itunes 返回所有书籍的数据。
所以查询会变成这样:
{
book(id:["9789025451608", "8974832789431","9789024576791"]){
amazon{
title
},
itunes{
title
}
}
}
和回应:
{
"data": {
"book": {
"book1":{
"amazon": {
"title": "De Moord op Commendatore"
},
"itunes": {
"title": "Niet beschikbaar" // not available
}
},
"book2":{
"amazon": {
"title": "Origin"
},
"itunes": {
"title": "Origin"
}
}
}
}
}
我已经搜索了使用 graphQLList 的示例,但我不确定在哪里使用 graphQLList 以及如何遍历 BookType。
也许有人可以帮助我或给我一个例子。
我的查询看起来像这样
{
book(id:["9789025451608"]){
amazon{
title
},
itunes{
title
}
}
}
并返回:
{
"data": {
"book": {
"amazon": {
"title": "De Moord op Commendatore"
},
"itunes": {
"title": "Niet beschikbaar" // not available
}
}
}
}
Schema.js
"use strict";
const graphql = require('graphql');
const axios = require('axios');
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLList
} = graphql;
// root queries
const RootQuery = require('./types/query');
module.exports = new GraphQLSchema({
query: RootQuery
});
根查询:
const graphql = require('graphql');
const axios = require('axios');
const {
GraphQLObjectType,
GraphQLString,
GraphQLList,
GraphQLInt
} = graphql;
const BookType = require('../types/book');
const RootQuery = new GraphQLObjectType({
name:'RootQuery',
fields: () => ( {
book: {
type: BookType,
args:{ id : { type: GraphQLString } },
resolve (parentValue, args) {
return resp = { id: args.id }
}
}
})
});
module.exports = RootQuery;
书型
const graphql = require('graphql');
const axios = require('axios');
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt
} = graphql;
// Itunes
const itunes = require('../connections/itunes');
const ItunesType = require('./itunes');
// Amazon
const amazon = require('../connections/amazon');
const AmazonType = require('./amazon');
const BookType = new GraphQLObjectType({
name:'book',
fields: () => ( {
id: {
type: GraphQLString,
},
itunes: {
type: ItunesType,
resolve(parentValue,args){
console.log(parentValue);
data = itunes.getMetadataItunes(parentValue.id);
return data;
}
},
amazon: {
type: AmazonType,
resolve(parentValue, args) {
console.log(parentValue);
data = amazon.getMetadataAmazon(parentValue.id);
return data;
}
}
})
});
module.exports = BookType;
亚马逊类型
const graphql = require('graphql');
const{
GraphQLObjectType,
GraphQLString,
GraphQLList
} = graphql;
const AmazonType = new GraphQLObjectType({
name: 'amazon',
fields: () => ( {
title: { type: GraphQLString },
})
});
module.exports = AmazonType;
相同类型的代码适用于 ItunesType。
最佳答案
如果你想返回多本书,book 字段的类型需要是一个 GraphQL 列表。
fields: () => ( {
book: {
type: new GrapQLList(BookType),
args:{ id : { type: GraphQLString } },
resolve (parentValue, args) {
return resp = { id: args.id }
}
}
关于arrays - GraphQL 循环遍历数组并获取所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48321689/