1

Tried postgrphile example but not sure where mistake were done?

I am trying to implement graphql-shield as a plugin

    middlewarePlugin = makeProcessSchemaPlugin((schema: typeof GraphQLSchema) => {
        return applyMiddleware(schema, permissions);
    });

permission.ts

const { rule, shield } = require("graphql-shield");

const isAuthenticated = rule()((parent: any, args: any,  user: any ) => {
    return user !== null;
});

const permissions = shield({
    Query: {
        viewer: isAuthenticated
    }
});

export = permissions;

I import middlewarePlugin as others plugins in postgraphile:

  appendPlugins: [
            myClass.myPlugin,
            myClass.jsonPlace,
            myClass.middlewarePlugin
        ],

crash log:

| A serious error occurred when building the initial schema. Exiting because retryOnInitFail is not set. Error details: graphql | graphql | TypeError: Cannot read property 'fragment' of undefined graphql | at isMiddlewareWithFragment (/home/node/app/node_modules/graphql-middleware/src/utils.ts:25:17) graphql | at Object.isMiddlewareFunction (/home/node/app/node_modules/graphql-middleware/src/utils.ts:33:10) graphql | at Object.validateMiddleware (/home/node/app/node_modules/graphql-middleware/src/validation.ts:9:7) graphql | at addMiddlewareToSchema (/home/node/app/node_modules/graphql-middleware/src/middleware.ts:33:27) graphql | at normalisedMiddlewares.reduceRight.schema.schema (/home/node/app/node_modules/graphql-middleware/src/middleware.ts:91:11) graphql | at Array.reduceRight () graphql | at applyMiddlewareWithOptions (/home/node/app/node_modules/graphql-middleware/src/middleware.ts:80:77) graphql | at applyMiddleware (/home/node/app/node_modules/graphql-middleware/src/middleware.ts:132:10) graphql | at hook (/home/node/app/resolvers/test.resolver.ts:254:16) graphql | at SchemaBuilder.applyHooks (/home/node/app/node_modules/graphile-build/src/SchemaBuilder.js:398:20)

galvakojis
  • 408
  • 1
  • 5
  • 19
  • Did you find a solution? – nth-chile May 22 '21 at 15:24
  • Nope, looks like postgraphile and graphql-shield doesn't work at the moment. https://github.com/maticzav/graphql-middleware/issues/369 – galvakojis Jun 04 '21 at 14:02
  • Yeah, I learned that the type of middleware postgraphile expects is a little different from graphql-middleware. I ended up with a custom solution using `makeWrapResolversPlugin`. It isn't perfect yet but it's also pretty simple to implement, would be happy to share if this is still an issue. Essentially, think of `makeWrapResolversPlugin` as a replacement for `graphql-shield` – nth-chile Jun 05 '21 at 15:04
  • Yeah, any solution to this one would be appreciated! – galvakojis Jun 08 '21 at 06:27

1 Answers1

1

The type of middleware postgraphile expects is a little different from graphql-middleware. Instead of downloading graphql-middleware and graphql-shield and trying to get them to work with postgraphile, I ended up writing my own "shield" with makeWrapResolversPlugin. Example:

const filter = (ctx) => {
  // Only attempting to do auth for non-root things, because
  // with postgraphile, all the data gets fetched at the root
  if (ctx.scope.isRootMutation || ctx.scope.isRootQuery) {
    // return this to make it available to the wrapper function
    return ctx.scope.fieldName
  }
  // return null to not wrap this non-root resolver
  return null
}

const wrapper = (fieldName) => 
  async (resolve, source, args, context) => {
    // Unless we're doing createUser, do an auth check

    // 
    if (fieldName === "createUser") return resolve()

    const isAuthenticated = await getIsAuthenticated(context)

    // 
    if (isAuthenticated) return resolve()

    throw new Error("Unauthorized");
  }

const Permissions = makeWrapResolversPlugin(filter, wrapper)
nth-chile
  • 616
  • 1
  • 7
  • 20