我想让我的 Graphql API 更安全,我正在寻找一种方法来禁用 Nestjs 中的 Graphql Introspection for Public 或一种排除某些私有 API 的方法,但在 Nestjs 文档中找不到任何参考,
我有 AuthGuards 设置,但它们无法达到阻止架构访问的目的。
如果您使用 Apollo,答案就在文档中。
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: process.env.NODE_ENV !== 'production'
});
这会禁用生产中的自省。如果您想始终禁用它(不推荐,因为这会使开发变得更加困难):
introspection: false
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
sortSchema: true,
playground: false,
introspection: process.env.NODE_ENV !== 'production'
context: ({ req, res }) => ({ req, res }),
subscriptions: {
'graphql-ws': true,
},
plugins: [
process.env.NODE_ENV !== 'production'
? ApolloServerPluginLandingPageLocalDefault()
: ApolloServerPluginLandingPageProductionDefault(),
],
}),
这将确保apollo沙箱仅在开发环境中可见