0

I need help building a GraphQL Api that wraps the ChuckNorris.io API

The API sholud have aQuery type that resolves all Categories (https://api.chuckmorris.io/jokes/categories)

The Api should have Query type that resolves a random joke given as an argument (https://api.chucknorris.io/jokes/random?category={category})

const express=require('express');
const {ApolloServer,gql}=require('apollo-server-express');
const fetch=require('node-fetch');

const typeDefs=gql`
type Joke{
    icon_url:String,
    id:String,
    url:String
    value: String
}

type Category{
    animal:String
    career:String
    celebrity:String
    dev:String
    explicit:String
    fashion:String
    food:String
    history:String
    money:String
    movie:String
    music:String
    political:Strig
    religion:String
    science:String
    sport:String
    travel:String
}

type Query{
    getCategory(category:String!):Joke
    category:Category
}

`

const resolvers={
  Query:{
 getCategory: async(_,{category})=>{
   const response=await fetch(`https://api.chucknorris.io/jokes/random?category=${category}`)
   return response.json();
  },
 category: async(_,{})=>{
   const response=await fetch('https://api.chucknorris.io/jokes/categories') 
   return response.json();
  }
  }
}


const server= new ApolloServer({typeDefs,resolvers});

const app=express();
server.applyMiddleware({app});

app.listen({port:4000},()=>
console.log('Now browse to http://localhost:4000' + server.graphqlPath)
)

1 Answers1

0

your query for type category should return a list of strings (array) so

export const typeDefs = gql`
    type Joke {
        value: String!
        id:ID!
        icon_url:String!
    }
    type Query {
        getAllCategories:[String!]!
        randomJoke(category: String!):Joke
    }
`;

for your resolver, you don't need fetch. apollo provides datasources to connect to external REST APIs like the one you have. so install the npm package "apollo-datasource-rest" and add it to your instance of apollo server like so

const server = new ApolloServer({
    typeDefs,
    resolvers,
    dataSources: ()=>({
        jokeinstance : new Jokenorris
    })
})

then create the datasource class for Jokenorris and import appropriately or do everything in one src file as you did.

import pkg from "apollo-datasource-rest";
const { RESTDataSource } = pkg;

export class Jokenorris extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = "https://api.chucknorris.io/jokes";
  }
  

  async getAllCategories() {
    const res = await this.get("categories");
    return res;
  }

  async getRandomJoke({ category }) {
    const response = await this.get("random", { category: category });
    return response;
  }
}

then your resolveer can look like so, you can ignore the exports and imports if you chunked everything in one file

 export  const resolvers = {
      Query: {
        allJokeCategories: (_, __, { dataSources }) =>
          dataSources.jokeinstance.getAllCategories(),
          randomJoke: (_, {category}, {dataSources})=>
          dataSources.jokeinstance.getRandomJoke({category:category})
      },
    };
Jude Bobinihi
  • 178
  • 1
  • 13