0

I'm completely new to GraphQL and I'm trying to set up a schema and trying it out as I request data from the cocktailDB API. I tried setting up in the same way as Traversy did in a tutorial where he's accessing the spaceX API. I think the error has to do with the cocktailDB returning a slightly different response than the spaceX API does. My schema looks like this:

// Cocktail Type
const CocktailType = new GraphQLObjectType({
    name   : 'Cocktail',
    fields : () => ({
        idDrink         : { type: GraphQLString },
        strDrink        : { type: GraphQLString },
        strAlcoholic    : { type: GraphQLString },
        strGlass        : { type: GraphQLString },
        strInstructions : { type: GraphQLString },
        strDrinkThumb   : { type: GraphQLString }
    })
});

// Root Query
const RootQuery = new GraphQLObjectType({
    name   : 'RootQueryType',
    fields : {
        cocktails : {
            type    : new GraphQLList(CocktailType),
            resolve(parent, args) {
                return axios.get('https://www.thecocktaildb.com/api/json/v1/1/search.php?f=a').then((res) => res.data);
            }
        }
    }
});

module.exports = new GraphQLSchema({
    query : RootQuery
});

The response from the cocktailDB looks like this (if I send a get request from postman):

{
    "drinks": [
        {
            "idDrink": "17222",
            "strDrink": "A1",
            "strDrinkAlternate": null,
            "strDrinkES": null,
            "strDrinkDE": null,
            "strDrinkFR": null,
            "strDrinkZH-HANS": null,
            "strDrinkZH-HANT": null,
            "strTags": null,
            "strVideo": null,
            "strCategory": "Cocktail",
            "strIBA": null,
            "strAlcoholic": "Alcoholic",
            "strGlass": "Cocktail glass",
            "strInstructions": "Pour all ingredients into a cocktail shaker, mix and serve over ice into a chilled glass.",
            "strInstructionsES": null,
            "strInstructionsDE": "Alle Zutaten in einen Cocktailshaker geben, mischen und über Eis in ein gekühltes Glas servieren.",
            "strInstructionsFR": null,
            "strInstructionsZH-HANS": null,
            "strInstructionsZH-HANT": null,
            "strDrinkThumb": "https://www.thecocktaildb.com/images/media/drink/2x8thr1504816928.jpg",
            "strIngredient1": "Gin",
            "strIngredient2": "Grand Marnier",
            "strIngredient3": "Lemon Juice",
            "strIngredient4": "Grenadine",
            "strIngredient5": null,
            "strIngredient6": null,
            "strIngredient7": null,
            "strIngredient8": null,
            "strIngredient9": null,
            "strIngredient10": null,
            "strIngredient11": null,
            "strIngredient12": null,
            "strIngredient13": null,
            "strIngredient14": null,
            "strIngredient15": null,
            "strMeasure1": "1 3/4 shot ",
            "strMeasure2": "1 Shot ",
            "strMeasure3": "1/4 Shot",
            "strMeasure4": "1/8 Shot",
            "strMeasure5": null,
            "strMeasure6": null,
            "strMeasure7": null,
            "strMeasure8": null,
            "strMeasure9": null,
            "strMeasure10": null,
            "strMeasure11": null,
            "strMeasure12": null,
            "strMeasure13": null,
            "strMeasure14": null,
            "strMeasure15": null,
            "strCreativeCommonsConfirmed": "No",
            "dateModified": "2017-09-07 21:42:09"
        }, {some other drink}, {some other drink}, etc
]
}

I get this message "Expected Iterable, but did not find one for field RootQueryType.cocktails." when I check the schema in GraphiQL with

{
  cocktails {
    strDrink
  }
}

I'd really appreciate if someone can help me to correct the schema so that I get the correct response.

ZeGeR
  • 51
  • 3
  • 10
  • Your type for the `cocktails` field is a list, but your resolver doesn't return one. It returns an object with a single property named `drinks`. – Daniel Rearden Jan 01 '20 at 21:27
  • Thank you for your reply! However I still don't understand how I can change my schema so I can access the fields in cocktails, such as strDrink and strGlass. – ZeGeR Jan 01 '20 at 22:29
  • Return just the `drinks` property instead of the entire response – Daniel Rearden Jan 01 '20 at 23:04
  • Oh, of course. That's it. Thank you so much! It works when I specify that in the response. – ZeGeR Jan 02 '20 at 01:16

0 Answers0