@simtlix/simfinity-js 2.3.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -53,7 +53,7 @@ A powerful Node.js framework that automatically generates GraphQL schemas from y
53
53
  - [Contributing](#-contributing)
54
54
  - [Query Examples from Series-Sample](#-query-examples-from-series-sample)
55
55
  - [State Machine Example from Series-Sample](#-state-machine-example-from-series-sample)
56
- - [Envelop Plugin for Count in Extensions](#-envelop-plugin-for-count-in-extensions)
56
+ - [Plugins for Count in Extensions](#-plugins-for-count-in-extensions)
57
57
  - [API Reference](#-api-reference)
58
58
 
59
59
  ## ✨ Features
@@ -2638,28 +2638,30 @@ query {
2638
2638
  - **count**: Optional boolean - if `true`, returns total count of matching records
2639
2639
 
2640
2640
  #### Getting Total Count:
2641
- When `count: true` is specified, the total count is available in the response extensions. You need to configure an Envelop plugin to expose it:
2642
-
2643
- ```javascript
2644
- // Envelop plugin for count in extensions
2645
- function useCountPlugin() {
2646
- return {
2647
- onExecute() {
2648
- return {
2649
- onExecuteDone({ result, args }) {
2650
- if (args.contextValue?.count) {
2651
- result.extensions = {
2652
- ...result.extensions,
2653
- count: args.contextValue.count,
2654
- };
2655
- }
2656
- }
2657
- };
2658
- }
2659
- };
2660
- }
2641
+ When `count: true` is specified, the total count is available in the response extensions. You need to configure a plugin to expose it. Simfinity.js provides utility plugins for both Apollo Server and Envelop:
2642
+
2643
+ ```javascript
2644
+ const simfinity = require('@simtlix/simfinity-js');
2645
+
2646
+ // For Envelop
2647
+ const getEnveloped = envelop({
2648
+ plugins: [
2649
+ useSchema(schema),
2650
+ simfinity.plugins.envelopCountPlugin(),
2651
+ ],
2652
+ });
2653
+
2654
+ // For Apollo Server
2655
+ const server = new ApolloServer({
2656
+ schema,
2657
+ plugins: [
2658
+ simfinity.plugins.apolloCountPlugin(),
2659
+ ],
2660
+ });
2661
2661
  ```
2662
2662
 
2663
+ See the [Plugins for Count in Extensions](#-plugins-for-count-in-extensions) section for complete examples.
2664
+
2663
2665
  #### Example Response:
2664
2666
  ```json
2665
2667
  {
@@ -2999,45 +3001,18 @@ mutation {
2999
3001
  ```
3000
3002
 
3001
3003
 
3002
- ## 📦 Envelop Plugin for Count in Extensions
3004
+ ## 📦 Plugins for Count in Extensions
3003
3005
 
3004
- To include the total count in the extensions of your GraphQL response, you can use an Envelop plugin. This is particularly useful for pagination and analytics.
3006
+ To include the total count in the extensions of your GraphQL response, Simfinity.js provides utility plugins for both Apollo Server and Envelop. This is particularly useful for pagination and analytics.
3005
3007
 
3006
- ### Envelop Plugin Example
3008
+ ### Envelop Plugin
3007
3009
 
3008
- Here's how you can implement the plugin:
3009
-
3010
- ```javascript
3011
- // Envelop plugin for count in extensions
3012
- function useCountPlugin() {
3013
- return {
3014
- onExecute() {
3015
- return {
3016
- onExecuteDone({ result, args }) {
3017
- if (args.contextValue?.count) {
3018
- result.extensions = {
3019
- ...result.extensions,
3020
- count: args.contextValue.count,
3021
- };
3022
- }
3023
- }
3024
- };
3025
- }
3026
- };
3027
- }
3028
- ```
3029
-
3030
- ### How to Use
3031
-
3032
- 1. **Integrate the Plugin**: Add the plugin to your GraphQL server setup.
3033
- 2. **Configure Context**: Ensure that your context includes the count value when executing queries.
3034
- 3. **Access Count**: The count will be available in the `extensions` field of the GraphQL response.
3035
-
3036
- ### Example Usage
3010
+ Use `simfinity.plugins.envelopCountPlugin()` to add count to extensions when using Envelop:
3037
3011
 
3038
3012
  ```javascript
3039
3013
  const { envelop, useSchema } = require('@envelop/core');
3040
3014
  const { makeExecutableSchema } = require('@graphql-tools/schema');
3015
+ const simfinity = require('@simtlix/simfinity-js');
3041
3016
 
3042
3017
  const schema = makeExecutableSchema({
3043
3018
  typeDefs,
@@ -3047,13 +3022,42 @@ const schema = makeExecutableSchema({
3047
3022
  const getEnveloped = envelop({
3048
3023
  plugins: [
3049
3024
  useSchema(schema),
3050
- useCountPlugin(), // Add the count plugin here
3025
+ simfinity.plugins.envelopCountPlugin(), // Add the count plugin here
3051
3026
  ],
3052
3027
  });
3053
3028
 
3054
3029
  // Use getEnveloped in your server setup
3055
3030
  ```
3056
3031
 
3032
+ ### Apollo Server Plugin
3033
+
3034
+ Use `simfinity.plugins.apolloCountPlugin()` to add count to extensions when using Apollo Server:
3035
+
3036
+ ```javascript
3037
+ const { ApolloServer } = require('apollo-server-express');
3038
+ const simfinity = require('@simtlix/simfinity-js');
3039
+
3040
+ const server = new ApolloServer({
3041
+ schema,
3042
+ plugins: [
3043
+ simfinity.plugins.apolloCountPlugin(), // Add the count plugin here
3044
+ ],
3045
+ context: ({ req }) => {
3046
+ // Your context setup
3047
+ return {
3048
+ user: req.user,
3049
+ // count will be automatically added to extensions if present in context
3050
+ };
3051
+ },
3052
+ });
3053
+ ```
3054
+
3055
+ ### How to Use
3056
+
3057
+ 1. **Import the Plugin**: Use `simfinity.plugins.envelopCountPlugin()` or `simfinity.plugins.apolloCountPlugin()` depending on your GraphQL server.
3058
+ 2. **Configure Context**: Ensure that your context includes the count value when executing queries (Simfinity.js automatically sets `context.count` when `count: true` is specified in pagination).
3059
+ 3. **Access Count**: The count will be available in the `extensions` field of the GraphQL response.
3060
+
3057
3061
  ### Example Response
3058
3062
 
3059
3063
  When the plugin is correctly set up, your GraphQL response will include the count in the extensions:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simtlix/simfinity-js",
3
- "version": "2.3.0",
3
+ "version": "2.3.1",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -2141,6 +2141,7 @@ export { createValidatedScalar };
2141
2141
 
2142
2142
  export { default as validators } from './validators.js';
2143
2143
  export { default as scalars } from './scalars.js';
2144
+ export { default as plugins } from './plugins.js';
2144
2145
 
2145
2146
  const createArgsForQuery = (argTypes) => {
2146
2147
  const argsObject = {};
package/src/plugins.js ADDED
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Apollo Server plugin to add count to GraphQL response extensions
3
+ * @returns {Object} Apollo Server plugin
4
+ */
5
+ export const apolloCountPlugin = () => {
6
+ return {
7
+ async requestDidStart() {
8
+ return {
9
+ async willSendResponse({ contextValue, response }) {
10
+ if (response.body.kind === 'single' && contextValue?.count) {
11
+ response.body.singleResult.extensions = {
12
+ ...(response.body.singleResult.extensions || {}),
13
+ count: contextValue.count,
14
+ };
15
+ }
16
+ }
17
+ };
18
+ }
19
+ };
20
+ };
21
+
22
+ /**
23
+ * Envelop plugin to add count to GraphQL response extensions
24
+ * @returns {Object} Envelop plugin
25
+ */
26
+ export const envelopCountPlugin = () => {
27
+ return {
28
+ onExecute() {
29
+ return {
30
+ onExecuteDone({ result, args }) {
31
+ if (args.contextValue?.count) {
32
+ result.extensions = {
33
+ ...result.extensions,
34
+ count: args.contextValue.count
35
+ };
36
+ }
37
+ }
38
+ };
39
+ }
40
+ };
41
+ };
42
+
43
+ // Export all plugins as an object for convenience
44
+ const plugins = {
45
+ apolloCountPlugin,
46
+ envelopCountPlugin,
47
+ };
48
+
49
+ export default plugins;
50
+