swellstores / horizon

Headless NextJS storefront starter powered by Swell
https://swell-horizon-demo.vercel.app/
MIT License
58 stars 52 forks source link

Extending categories GQL query produces an error even if model is equal to developer->models #38

Closed agsmartwolf closed 1 year ago

agsmartwolf commented 1 year ago

Hi there!

I try to expand categories query with fields "id", "parentId" and "topId":

  categories {
    results {
      name
      slug
      id             <------------ added this field
      parentId       <------------ added this field
      topId          <------------ added this field
      images {
        id
        caption
        file {
          url
          width
          height
        }
      }
      content {
        showProductsPrice
        showProductsDescription
        showFeaturedCategories
        productsPerRow
        enableQuickAdd
        featuredCategories {
          category {
            slug
            name
            description
            images {
              caption
              file {
                url
                width
                height
              }
              id
            }
          }
        }
      }
    }
  }
}

Everything work but "id" field.

When i'm trying to run "graphql-codegen" there is an error pluginHelpers.isDetailedError is not a function. I've checked in swell models in admin developer panel on the web and all of these fields are presented in model.

There is the error only for field id.

Code is generated with fields parentId and topId and they are fetched if remove id field.

rodrigoddalmeida commented 1 year ago

Hi @agsmartwolf. The category.id field is not added by default in the Frontend API permissions - it should be useful to add it to our defaults, we should change that soon. Thanks for the heads up.

Meanwhile, you might find this useful if you want that field (and additional custom fields) to work as well: https://developers.swell.is/guides/opening-content-permissions

You would add something like:

{
  "permissions": {
    "categories": {
      "fields": ["id"]
    }
  }
}
agsmartwolf commented 1 year ago

Hi @rodrigoddalmeida. Thank you for reply. I've tried you said.

I've got the next response:

{
  "parent_id": "store name here",
  "public": "My public key here",
  "date_created": "2022-07-10T18:07:10.902Z",
  "scope": "user",
  "revoked": false,
  "date_updated": "2023-02-23T10:49:27.961Z",
  "permissions": {
    "categories": {
      "fields": [
        "id"
      ],
      "input": {
        "fields": [
          "id"
        ]
      }
    }
  },
  "id": "internal PK here"
}

I've done these actions with all of my public keys. Unfortunately i have the same error:

Screenshot 2023-02-23 at 14 53 10

The error line is id line in categories.gql.

rodrigoddalmeida commented 1 year ago

Are you able to see id in the Category schema by accessing the playground? (https://<your-store>.swell.store/playground)

It seems that graphql-codegen could provide more info on the error, but something is wrong with the package - pluginHelpers is not found, so maybe there is an issue with peer dependencies or version mismatch for them.

BTW, you don't generally want input to be set in base model permissions, as they would allow anyone to update that field.

  ...,
  // Not safe
  "input": {
    "fields": [
      "id"
    ]
  }
  ...
agsmartwolf commented 1 year ago

I can see id in the query I've provided above by accessing the playground. It's working as expected in the playground.

I've also upgraded to the latest versions net packages: @graphql-codegen/introspection@latest @graphql-codegen/typescript@latest @graphql-codegen/typescript-graphql-request@latest @graphql-codegen/typescript-operations@latest.

Still the same error.

Screenshot 2023-02-25 at 22 40 53
agsmartwolf commented 1 year ago

@rodrigoddalmeida Do you have any thoughts?

rodrigoddalmeida commented 1 year ago

@agsmartwolf the playground is a good indicator that your schema is being correctly built and have all fields available.

In that case it seems there might be an issue with how @graphql-codegen/* packages are interacting with each other. I would suggest doing a full clean up of your node_modules (and possibly npm's cache) and reinstall your dependencies.

agsmartwolf commented 1 year ago

It doesn't help =(

rodrigoddalmeida commented 1 year ago

It doesn't help =(

I wish I could give more insight into the error, but additional information seems to be blocked by the missing pluginHelpers.isDetailedError call, which is a @graphql-codegen responsibility. Maybe you could monkey-patch your local dependency to extract out the error message there, before the error happens.

If you happen to fix that at some point, let me know.

agsmartwolf commented 1 year ago

How can I open permission for the field product.variants.results.active? I've tried all of the option I could imagine:

{
  "parent_id": "blabla",
  "public": "pk",
  "date_created": "2022-07-10T18:09:25.254Z",
  "scope": "user",
  "revoked": false,
  "date_updated": "2023-03-08T15:58:17.174Z",
  "permissions": {
    "categories": {
      "fields": [
        "id"
      ]
    },
    "products": {
      "fields": [
        ":variants.active",
        "variants.active",
        "variants.results.active",
        "options.active"
      ]
    },
    "product": {
      "fields": [
        "active",
        "variants.active",
        "variants.results.active"
      ]
    },
    "products:variants": {
      "fields": [
        "active",
        "variants.active",
        "variants.results.active",
        "options.active"
      ]
    },
    "variant": {
      "fields": [
        "active"
      ]
    },
    "ProductsVariants": {
      "fields": [
        "active"
      ]
    },
    "variants": {
      "fields": [
        "active"
      ]
    }
  },
  "id": "blabla"
}
rodrigoddalmeida commented 1 year ago

The correct permission for that field is:

{
  "products": {
    "fields": [
      "variants.active"
    ]
  }
}

The reason why you might not see immediate changes to your GraphQL schema after changing permissions is related to caching. One way to force the schema to be refreshed is to request the API with a Cache-control: no-cache header.

That can be done in the GQL Playground, by setting up its HTTP Headers in the bottom left panel.

agsmartwolf commented 1 year ago

I've sent the PUT request with the payload you provided. I have next response:

Screenshot 2023-03-09 at 17 47 32

I tried to send an empty array for the field fields for other models I've updated before, but it's not updated.

So by the next step, I've set a Cache-Control header as you said but I still have the problem:

image
agsmartwolf commented 1 year ago

About @graphql-codegen and categories id field. I've re-updated the package and now it shows the error in the next way:

Screenshot 2023-03-09 at 23 53 11

Moreover in playground with cache control is working perfect:

image

But if I remove the cache control header, remove theid field - send request, then return id field and send the request again - an error is returned!

image
rodrigoddalmeida commented 1 year ago

I've sent the PUT request with the payload you provided. I have next response: Screenshot 2023-03-09 at 17 47 32

I tried to send an empty array for the field fields for other models I've updated before, but it's not updated.

So by the next step, I've set a Cache-Control header as you said but I still have the problem: image

It seems you are trying to access active on the SwellProductVariants type, which is a Page type and should not contain that field. It should be accessible within results (SwellProductVariant type), though:

query {  
  ...
  variants {
    results {
      active
    }
  }
}
agsmartwolf commented 1 year ago

Unfortunately it doesn't work:

image
agsmartwolf commented 1 year ago

OMG! About products.variants.results.active - it worked. I have multiple public keys and had changed permissions for another one... So I used the right internal id for the PK I use, and it worked fine.

About codegen I could resolve it too! I've changed codegen.yml to the next:

Screenshot 2023-03-10 at 18 15 49