javascript - Graphiql 返回所有项目而不是特定项目

标签 javascript node.js graphql

我试图通过调用 api 并在查询中设置参数来获取特定项目。我正在尝试使用 find 函数,但在解析 graphiql 中的查询时它给了我错误。
这是我用promise调用api函数。

var request = require('request');

var options = { method: 'GET',
    url: 'myurl',
    qs: { searchCriteria: '' },
    headers:
        { 'postman-token': '12d2dbd7-6ea1-194c-ad38-5bffbac6706c',
            'cache-control': 'no-cache',
            authorization: 'Bearer b0kk2w8ptk9smhnl4ogh4y40adly0s5h',
            'content-type': 'application/json' } };

const getAllLinks = () => {
    return new Promise((resolve, reject) => {
        request(options, function (error, response, body) {
            //console.log(body);

            if (error) {
                reject(error);

            }

            else {

                const myJSON = JSON.parse(body);
                resolve(myJSON.items);
                //console.log(myJSON.items);




            }

        });

    });

};

const testAllLinks = () => {
  return getAllLinks().then((res) => {
      return res;
  })
};

然后在这里我的解析器,查询应该从 api 返回所有项目的特定 id。

Query: {
        allItems: (_, { id }) => find(getAllLinks(), {id: id}),

但我不断收到错误无法为不可为空的字段Query.allItems返回null。我允许在架构中使用参数(allItems(id: Int): [Item] !)。
当我将解析器中的查询更改为: allItems: () => getAllLinks(),//或 testAllLinks() 它返回所有项目。

在这里,我使用 find 函数 https://launchpad.graphql.com/r9wk3kpk8n 为此示例创建了启动板它在那里有效,但在这里不起作用......


架构:

const typeDefs = `
    type Item  {
        id: ID!
        sku: String!
        name: String!
        attribute_set_id: Int!
        price: Float
        status: Int!
        visibility: Int!
        type_id: String!
        created_at: String!
        updated_at: String!
        product_links: [String]
        tier_prices: [Int]
        custom_attributes: [CUSTOM_ATTRIBUTES]
    }

    union CUSTOM_ATTRIBUTES = CustomString | CustomArray

   type CustomString {
    attribute_code: String
    value: String
  }

  type CustomArray {
    attribute_code: String
    value: [String]

  } 

  type Query {
    allItems(id: Int): [Item]!

  }
`;

最佳答案

您的启动板显示一个架构,其中包含解析为单一类型(作者)的查询。您没有在问题中包含架构,但我猜测查询 allItems 应该返回一个列表(类似于 [Item])。如果 GraphQL 需要一个列表,那么您的解析器需要返回一个数组,而不是单个对象。

Lodash 的 find (以及与此相关的内置数组方法)采用一个数组并从该数组返回单个项目。如果找不到符合您指定条件的项目,它将返回未定义。

这意味着您可能需要使用filter,而不是find,将数组减少为仅包含传入 id 的项目。

此外,getAllLinks 返回一个 Promise,因此您需要先解析它,然后再使用 filterfind 等内容来修改结果。类似的东西

allItems: (_, { id }) => getAllLinks().then(result => filter(result, {id: id}))
// or
allItems: async (_, { id }) => filter(await getAllLinks(), {id: id})

关于javascript - Graphiql 返回所有项目而不是特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49383976/

相关文章:

javascript - 尝试清除消息时出现 DiscordAPIError 无效的表单正文

javascript - 尝试了解对象映射的reduce 函数?

graphql - 使用 GraphQL 检索包含具有不同架构的对象数组的对象

javascript - 如何在 CSS 或 jQuery 中持续更改按钮的颜色

javascript - 优化 emberjs 过滤器搜索

javascript - 如何从index.html获取我的js文件的所有src?

search - Github GraphQL 存储库查询,提交总数

reactjs - 使用 GraphQL 在 NextJS 中呈现比之前呈现更多的钩子(Hook)

javascript - 如果第二个输入字段已填满,则暂时禁用输入字段

javascript - 它在哪里调用弹出窗口?