【问题标题】:Graphiql returns all items instead of specific onesGraphiql 返回所有项目而不是特定项目
【发布时间】:2023-03-12 01:56:02
【问题描述】:

我正在尝试通过调用 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}),

但我不断收到错误 Cannot return null for non-nullable field Query.allItems. 我允许架构中的参数 (allItems(id: Int): [Item]!)。
当我将解析器中的查询更改为: allItems: () => getAllLinks(), //or testAllLinks() 它返回所有项目。

在这里,我使用查找功能 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]!

  }
`;

【问题讨论】:

    标签: javascript node.js graphql


    【解决方案1】:

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

    Lodash 的find(以及内置的数组方法)接受一个数组并从该数组中返回一个项目。如果找不到符合您指定条件的项目,它将返回 undefined。

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

    此外,getAllLinks 返回一个承诺,因此您需要先解决它,然后再使用filterfind 之类的东西来修改结果。类似的东西

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

    【讨论】:

    • 嗯,我不知道,对我来说,我的架构看起来和这个一样,这里来自 graphiql,看看imgur.com/a/enbx6 - 这就是我尝试使用 find 功能的原因。无论如何,使用过滤器函数查询返回 null(但是在我指定的架构中 [Item]!)
    • 我还更新了包含架构的第一篇文章。 allItems: (_, { id }) => filter(getAllLinks(), {id: id}), 查询不返回任何内容 - "allItems": []
    • 糟糕,忘记了您的 getAllLinks 函数正在返回一个 Promise。见编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2020-11-02
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多