Resolver / Schema issue?

Hey! I’ve been playing around with Apollo GraphQL for couple of days now and it’s been great!

Today I started to fetch some data from a public event REST API. I’ve been successful at fetching / using some REST APIs previously while going through tutorials, but right now that I’ve started working on my own little thing, I feel like I’m a little stuck. Can anyone tell me why I’m getting INTERNAL_SERVER_ERROR errors and Expected Iterable, but did not find one for field \"Query.listEvents\ messages?
I assume there’s something wrong with my Schema and/or Resolver.

index.js

const { ApolloServer } = require('apollo-server');
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const EventAPI = require('./datasources/events');

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => {
    return {
      eventsAPI: new EventAPI(),
    };
  },
});

server.listen().then(({ url }) => {
  console.log(`🚀 Server ready at ${url}`);
});

datasources: events.js

class EventAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = 'https://api.turku.fi/linkedevents/v1/';
  }

  getEvents() {
    return this.get('event');
  }
}

resolvers.js

const resolvers = {
  Query: {
    listEvents: (_, __, { dataSources }) => {
      return dataSources.eventsAPI.getEvents();
    },
  },
};

schema.js

const typeDefs = gql`
  type Query {
    listEvents: [Event!]!
  }

  type Event {
    id: ID!
    event_status: String!
    start_time: String!
    end_time: String!
    info_url: String
  }
`;

Thank you for your time :slight_smile:

Hi! The error message is telling you that your listEvents field expects an array to be returned, but it’s not. Your data source is calling this.get('event') - the use of the singular makes me wonder if a single event object might be returned rather than an array of events?

2 Likes

Ah, thank you for the response!
I see. I did ponder if that might have been the case.
How the API serves the data might also cause some confusion.
Link to /event/

When I console log the response, it does come back with a "meta" object and a "data" array, the "data" array is populated with all the event information (same as when you go to /event/).

So, I assume I need to change how I handle that API response - “data” on my end?

Yes, your resolver needs to return exactly what the API defines. So I suppose you’ll have to await the REST response and return the data array, if that’s where the events are.

1 Like

Got it working!
Thank you so much for the help :grinning:

Solved.