Apollo服务器数据源定义生成错误

时间:2018-10-26 04:50:01

标签: graphql apollo-server

使用Apollo graphql服务器创建REST数据源时遇到困难 我有一个data.js文件来定义REST数据源,如下所示:

    const { RESTDataSource } = require('apollo-datasource-rest');

    class MyAPI extends RESTDataSource {
        constructor() {
            super();
            this.baseURL = 'https://my-end-point;
        }

        async fetchData(what) {
           return this.get(`myparam`);
        }
   }

然后将其导入到resolver.js中,如下所示:

const myAPI = require('./data');
export const resolvers = {
     Query: {
          field () => {
             return myAPI.fetchData(param);
           }
     }
}

运行此命令时出现以下错误:

myAPI.fetchData is not a function

一些输入会很有帮助。

2 个答案:

答案 0 :(得分:0)

您的模块正在导出类,这就是您要导入的类,但是您从未实例化它。您需要通过调用MyAPI获取new MyAPI()的实例。然后,您可以在该实例上调用fetchData。只需更新模块以导出实例即可:

module.exports = new myAPI()

答案 1 :(得分:0)

嘿,您的问题模棱两可,但让我用一些可以与您的数据匹配的结构来解决它,

几个工作示例:

const { RESTDataSource } = require('apollo-datasource-rest');

class exampleAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = "http://Endpoint";
  }

async allProperties() {
const fetch = await this.get("xml.php?cl="); // incase your endpoint looks like this.
return fetch.properties;
}

async getAllProperties() {
const  response = await this.allProperties(); // you can pass the above function into a new function.
return Array.isArray(response)
? response.map((item) => this.propertyReducer(item))
: []
}

// incase your end point is not an array then you need to include a ! boolean in the return Array.isArray.
async getAllProperties() {
const  response = await this.allProperties(); // you can pass the above function into a new function.
return !Array.isArray(response)
? response.properties.map((item) => this.propertyReducer(item))
: []
}

const { RESTDataSource } = require('apollo-datasource-rest');

class PropertytwoAPI extends RESTDataSource {
  constructor() {
    super();
    this.baseURL = ""
  }

  async getAllData() {
    const response = await this.get("Data");
    return !Array.isArray(response)
    ? response.documents.map((item, index) => this.propertyReducer(item))
    : []
   }


  propertyReducer(item, index){
    return {
       id : [index] - 0,
       fields : {
        Rent_Frequency : {
           stringValue : item.fields.Rent_Frequency.stringValue,
         },
        geopoints : {
           geoPointValue : {
              latitude  : item.fields.geopoints.geoPointValue.latitude,
              longitude : item.fields.geopoints.geoPointValue.longitude,
           }
       },
       Images : {
          arrayValue : {
            values : item.fields.Images.arrayValue.values.map(i => ({
            stringValue : i.stringValue
            })),
          }
      },
       Property_Size : {
         stringValue : item.fields.Property_Size.stringValue,
       },
       Property_purpose :  {
         stringValue : item.fields.Property_purpose.stringValue,
       },
       Property_Description :  {
        stringValue : item.fields.Property_Description.stringValue,
       },
       Price : {
          stringValue : item.fields.Price.stringValue,
       },

       Property_Size_Unit :  {
         stringValue : item.fields.Property_Size_Unit.stringValue,
       },
       Locality :  {
         stringValue : item.fields.Locality.stringValue,
       },
       Property_Type : {
         stringValue : item.fields.Property_Type.stringValue,
       },
       Listing_Agent :   {
         stringValue : item.fields.Listing_Agent.stringValue,
       },
       Listing_Agent_Phone : {
         stringValue : item.fields.Listing_Agent_Phone.stringValue,
       },
       Property_Title :  {
         stringValue : item.fields.Property_Title.stringValue,
       },
       Bathroom :  {
         stringValue : item.fields.Bathroom.stringValue,
       },
       Listing_Agent_Email :  {
         stringValue : item.fields.Listing_Agent_Email.stringValue,
       },
       Bedrooms :  {
         stringValue : item.fields.Bedrooms.stringValue,
       },
       City :  {
         stringValue : item.fields.City.stringValue,
       },
       Property_Status :  {
         stringValue : item.fields.Property_Status.stringValue,
       },
       Sub_Locality :  {
         stringValue : item.fields.Sub_Locality.stringValue,
       },
     }
     }
    }
  }



module.exports = PropertytwoAPI;

解析器:

const { paginateResults } = require('./utils');


module.exports = {
    Query : {

    properties: async (_, {limit = 20, after}, {dataSources}) => {
      const allProperties = await dataSources.propertyAPI.getAllProperties();
      allProperties;
      const properties = paginateResults({
        after,
        limit,
        results: allProperties
      });
      return {
        properties,
        cursor : properties.length ? properties[properties.length -1].cursor : null,
        hasMore : properties.length
        ? properties[properties.length -1].cursor !==
        allProperties[allProperties.length -1].cursor
        : false
      };
    },

    propertytwo : (_, __, {dataSources}) =>
      dataSources.propertytwoAPI.getAllData(),

    property: (_, { id }, { dataSources }) =>
      dataSources.propertyAPI.getPropertyById({ propertyId: id }),

       },

    },




  };

您可以参考以下github文件:https://github.com/trackmystories/apollo-server-RESTDataSource

这是我写的一篇文章,旨在解决阿波罗文档中的一些理解问题

https://medium.com/@ashirazee/array-isarray-response-returns-null-apollo-graphql-e9132cc98153