Vikas Roy
Vikas Roy

Reputation: 864

How to join two REST Data Source in Apollo GraphQL Server?

I am trying to join the responses from two different data sources in Apollo GraphQL Server. Basically, I am trying to join the responses from Google Nearby Search API and Google Place Details API.

Below is my code:

const { ApolloServer, gql } = require('apollo-server');
const { NearbySearchAPI } = require('./NearbySearchAPI');
const { PlaceDetailsAPI } = require('./PlaceDetailsAPI');

const typeDefs = gql`

  type Place {
      name: String,
      place_id: String,
      rating: Float,
      user_ratings_total: Int,
      place_detail: PlaceDetail

  } 

  type PlaceDetail {
    formatted_address: String,
    formatted_phone_number: String,
    international_phone_number: String,
    website: String
  }

  type Query {
    nearbyPlaces(location: String, radius: Int, type: String): [Place],
    placeDetail(place_id: String): PlaceDetail
  }

`;


const resolvers = {
    Query: {
      nearbyPlaces: async (parent, {location, radius, type}, { dataSources }) => {
        let response =  dataSources.nearbySearchAPI.getNearbyPlaces(location, radius, type);
        return response.then(data => data.results);
      },
      placeDetail: async (parent, {place_id}, {dataSources}) => {

        let response = dataSources.placeDetailAPI.getPlaceDetail(place_id);
        return response.then(data => data.result);

      } 
    },
  };

const server = new ApolloServer({ 
    typeDefs, 
    resolvers,
    dataSources: () => {
        return {
          nearbySearchAPI: new NearbySearchAPI(),
          placeDetailAPI: new PlaceDetailsAPI()
        }
    }, 
    context: () => {
        return {
            key: 'AIzaSXXXXXXXXXtzb_XvrBQh4I',
        };
    },
});

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



I have PlaceDetail type embedded in Place type. Both the queries are individually giving correct response but when I do nested query I am always getting PlaceDetail as null.

{
  nearbyPlaces(location:"-1.259733, 36.815877", radius: 2000, type: "restaurant"){
    place_id,
    name,
    rating,
    user_ratings_total,
    place_detail {
      formatted_address,
      formatted_phone_number,
      international_phone_number,
      website
    }
  }
}

Any idea where can the issue be?

Upvotes: 1

Views: 1201

Answers (1)

medicengonzo
medicengonzo

Reputation: 519

The Query resolver is correctly formed to return its root properties Query.nearbyPlaces and Query.placesDetail.

However Query.nearbyplaces.placesDetailsexpects to return a Place type and since there is no resolver for Place.placeDetail it returns null.

You need to define a new root resolver for type Place like so:

const resolvers = {
 Query: {
   ...QueryResolvers
  },
 Place: {
     placeDetail: async (root, args, { dataSources }) {
         
         // parent.place_id is the place_id property of each item to return  

         let response = dataSources.placeDetailAPI.getPlaceDetail(parent.place_id);
         return response.then(data => data.result);
     }
   }
}

Upvotes: 1

Related Questions