为什么在此GraphQL解析器中“ refproductoptions.getProductOptions不是函数”?

时间:2019-06-25 17:07:37

标签: graphql apollo-server resolver

我正在使用来自关联关系的三个表。父“产品”具有与表RefProductOptions.productID关联的ID。这与我的解析器完美配合,并且GraphQL正确响应。

当我引入第三个表“ productoptions”并尝试在“ refproductoptions.optionId”与“ productoptions.id”之间添加关联层时,GraphQL查找失败。

The GraphQL error message is:
{
  "errors": [
    {
      "message": "refproductoptions.getProductOptions is not a function",
      "locations": [
        {
          "line": 13,
          "column": 7
        }
      ],
      "path": [
        "products",
        "refproductoptions",
        0,
        "productoptions"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "stacktrace": [
            "TypeError: refproductoptions.getProductOptions is not a function",
            "    at productoptions (/home/bob/graphql-server/src/resolvers.js:35:35)",
            "    at field.resolve (/home/bob/graphql-server/node_modules/graphql-extensions/dist/index.js:140:26)",
...

我已经审查了我的代码,以确保我尊重引用及其大小写。

// src/resolvers.js

const resolvers = {
    Query: {
        async products (root, { name }, { models }) {
            let searchCriteria = {where: { name } };
            return await models.Products.findOne(searchCriteria);
        },
        async refproductoptions (root, { productId }, { models }) {
            let searchCriteria = {where: { productId } };
            //console.log( searchCriteria );
            return await models.RefProductOptions.findOne(searchCriteria);
        },
        async productoptions (root, { id }, { models }) {
            let searchCriteria = {where: { id } };
            console.log ( searchCriteria );
            return await models.ProductOptions.findOne(searchCriteria);
        },
    },
    Products: {
        async refproductoptions (products) {
            return products.getRefProductOptions()
        }
    },
    RefProductOptions: {
        async productoptions (refproductoptions) {
            console.log("====================== RefProductOptions");
            return refproductoptions.getProductOptions()
        }
    },
    /*
    ProductOptions: {
        async refproductoptions (productoptions) {
            return productoptions.getRefProductOptions()
        }
    }
    */
}

module.exports = resolvers

如果我运行此查询:

{
  products (name: "Test Product") {
    name
    urlWordRef
    seoTitle
    seoDescription
    standardEquipment
    technicalSpecs
    refproductoptions {
      id
      productId
      optionId
    }
  }
}

我得到了很好的结果。但是,如果我在下面的查询的“产品选项”部分中添加,它将失败,并出现上述错误。

{
  products (name: "test") {
    name
    urlWordRef
    seoTitle
    seoDescription
    standardEquipment
    technicalSpecs
    refproductoptions {
      id
      productId
      optionId
      productoptions {
        name
      }
    }
  }
}

此外,此查询也失败:

{
  refproductoptions(productId: 1) {
    id
    productId
    optionId
    productoptions {
      name
    }
  }
}

0 个答案:

没有答案