Graphql: How to use graphql-shield in nestjs, I don't know how to configure

Created on 12 Dec 2018  Â·  7Comments  Â·  Source: nestjs/graphql

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


graphql-shiel is a GraphQL tool to ease the creation of permission layer.
but I don't know how to configure graphql-shiel with nest. I saw a project based on apollo-server that was configured in this way.

https://github.com/guimpster/shield/blob/master/src/index.js

#

import { applyMiddleware } from 'graphql-middleware'
import { makeExecutableSchema } from 'graphql-tools'
import { ApolloServer } from 'apollo-server'

import { permissions } from './permissions'
import resolvers from './resolvers'
import { getUser } from './faker'
import typeDefs from './schema.graphql'

const schemaWithMiddleware = applyMiddleware(
makeExecutableSchema({ typeDefs, resolvers }),
permissions
)

const Server = new ApolloServer({
schema: schemaWithMiddleware,
context: ({ req }) => ({
...req,
user: getUser(req)
})
})

const options = { port: 4000 }

Server.listen(options).then(({ url }) => {
console.log(🚀 Server ready at ${url})
}).catch((e) => {
console.error(e)
})

#

and in nest ,I couldn't find the place where shiled was imported.

#

@Module({
imports: [
GraphQLModule.forRoot({
typePaths: ['./*/.graphql'],
debug: false,
playground: true
})
],
controllers: [AppController],
providers: [AppService, UserResolver],
})
export class AppModule { }

#

Expected behavior


so How should I import shiled correctly?
thanks.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Nest version: X.Y.Z


For Tooling issues:
- Node version: XX  
- Platform:  

Others:

question

Most helpful comment

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;
  }
})

All 7 comments

There's no way to use graphql-shield.
Already tried to rewrite the @nestjs/graphql module with support for shields, but it's impossible as it wasn't possible to do with our current design patterns and how graphql-shield works.

Probably we should make a graphql-prisma package for nestjs? That allows to
forward entire types from the prisma client and helps progressively add
functionality. It could support all of the features of prisma, but in a
nestjs way.

On Wed, Dec 12, 2018, 17:24 Marcus S. Abildskov <[email protected]
wrote:

With the current implementation there's no way to use graphql-shield

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/nestjs/graphql/issues/92#issuecomment-446649252, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AE64MDmOPKtBFQnZhb5h_3663RDcVoA2ks5u4S20gaJpZM4ZPeCh
.

Hi,

I am also looking to use shield. I was checking the documentation and it appears you can use graphql-middleware ( https://github.com/prisma/graphql-middleware ) and apply the shield as middlware.

Taken from the graphql-shield github docs

// Permissions...

// Apply permissions middleware with applyMiddleware
// Giving any schema (instance of GraphQLSchema)

import { applyMiddleware } from 'graphql-middleware'
// schema definition...
schema = applyMiddleware(schema, permissions)

so it appears all we need access to is the schema.

Would that be possible ?

You stil have the issue with the shield decorators.

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;
  }
})

@ph55
great!you are a good man!
thanks!

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings