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.