所以这可能是非常基本的问题,所以请耐心等待。让我解释一下我在做什么以及我真正需要什么。
<小时/>
说明
<小时/>
我使用 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' })
server.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
index.js
require('babel-core')
require('babel-polyfill')
require = require('esm')(module/* , options */)
const server = require('./server.js') // --> the above file
server.init()
<小时/>
问题陈述
<小时/>
我正在使用node index.js来启动应用程序。因此,应用程序期望始终存在授权 header (JWT token ),即使对于内省(introspection)查询也是如此。但这不是我想要的,我希望即使没有 token ,内省(introspection)查询也可以解析。以便任何人都可以查看文档。
请阐明并指导最佳方法是什么。快乐编码:)
.startsWith('query Introspection')
是不安全,因为任何查询都可以命名为Introspection
。
更好的方法是检查整个查询。
首先导入graphql并准备内省(introspection)查询字符串:
const { parse, print, getIntrospectionQuery } = require('graphql');
// format introspection query same way as apollo tooling do
const introspectionQuery = print(parse(getIntrospectionQuery()));
然后在 Apollo Server 配置中检查查询:
context: ({ req }) => {
// allow introspection query
if (req.body.query === introspectionQuery) {
return {};
}
// continue
}
我是一名优秀的程序员,十分优秀!