GraphQL loop through array and get all results
Asked Answered
C

1

14

I'm new to GraphQL. I'm using API's from Amazon and Itunes to get the title (and other information) of a book. I'm returning an object like this:

var data = [];
data.title = results[0].title;
data.author = results[0].author;
return data;

I can make calls to the Amazon and Itunes API and return the available data of a book. However I would like to be able to insert an array of EAN/ISBN, and return data from Amazon and Itunes for all the books.

So the query would become something like this:

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

And the response:

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

I've searched for examples of the use of graphQLList, but I'm unsure where to use a graphQLList and how to loop through the BookTypes.

Maybe someone can help me or give me an example.

My query looks like this

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

and returns:

{
  "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
});

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;

Booktype

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;

AmazonType

const graphql = require('graphql');

const{
  GraphQLObjectType,
  GraphQLString,
  GraphQLList
} = graphql;


const AmazonType = new GraphQLObjectType({
    name: 'amazon',
    fields: () => ( {
        title: { type: GraphQLString },
    })
});

module.exports = AmazonType;

Same kind of code goes for ItunesType.

Crean answered 18/1, 2018 at 12:44 Comment(2)
What was your way out here?Pacha
This may be relevant: How to query list of objects with array as an argument in GraphQL - Stack Overflow though I'd like to know how it would work to query a list of Github repositories, where each one requires both an "owner" and a "name" for the repo (a bit more complicated than a simple list of ids)Mum
E
-1

If you want to return multiple books the type of the book field needs to be a GraphQL List.

fields: () => ( {
    book: {
      type: new GrapQLList(BookType),
      args:{ id : { type: GraphQLString } },
      resolve (parentValue, args) {
       return resp = { id: args.id }
     }
    }
Estrade answered 22/8, 2021 at 22:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.