contentstack / gatsby-source-contentstack

Contentstack provides a source plugin for pulling content into Gatsby from Contentstack stacks. It helps you query content types and entries in Gatsby using GraphQL
MIT License
14 stars 31 forks source link

Gatsby-4: Changing environment throws an error when sourcing #117

Open psykzz opened 2 years ago

psykzz commented 2 years ago

Once setup, if you change environment (but keeping the same key) you will get the following error.

{
  error_message: 'Failed to fetch items. Please try again with valid parameters.',
  error_code: 141,
  errors: { access_token: [ 'environment or access token is not matched.' ] }
}

Looks like we just need to include the environment + api_key in the cache key.

vkalta commented 2 years ago

@psykzz Thanks for reporting this issue. I will check and get back to you asap.

vkalta commented 2 years ago

@psykzz Can you share the steps to reproduce this issue?

psykzz commented 2 years ago
vkalta commented 2 years ago

Thanks @psykzz for sharing the steps. Will check this ASAP.

psykzz commented 2 years ago

Any updates on this issue? @vkalta

vkalta commented 2 years ago

@abhishek305 Can you please check this and update @psykzz ?

psykzz commented 2 years ago

More details and a potential fix can be found in https://github.com/contentstack/gatsby-source-contentstack/pull/116#discussion_r768926000

abhishek305 commented 2 years ago

Hey @psykzz, I will try to reproduce this issue on my end and will update you soon. Thanks again

abhishek305 commented 2 years ago

@abhishek305 Can you please check this and update @psykzz ?

Yes will check

abhishek305 commented 2 years ago
  • Setup a contentstack stack with two environments, A + B.
  • Setup a Gatsby site building data from stack B.
  • Change the access_token and environement from A -> B.
  • Observe the issue

Hey @psykzz,

Could please elaborate there seems to be some confusion in step 3 are you talking about setting up 2 stacks or a stack with two environments and also the gatsby site sourcing from 2 stacks?

psykzz commented 2 years ago

So I have a stack with multiple environments. I setup and run against environment A, and everything builds fine.

In this case i needed to provide the following configuration

{
  api_key: "stack-api-key"
  delivery_token: "environment-a-delivery-token"
  environment_name: "environment-a"
}

If i then swap to environment B. I replace my config with

{
  api_key: "stack-api-key"
  delivery_token: "environment-b-delivery-token"
  environment_name: "environment-b"
}

After running the 2nd time, i get

{
  error_message: 'Failed to fetch items. Please try again with valid parameters.',
  error_code: 141,
  errors: { access_token: [ 'environment or access token is not matched.' ] }
}

This is because i believe the cached data is only keyed by the api_key and so changing the delivery token attempts to use the old cache, which in this case is wrong, leading to the error message.

abhishek305 commented 2 years ago

So I have a stack with multiple environments. I setup and run against environment A, and everything builds fine.

In this case i needed to provide the following configuration

{
  api_key: "stack-api-key"
  delivery_token: "environment-a-delivery-token"
  environment_name: "environment-a"
}

If i then swap to environment B. I replace my config with

{
  api_key: "stack-api-key"
  delivery_token: "environment-b-delivery-token"
  environment_name: "environment-b"
}

After running the 2nd time, i get

{
  error_message: 'Failed to fetch items. Please try again with valid parameters.',
  error_code: 141,
  errors: { access_token: [ 'environment or access token is not matched.' ] }
}

This is because i believe the cached data is only keyed by the api_key and so changing the delivery token attempts to use the old cache, which in this case is wrong, leading to the error message.

Hi @psykzz, I tried to reproduce the error from the steps you've given but was unable to reproduce this on my end. Steps I performed:

Could you please update the source plugin version and try it once also if possible could you share a few details?

let me know what is observed after the update Thanks !!