Apollo 服务器数据源定义产生错误
Apollo server datasources definition generates an error
我在使用 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
一些意见会很有帮助。
您的模块正在导出 class,这就是您导入的内容,但您从未实例化它。您需要通过调用 new MyAPI()
来获取 MyAPI
的实例。然后您可以在该实例上调用 fetchData
。只需更新您的模块即可导出实例:
module.exports = new myAPI()
嘿,你的问题不明确,但是让我用一些可以匹配你的数据的结构来解决它,
几个工作示例:
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
这是我写的一篇文章,解决了 apollo 文档中的一些理解问题
https://medium.com/@ashirazee/array-isarray-response-returns-null-apollo-graphql-e9132cc98153
我在使用 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
一些意见会很有帮助。
您的模块正在导出 class,这就是您导入的内容,但您从未实例化它。您需要通过调用 new MyAPI()
来获取 MyAPI
的实例。然后您可以在该实例上调用 fetchData
。只需更新您的模块即可导出实例:
module.exports = new myAPI()
嘿,你的问题不明确,但是让我用一些可以匹配你的数据的结构来解决它,
几个工作示例:
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
这是我写的一篇文章,解决了 apollo 文档中的一些理解问题
https://medium.com/@ashirazee/array-isarray-response-returns-null-apollo-graphql-e9132cc98153