GraphQL 遍历数组并获取所有结果

GraphQL loop through array and get all results

提问人:Elvira 提问时间:1/18/2018 更新时间:2/3/2023 访问量:13583

问:

我是 GraphQL 的新手。我正在使用亚马逊和 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 以及如何遍历 BookTypes。

也许有人可以帮助我或给我举个例子。

我的查询如下所示

{
  book(id:["9789025451608"]){
    amazon{
      title
    },
    itunes{
      title
    }
  }
}

并返回:

{
  "data": {
    "book": {
      "amazon": {
        "title": "De Moord op Commendatore"
      },
      "itunes": {
        "title": "Niet beschikbaar" // not available
      }
    }
  }
}

架构.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也使用相同的代码。

数组 列表 graphql graphql-js

评论

0赞 Everton Fernandes Rosario 7/20/2018
你是怎么离开这里的?
1赞 nealmcb 11/28/2018
这可能是相关的: 如何在 GraphQL 中使用数组作为参数查询对象列表 - Stack Overflow 尽管我想知道查询 Github 存储库列表是如何工作的,其中每个存储库都需要存储库的“所有者”和“名称”(比简单的 ID 列表复杂一些)

答:

-1赞 Benjamin Skinner 8/23/2021 #1

如果要返回多本书,则书籍字段的类型需要是 GraphQL 列表。

fields: () => ( {
    book: {
      type: new GrapQLList(BookType),
      args:{ id : { type: GraphQLString } },
      resolve (parentValue, args) {
       return resp = { id: args.id }
     }
    }