为来自具有对象数组的 API 的响应创建 graphql 模式
creating graphql schema for a response from an API that has an array of objects
我收到 Api 的回复,如下所示:
property: Array(100)
0: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
1: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
2: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
3: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
我想要地址对象中一些指定字段的列表,例如国家和单行,但对于 属性
的每个索引
数组
address:
country: "US"
countrySubd: "CA"
line1: "1702 ELKHORN RD"
line2: "ROYAL OAKS, CA 95076"
locality: "Royal Oaks"
matchCode: "ExaStr"
oneLine: "1702 ELKHORN RD, ROYAL OAKS, CA 95076"
postal1: "95076"
postal2: "9218"
postal3: "R002"
我已经为如何在我的 graphql 模式页面中为此编写模式而苦苦挣扎了 2 天。有人能帮帮我吗?
这是我一直在尝试的方法,但一直在获取数据的空值
require("es6-promise").polyfill();
require("isomorphic-fetch");
const {
GraphQLString,
GraphQLList,
GraphQLSchema,
GraphQLObjectType,
GraphQLInt
} = require("graphql");
const Identifier = new GraphQLObjectType({
name: "identifier",
fields: () => ({
obPropId: { type: GraphQLInt }
})
});
const AddressType = new GraphQLObjectType({
name: 'Address',
fields: () => ({
country: { type: GraphQLString },
oneLine: {type: GraphQLString }
})
})
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
property: {
type: new GraphQLList(Identifier),
resolve(parent, args) {
return fetch(
"https://api.gateway.attomdata.com/propertyapi/v1.0.0/property/address?postalcode=95076&page=1&pagesize=100",
{
headers: {
Accept: "application/json",
APIKey: "XXXXXXXXXXXXXXXXXXXX"
}
}
)
.then((response) => {
const jsonResponse = response.json();
return jsonResponse
}).then((jsonResonse) => console.log(JSON.stringify(jsonResonse)))
.then(res => res.data)
.catch(error => {
console.log(error);
});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
Im running it on a express server and do my checks on localhost:5000/graphql
在评论中我们能够得出以下结论:
需要另一种类型来连接 Address 类型与 RootQuery 类型。我们可以引入类型,调整查询类型的return类型:
type Property {
id: Identifier
address: Address
}
type Query {
property: [Property] # consider using plural field name "properties"
}
我创建了一个可用的 Codesandboy 来展示它的行为方式。
我收到 Api 的回复,如下所示:
property: Array(100)
0: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
1: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
2: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
3: {identifier: {…}, address: {…}, location: {…}, vintage: {…}}
我想要地址对象中一些指定字段的列表,例如国家和单行,但对于 属性
的每个索引数组
address:
country: "US"
countrySubd: "CA"
line1: "1702 ELKHORN RD"
line2: "ROYAL OAKS, CA 95076"
locality: "Royal Oaks"
matchCode: "ExaStr"
oneLine: "1702 ELKHORN RD, ROYAL OAKS, CA 95076"
postal1: "95076"
postal2: "9218"
postal3: "R002"
我已经为如何在我的 graphql 模式页面中为此编写模式而苦苦挣扎了 2 天。有人能帮帮我吗?
这是我一直在尝试的方法,但一直在获取数据的空值
require("es6-promise").polyfill();
require("isomorphic-fetch");
const {
GraphQLString,
GraphQLList,
GraphQLSchema,
GraphQLObjectType,
GraphQLInt
} = require("graphql");
const Identifier = new GraphQLObjectType({
name: "identifier",
fields: () => ({
obPropId: { type: GraphQLInt }
})
});
const AddressType = new GraphQLObjectType({
name: 'Address',
fields: () => ({
country: { type: GraphQLString },
oneLine: {type: GraphQLString }
})
})
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
property: {
type: new GraphQLList(Identifier),
resolve(parent, args) {
return fetch(
"https://api.gateway.attomdata.com/propertyapi/v1.0.0/property/address?postalcode=95076&page=1&pagesize=100",
{
headers: {
Accept: "application/json",
APIKey: "XXXXXXXXXXXXXXXXXXXX"
}
}
)
.then((response) => {
const jsonResponse = response.json();
return jsonResponse
}).then((jsonResonse) => console.log(JSON.stringify(jsonResonse)))
.then(res => res.data)
.catch(error => {
console.log(error);
});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
Im running it on a express server and do my checks on localhost:5000/graphql
在评论中我们能够得出以下结论:
需要另一种类型来连接 Address 类型与 RootQuery 类型。我们可以引入类型,调整查询类型的return类型:
type Property {
id: Identifier
address: Address
}
type Query {
property: [Property] # consider using plural field name "properties"
}
我创建了一个可用的 Codesandboy 来展示它的行为方式。