1
0

index.mdx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. ---
  2. title: "StellatePlugin"
  3. generated: true
  4. ---
  5. <GenerationInfo sourceFile="packages/stellate-plugin/src/stellate-plugin.ts" sourceLine="246" packageName="@vendure/stellate-plugin" since="2.1.5" />
  6. A plugin to integrate the [Stellate](https://stellate.co/) GraphQL caching service with your Vendure server.
  7. The main purpose of this plugin is to ensure that cached data gets correctly purged in
  8. response to events inside Vendure. For example, changes to a Product's description should
  9. purge any associated record for that Product in Stellate's cache.
  10. ## Pre-requisites
  11. You will first need to [set up a free Stellate account](https://stellate.co/signup).
  12. You will also need to generate an **API token** for the Stellate Purging API. For instructions on how to generate the token,
  13. see the [Stellate Purging API docs](https://docs.stellate.co/docs/purging-api#authentication).
  14. ## Installation
  15. ```
  16. npm install @vendure/stellate-plugin
  17. ```
  18. ## Configuration
  19. The plugin is configured via the `StellatePlugin.init()` method. This method accepts an options object
  20. which defines the Stellate service name and API token, as well as an array of <DocsLink href="/reference/core-plugins/stellate-plugin/purge-rule#purgerule">PurgeRule</DocsLink>s which
  21. define how the plugin will respond to Vendure events in order to trigger calls to the
  22. Stellate [Purging API](https://stellate.co/docs/graphql-edge-cache/purging-api).
  23. *Example*
  24. ```ts
  25. import { StellatePlugin, defaultPurgeRules } from '@vendure/stellate-plugin';
  26. import { VendureConfig } from '@vendure/core';
  27. export const config: VendureConfig = {
  28. // ...
  29. plugins: [
  30. StellatePlugin.init({
  31. // The Stellate service name, i.e. `<serviceName>.stellate.sh`
  32. serviceName: 'my-service',
  33. // The API token for the Stellate Purging API. See the "pre-requisites" section above.
  34. apiToken: process.env.STELLATE_PURGE_API_TOKEN,
  35. devMode: !isProd || process.env.STELLATE_DEBUG_MODE ? true : false,
  36. debugLogging: process.env.STELLATE_DEBUG_MODE ? true : false,
  37. purgeRules: [
  38. ...defaultPurgeRules,
  39. // custom purge rules can be added here
  40. ],
  41. }),
  42. ],
  43. };
  44. ```
  45. In your Stellate dashboard, you can use the following configuration example as a sensible default for a
  46. Vendure application:
  47. *Example*
  48. ```ts
  49. import { Config } from "stellate";
  50. const config: Config = {
  51. config: {
  52. name: "my-vendure-server",
  53. originUrl: "https://my-vendure-server.com/shop-api",
  54. ignoreOriginCacheControl: true,
  55. passThroughOnly: false,
  56. scopes: {
  57. SESSION_BOUND: "header:authorization|cookie:session",
  58. },
  59. headers: {
  60. "access-control-expose-headers": "vendure-auth-token",
  61. },
  62. rootTypeNames: {
  63. query: "Query",
  64. mutation: "Mutation",
  65. },
  66. keyFields: {
  67. types: {
  68. SearchResult: ["productId"],
  69. SearchResponseCacheIdentifier: ["collectionSlug"],
  70. },
  71. },
  72. rules: [
  73. {
  74. types: [
  75. "Product",
  76. "Collection",
  77. "ProductVariant",
  78. "SearchResponse",
  79. ],
  80. maxAge: 900,
  81. swr: 900,
  82. description: "Cache Products & Collections",
  83. },
  84. {
  85. types: ["Channel"],
  86. maxAge: 9000,
  87. swr: 9000,
  88. description: "Cache active channel",
  89. },
  90. {
  91. types: ["Order", "Customer", "User"],
  92. maxAge: 0,
  93. swr: 0,
  94. description: "Do not cache user data",
  95. },
  96. ],
  97. },
  98. };
  99. export default config;
  100. ```
  101. ## Storefront setup
  102. In your storefront, you should point your GraphQL client to the Stellate GraphQL API endpoint, which is
  103. `https://<service-name>.stellate.sh`.
  104. Wherever you are using the `search` query (typically in product listing & search pages), you should also add the
  105. `cacheIdentifier` field to the query. This will ensure that the Stellate cache is correctly purged when
  106. a Product or Collection is updated.
  107. *Example*
  108. ```ts
  109. import { graphql } from '../generated/gql';
  110. export const searchProductsDocument = graphql(`
  111. query SearchProducts($input: SearchInput!) {
  112. search(input: $input) {
  113. // highlight-start
  114. cacheIdentifier {
  115. collectionSlug
  116. }
  117. // highlight-end
  118. items {
  119. # ...
  120. }
  121. }
  122. }
  123. }`);
  124. ```
  125. ## Custom PurgeRules
  126. The configuration above only accounts for caching of some of the built-in Vendure entity types. If you have
  127. custom entity types, you may well want to add them to the Stellate cache. In this case, you'll also need a way to
  128. purge those entities from the cache when they are updated. This is where the <DocsLink href="/reference/core-plugins/stellate-plugin/purge-rule#purgerule">PurgeRule</DocsLink> comes in.
  129. Let's imagine that you have built a simple CMS plugin for Vendure which exposes an `Article` entity in your Shop API, and
  130. you have added this to your Stellate configuration:
  131. *Example*
  132. ```ts
  133. import { Config } from "stellate";
  134. const config: Config = {
  135. config: {
  136. // ...
  137. rules: [
  138. // ...
  139. {
  140. types: ["Article"],
  141. maxAge: 900,
  142. swr: 900,
  143. description: "Cache Articles",
  144. },
  145. ],
  146. },
  147. // ...
  148. };
  149. export default config;
  150. ```
  151. You can then add a custom <DocsLink href="/reference/core-plugins/stellate-plugin/purge-rule#purgerule">PurgeRule</DocsLink> to the StellatePlugin configuration:
  152. *Example*
  153. ```ts
  154. import { StellatePlugin, defaultPurgeRules } from "@vendure/stellate-plugin";
  155. import { VendureConfig } from "@vendure/core";
  156. import { ArticleEvent } from "./plugins/cms/events/article-event";
  157. export const config: VendureConfig = {
  158. // ...
  159. plugins: [
  160. StellatePlugin.init({
  161. // ...
  162. purgeRules: [
  163. ...defaultPurgeRules,
  164. new PurgeRule({
  165. eventType: ArticleEvent,
  166. handler: async ({ events, stellateService }) => {
  167. const articleIds = events.map((e) => e.article.id);
  168. stellateService.purge("Article", articleIds);
  169. },
  170. }),
  171. ],
  172. }),
  173. ],
  174. };
  175. ```
  176. ## DevMode & Debug Logging
  177. In development, you can set `devMode: true`, which will prevent any calls being made to the Stellate Purging API.
  178. If you want to log the calls that _would_ be made to the Stellate Purge API when in devMode, you can set `debugLogging: true`.
  179. Note that debugLogging generates a lot of debug-level logging, so it is recommended to only enable this when needed.
  180. *Example*
  181. ```ts
  182. import { StellatePlugin, defaultPurgeRules } from '@vendure/stellate-plugin';
  183. import { VendureConfig } from '@vendure/core';
  184. export const config: VendureConfig = {
  185. // ...
  186. plugins: [
  187. StellatePlugin.init({
  188. // ...
  189. devMode: !process.env.PRODUCTION,
  190. debugLogging: process.env.STELLATE_DEBUG_MODE ? true : false,
  191. purgeRules: [
  192. ...defaultPurgeRules,
  193. ],
  194. }),
  195. ],
  196. };
  197. ```
  198. ```ts title="Signature"
  199. class StellatePlugin implements OnApplicationBootstrap {
  200. static options: StellatePluginOptions;
  201. init(options: StellatePluginOptions) => ;
  202. constructor(options: StellatePluginOptions, eventBus: EventBus, stellateService: StellateService, moduleRef: ModuleRef)
  203. onApplicationBootstrap() => ;
  204. }
  205. ```
  206. * Implements: OnApplicationBootstrap
  207. <div className="members-wrapper">
  208. ### options
  209. <MemberInfo kind="property" type={`<a href='/reference/core-plugins/stellate-plugin/stellate-plugin-options#stellatepluginoptions'>StellatePluginOptions</a>`} />
  210. ### init
  211. <MemberInfo kind="method" type={`(options: <a href='/reference/core-plugins/stellate-plugin/stellate-plugin-options#stellatepluginoptions'>StellatePluginOptions</a>) => `} />
  212. ### constructor
  213. <MemberInfo kind="method" type={`(options: <a href='/reference/core-plugins/stellate-plugin/stellate-plugin-options#stellatepluginoptions'>StellatePluginOptions</a>, eventBus: <a href='/reference/typescript-api/events/event-bus#eventbus'>EventBus</a>, stellateService: <a href='/reference/core-plugins/stellate-plugin/stellate-service#stellateservice'>StellateService</a>, moduleRef: ModuleRef) => StellatePlugin`} />
  214. ### onApplicationBootstrap
  215. <MemberInfo kind="method" type={`() => `} />
  216. </div>