javascript - Express-GraphQL 上的多个过滤器

标签 javascript express graphql

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

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 为事件的所有帐户。

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);
          });
        });

关于javascript - Express-GraphQL 上的多个过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40856226/

相关文章:

node.js - 如何返回用户邮箱

node.js - socket.io session ID 和 Express session

javascript - 为什么 graphql 不将我的数据注入(inject)到 React 组件 prop 中?

architecture - 我应该有多个 GraphQL 实例还是只有一个?

javascript - 为什么我的 HTML 表单会被提交?

javascript - Chart.js 如何在条形图中对齐两个 X 轴?

javascript - Node/Express 中 GET 和 POST 请求的时间间隔

reactjs - 如何获取站点地图 Gatsby 的静态文件的更新/lastmod 值

javascript - 将变量从 JavaScript 传递到 PHP 代码不起作用

javascript - 根据现有对象数组的几个属性返回新的对象数组