Graphql-shield: How to import graphql-shield into nestjs

Created on 10 Dec 2018  Â·  6Comments  Â·  Source: maticzav/graphql-shield

Question about GraphQL Shield

How to import graphql-shield into nestjs?
can you give me an example to me?

the nestjs graphql is based on apollo-server-express . about nestjs ,please click here https://docs.nestjs.com/graphql/quick-start

  • [√ ] I have checked other questions and found none that macthes mine.

Most helpful comment

@dogscar
You can use transformSchema:

import { applyMiddleware } from 'graphql-middleware';
import { rule, shield } from 'graphql-shield';
import { mergeSchemas } from 'graphql-tools';

// ...

// Permissions
const isAuthenticated = rule()(async (parent, args, ctx, info) => {
  return false;
});
const permissions = shield({
  Query: {
    users: isAuthenticated
  }
});

GraphQLModule.forRoot({
  //...
  transformSchema: (schema: GraphQLSchema) => {
    schema = applyMiddleware(schema, permissions);
    return schema;
  }
})

https://github.com/nestjs/graphql/issues/92#issuecomment-449951759

All 6 comments

I stumbled upon graphql-shield after discovering the need for role based authorization in my Nest project today. I was going to ask a similar question, since Nest recommended (and I did) use SDL it's not clear how shield's applyMiddleware would work in this case.
To hopefully make matters simpler though Nest wraps Apollo Server for their implementation.

@dogscar If graphql shield doesn't work out an alternative is to use Nest's native guards, see https://docs.nestjs.com/guards

Additionally they cover a sample GqlAuthGuard under their authentication section: https://docs.nestjs.com/techniques/authentication , just search for GraphQL on that page

Hey @dogscar 👋,

I am delighted to hear that NestJS users are interested in using graphql-shield. Unfortunately, I am not familiar with NestJS. However, I am willing to explore the topic. It would be especially helpful if you could provide me with some more concrete entry points to using nestjs, in particular, ways to change the schema.

graphql-shield relies on graphql-middleware which is supported out-of-the-box with graphql-yoga, and has a helper function to use it with ApolloServer. I believe this could be a great starting point of our exploration; you can read more about it here.

Let me know if you find anything useful 🙂

@dogscar
You can use transformSchema:

import { applyMiddleware } from 'graphql-middleware';
import { rule, shield } from 'graphql-shield';
import { mergeSchemas } from 'graphql-tools';

// ...

// Permissions
const isAuthenticated = rule()(async (parent, args, ctx, info) => {
  return false;
});
const permissions = shield({
  Query: {
    users: isAuthenticated
  }
});

GraphQLModule.forRoot({
  //...
  transformSchema: (schema: GraphQLSchema) => {
    schema = applyMiddleware(schema, permissions);
    return schema;
  }
})

https://github.com/nestjs/graphql/issues/92#issuecomment-449951759

Closing the issues as I believe @ph55 solved it. 🙂

@ph55
it works! thanks

@ph55 any ideas how to achieve this with code first schema generation? I don't think transformSchema is called.

Was this page helpful?
0 / 5 - 0 ratings