【问题标题】:How to join two REST Data Source in Apollo GraphQL Server?如何在 Apollo GraphQL Server 中加入两个 REST 数据源?
【发布时间】:2020-11-19 00:51:19
【问题描述】:

我正在尝试在 Apollo GraphQL Server 中加入来自两个不同数据源的响应。基本上,我正在尝试加入来自 Google Nearby Search API 和 Google Place Details API 的响应。

下面是我的代码:

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}`);
});



我在 Place 类型中嵌入了 PlaceDetail 类型。这两个查询都分别给出了正确的响应,但是当我进行嵌套查询时,我总是将 PlaceDetail 设为 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
    }
  }
}

知道问题出在哪里吗?

【问题讨论】:

    标签: graphql apollo-server


    【解决方案1】:

    查询解析器的格式正确,可以返回其根属性Query.nearbyPlacesQuery.placesDetail

    但是 Query.nearbyplaces.placesDetails 期望返回 Place 类型,并且由于 Place.placeDetail 没有解析器,它返回 null。

    您需要为type Place 定义一个新的根解析器,如下所示:

    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);
         }
       }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-16
      • 2018-10-04
      • 2020-09-29
      • 2018-07-26
      • 2018-11-11
      • 2019-12-28
      • 2020-02-15
      • 2022-12-02
      • 2017-05-24
      相关资源
      最近更新 更多