arrays - GraphQL 循环遍历数组并获取所有结果

标签 arrays list graphql graphql-js

我是 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/

相关文章:

javascript - 如何从 Node js 查询 GraphQl 端点而不需要笨重的字符串操作?

java - 如何修改 String[] 内的元素,该元素位于另一个 Arraylist 内的 Arraylist 内

C++ 指向数组的指针并访问?

javascript - 当有多个值时,如何使我的过滤器作为 "and"而不是 "or"工作?

python - 值错误 : Unknown format code 'f' for object of type 'str'

Java 使列表线程的副本安全吗?

python - 如何获得两个双元素子列表列表之间的对称差异?

reactjs - React/Apollo - 不同组件中的类似查询?

python - 根据给定值查找列表中字典项的索引号

javascript - 创建多变量 GraphQL 查询