@@ -20,12 +20,13 @@ A powerful Node.js framework that automatically generates GraphQL schemas from y
2020 - [ Adding Middlewares] ( #adding-middlewares )
2121 - [ Middleware Parameters] ( #middleware-parameters )
2222 - [ Common Use Cases] ( #common-use-cases )
23- - [ Authorization Middleware ] ( #-authorization-middleware )
23+ - [ Authorization] ( #-authorization )
2424 - [ Quick Start] ( #quick-start-1 )
2525 - [ Permission Schema] ( #permission-schema )
2626 - [ Rule Helpers] ( #rule-helpers )
2727 - [ Policy Expressions (JSON AST)] ( #policy-expressions-json-ast )
28- - [ Integration with graphql-middleware] ( #integration-with-graphql-middleware )
28+ - [ Integration with GraphQL Yoga / Envelop] ( #integration-with-graphql-yoga--envelop )
29+ - [ Legacy: Integration with graphql-middleware] ( #legacy-integration-with-graphql-middleware )
2930- [ Relationships] ( #-relationships )
3031 - [ Defining Relationships] ( #defining-relationships )
3132 - [ Auto-Generated Resolve Methods] ( #auto-generated-resolve-methods )
@@ -75,7 +76,7 @@ A powerful Node.js framework that automatically generates GraphQL schemas from y
7576- ** Lifecycle Hooks** : Controller methods for granular control over operations
7677- ** Custom Validation** : Field-level and type-level custom validations
7778- ** Relationship Management** : Support for embedded and referenced relationships
78- - ** Authorization Middleware ** : Production-grade GraphQL authorization with RBAC/ABAC, function-based rules, and declarative policy expressions
79+ - ** Authorization** : Production-grade GraphQL authorization with RBAC/ABAC, function-based rules, declarative policy expressions, and native Envelop/Yoga plugin support
7980
8081## 📦 Installation
8182
@@ -629,17 +630,17 @@ simfinity.use((params, next) => {
6296305. ** Performance consideration** : Middlewares run on every operation, keep them lightweight
6306316. ** Use context wisely** : Store request- specific data in the GraphQL context object
631632
632- ## 🔐 Authorization Middleware
633+ ## 🔐 Authorization
633634
634- Simfinity .js provides a production- grade centralized GraphQL authorization middleware supporting RBAC / ABAC , function -based rules, declarative policy expressions (JSON AST ), wildcard permissions, and configurable default policies.
635+ Simfinity .js provides production- grade centralized GraphQL authorization supporting RBAC / ABAC , function -based rules, declarative policy expressions (JSON AST ), wildcard permissions, and configurable default policies. It ships as a native Envelop plugin for GraphQL Yoga (recommended) and also supports the legacy graphql-middleware approach .
635636
636637### Quick Start
637638
638639```javascript
639640const { auth } = require (' @simtlix/simfinity-js' );
640- const { applyMiddleware } = require (' graphql-middleware ' );
641+ const { createYoga } = require (' graphql-yoga ' );
641642
642- const { createAuthMiddleware , requireAuth , requireRole } = auth;
643+ const { createAuthPlugin , requireAuth , requireRole } = auth;
643644
644645// Define your permission schema
645646const permissions = {
@@ -665,9 +666,9 @@ const permissions = {
665666 },
666667};
667668
668- // Create and apply the middleware
669- const authMiddleware = createAuthMiddleware (permissions, { defaultPolicy: ' DENY' });
670- const schemaWithAuth = applyMiddleware ( schema, authMiddleware );
669+ // Create the Envelop auth plugin and pass it to your server
670+ const authPlugin = createAuthPlugin (permissions, { defaultPolicy: ' DENY' });
671+ const yoga = createYoga ({ schema, plugins : [authPlugin] } );
671672` ` `
672673
673674### Permission Schema
@@ -869,25 +870,24 @@ Use `{ ref: 'path' }` to reference values:
869870- Unknown operators fail closed (deny)
870871- No ` eval ()` or ` Function ()` - pure object traversal
871872
872- ### Integration with graphql-middleware
873+ ### Integration with GraphQL Yoga / Envelop
873874
874- The auth middleware integrates with the ` graphql - middleware ` package:
875+ The recommended way to use the auth system is via the Envelop plugin, which works natively with GraphQL Yoga and any Envelop-based server. The plugin wraps resolvers in-place without rebuilding the schema, avoiding compatibility issues.
875876
876877` ` ` javascript
877- const express = require (' express' );
878- const { graphqlHTTP } = require (' express-graphql' );
879- const { applyMiddleware } = require (' graphql-middleware' );
878+ const { createYoga } = require (' graphql-yoga' );
879+ const { createServer } = require (' http' );
880880const simfinity = require (' @simtlix/simfinity-js' );
881881
882882const { auth } = simfinity;
883- const { createAuthMiddleware , requireAuth , requireRole , requirePermission } = auth;
883+ const { createAuthPlugin , requireAuth , requireRole , requirePermission } = auth;
884884
885885// Define your types and connect them
886886simfinity .connect (null , UserType, ' user' , ' users' );
887887simfinity .connect (null , PostType, ' post' , ' posts' );
888888
889889// Create base schema
890- const baseSchema = simfinity .createSchema ();
890+ const schema = simfinity .createSchema ();
891891
892892// Define permissions
893893const permissions = {
@@ -921,36 +921,51 @@ const permissions = {
921921 },
922922};
923923
924- // Create auth middleware
925- const authMiddleware = createAuthMiddleware (permissions, {
926- defaultPolicy: ' DENY' , // Deny access when no rule matches
927- debug: false , // Enable for debugging
924+ // Create auth plugin
925+ const authPlugin = createAuthPlugin (permissions, {
926+ defaultPolicy: ' DENY' ,
927+ debug: false ,
928928});
929929
930- // Apply middleware to schema
931- const schema = applyMiddleware (baseSchema, authMiddleware);
932-
933- // Setup Express with context
934- const app = express ();
935-
936- app .use (' /graphql' , graphqlHTTP ((req ) => ({
930+ // Setup Yoga with the auth plugin
931+ const yoga = createYoga ({
937932 schema,
938- graphiql: true ,
939- context: {
940- user: req .user , // Set by your authentication middleware
941- },
942- formatError: simfinity .buildErrorFormatter ((err ) => {
943- console .error (err);
933+ plugins: [authPlugin],
934+ context : (req ) => ({
935+ user: req .user , // Set by your authentication layer
944936 }),
945- }))) ;
937+ });
946938
947- app .listen (4000 );
939+ const server = createServer (yoga);
940+ server .listen (4000 );
941+ ` ` `
942+
943+ ### Legacy: Integration with graphql-middleware
944+
945+ > **Deprecated:** ` applyMiddleware` from ` graphql- middleware` rebuilds the schema via ` mapSchema` ,
946+ > which can cause ` " Schema must contain uniquely named types" ` errors with Simfinity schemas.
947+ > Use ` createAuthPlugin` with GraphQL Yoga / Envelop instead.
948+
949+ ` ` ` javascript
950+ const { applyMiddleware } = require (' graphql-middleware' );
951+ const simfinity = require (' @simtlix/simfinity-js' );
952+
953+ const { auth } = simfinity;
954+ const { createAuthMiddleware , requireAuth , requireRole } = auth;
955+
956+ const baseSchema = simfinity .createSchema ();
957+
958+ const authMiddleware = createAuthMiddleware (permissions, {
959+ defaultPolicy: ' DENY' ,
960+ });
961+
962+ const schema = applyMiddleware (baseSchema, authMiddleware);
948963` ` `
949964
950- ### Middleware Options
965+ ### Plugin / Middleware Options
951966
952967` ` ` javascript
953- const middleware = createAuthMiddleware (permissions, {
968+ const plugin = createAuthPlugin (permissions, {
954969 defaultPolicy: ' DENY' , // 'ALLOW' or 'DENY' (default: 'DENY')
955970 debug: false , // Enable debug logging
956971});
0 commit comments