为来自具有对象数组的API的响应创建graphql架构
我从一个Api得到如下响应:为来自具有对象数组的API的响应创建graphql架构,graphql,schema,graphql-js,express-graphql,graphql-schema,Graphql,Schema,Graphql Js,Express Graphql,Graphql Schema,我从一个Api得到如下响应: property: Array(100) 0: {identifier: {…}, address: {…}, location: {…}, vintage: {…}} 1: {identifier: {…}, address: {…}, location: {…}, vintage: {…}} 2: {identifier: {…}, address: {…}, location: {…}, vintage: {…}} 3: {identifier: {…}, a
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和oneLine,但用于属性的每个索引
排列
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模式页面中为此编写模式苦苦挣扎了两天。谁能帮帮我吗
这是我一直在尝试的,但一直在为数据获取空值
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
在评论中,我们得出以下结论: 连接地址类型和根查询类型需要另一种类型。我们可以引入查询类型,调整查询类型的返回类型:
type Property {
id: Identifier
address: Address
}
type Query {
property: [Property] # consider using plural field name "properties"
}
我创建了一个工作的Codesandboy来显示它的行为。请显示所有相关的代码,包括您尝试过的代码,以及您遇到的错误或意外行为。在我看来,您似乎缺少
地址
和查询字段之间的类型。您将引入类型属性{id:Identifier,address:address}
。然后,您的属性查询将返回该属性的数组@赫尔库,谢谢你,伙计,就是这样!!如果你不介意的话,我会补充一个答案