首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Express-GraphQL上的多个过滤器

Express-GraphQL上的多个过滤器
EN

Stack Overflow用户
提问于 2016-11-29 09:12:34
回答 1查看 586关注 0票数 3

我在我的应用程序中使用了express-graphql和node-fetch。我正尝试在api调用中使用graphql来获取数据。目前我正在做的是

代码语言:javascript
复制
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如下

代码语言:javascript
复制
 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}
  })
});

我正在尝试这样做:

代码语言:javascript
复制
{ 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格式如下:

代码语言:javascript
复制
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看起来像这样

代码语言:javascript
复制
function getAccountByUrl (ids) {
  return fetch(URL THAT LOOKS UP 1 ID at a TIME)
    .then(res => res.json())
    .then(json => json)
    .catch(err => err)
}
EN

回答 1

Stack Overflow用户

发布于 2020-03-05 22:19:36

您可以直接从代码中尝试此操作

代码语言:javascript
复制
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);
          });
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40856226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档