Ank*_*rma 6 authorization introspection node.js graphql apollo-server
所以可能这是一个非常基本的问题,所以请耐心等待。让我解释一下我在做什么以及我真正需要什么。
解释
我使用ApolloGraphql(apollo-server-express npm 模块)创建了一个 graphql 服务器。
这是给你一个想法的代码片段。
api.js
import express from 'express'
import rootSchema from './root-schema'
.... // some extra code
app = express.router()
app.use(jwtaAuthenticator) // --> this code authenticates Authorization header
.... // some more middleware's added
const graphQLServer = new ApolloServer({
schema: rootSchema, // --> this is root schema object
context: context => context,
introspection: true,
})
graphQLServer.applyMiddleware({ app, path: '/graphql' })
Run Code Online (Sandbox Code Playgroud)
服务器.js
import http from 'http'
import express from 'express'
import apiRouter from './api' // --> the above file
const app = express()
app.use([some middlewares])
app.use('/', apiRouter)
....
....
export async function init () {
try {
const httpServer = http.createServer(app)
httpServer
.listen(PORT)
.on('error', (err) => { setTimeout(() => process.exit(1), 5000) })
} catch (err) {
setTimeout(() => process.exit(1), 5000)
}
console.log('Server started --- ', PORT)
}
export default app
Run Code Online (Sandbox Code Playgroud)
索引.js
require('babel-core')
require('babel-polyfill')
require = require('esm')(module/* , options */)
const server = require('./server.js') // --> the above file
server.init()
Run Code Online (Sandbox Code Playgroud)
问题陈述
我正在使用node index.js来启动应用程序。因此,应用程序希望授权标头(JWT 令牌)始终存在,即使是自省查询也是如此。但这不是我想要的,我希望即使没有令牌,内省查询也可以解决。这样任何人都可以看到文档。
请阐明并请指导这样做的最佳方法是什么。快乐编码:)
小智 10
.startsWith('query Introspection')是不安全的,因为任何查询都可以命名为Introspection。
更好的方法是检查整个查询。
首先导入graphql并准备自省查询字符串:
const { parse, print, getIntrospectionQuery } = require('graphql');
// format introspection query same way as apollo tooling do
const introspectionQuery = print(parse(getIntrospectionQuery()));
Run Code Online (Sandbox Code Playgroud)
然后在 Apollo Server 配置检查查询:
context: ({ req }) => {
// allow introspection query
if (req.body.query === introspectionQuery) {
return {};
}
// continue
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2650 次 |
| 最近记录: |