作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 GraphQL 的新手。我正在使用来自 Amazon 和 Itunes 的 API 来获取一本书的标题(和其他信息)。我正在返回一个这样的对象:
var data = [];
data.title = results[0].title;
data.author = results[0].author;
return data;
{
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"
}
}
}
}
}
{
book(id:["9789025451608"]){
amazon{
title
},
itunes{
title
}
}
}
{
"data": {
"book": {
"amazon": {
"title": "De Moord op Commendatore"
},
"itunes": {
"title": "Niet beschikbaar" // not available
}
}
}
}
"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;
最佳答案
如果你想返回多本书,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/
我是一名优秀的程序员,十分优秀!