Why is “refproductoptions.getProductOptions is not a function” in this GraphQL resolver?

懵懂的女人 提交于 2019-12-11 15:53:41

问题


I'm using three tables from an associative relationship. The parent "products" has an id which is associated to the table RefProductOptions.productID. This works perfectly fine with my resolvers and GraphQL responds correctly.

When I introduce the third table "productoptions" and I try to add an association layer between "refproductoptions.optionId" with "productoptions.id", the GraphQL look up fails.

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)",
...

I've reviewed my code to make sure I'm respecting the references and their casing.

// 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

If I run this query:

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

I get good results. However, if I add in the Product Option part of the query below, it will fail with the error noted above.

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

In addition, this query fails too:

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

来源:https://stackoverflow.com/questions/56758931/why-is-refproductoptions-getproductoptions-is-not-a-function-in-this-graphql-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!