Javascript Express GraphQL上的多个过滤器
我正在应用程序中使用express graphql和节点获取。我试图在api调用中使用graphql来获取数据。目前我正在做Javascript Express GraphQL上的多个过滤器,javascript,express,graphql,Javascript,Express,Graphql,我正在应用程序中使用express graphql和节点获取。我试图在api调用中使用graphql来获取数据。目前我正在做 const QueryType = new GraphQLObjectType({ name: "Query", fields: () => ({ acctsFromRelation: { type: new GraphQLList(AcctType), args: { id: {type: GraphQLStr
const QueryType = new GraphQLObjectType({
name: "Query",
fields: () => ({
acctsFromRelation: {
type: new GraphQLList(AcctType),
args: {
id: {type: GraphQLString},
status: {type: GraphQLString}
},
resolve: (root, args) => getOpIds(args)
}
})
});
AcctType如下所示
const AcctType = new GraphQLObjectType({
name: "acct",
fields: () => ({
id: {type: GraphQLString},
name: {type: GraphQLString},
clientType: {type: GraphQLString},
accountStatus: {type: GraphQLString,
args: {
status: {type: GraphQLString}
}},
primaryContact: {type: contactType},
billingAddress: {type: billingType}
})
});
我正在尝试这样做:
{ acctsFromRelation (id: "2") {
id
name
accountStatus (status: "active")
primaryContact {
firstName
lastName
phone
email
}
billingAddress {
address1
address2
city
state
postalCode
country
}
}
}
其中我获得id为2且accountStatus为active的所有帐户
GetOpIds如下所示:
function getOpIds (ids) {
return fetch(API CALL THAT GIVES IDS)
.then(res => res.json())
.then(json => json.map((element) => getAccountByUrl(element.id)))
.catch(err => err)
}
getAccountByUrl看起来像这样
function getAccountByUrl (ids) {
return fetch(URL THAT LOOKS UP 1 ID at a TIME)
.then(res => res.json())
.then(json => json)
.catch(err => err)
}
您可以直接从代码中尝试此操作
const query = `query AcctsFromRelation($id: ID, $status: String){acctsFromRelation(id: $id, status: $status)}`;
const variables = { id: id, status: status };
return new Promise((resolve, reject) => {
request("/graphql", query, variables).then(data => {
resolve(data.acctsFromRelation);
});
});