问题描述
type User {
id: Int!
name: String
dob: String
friends: [User]
}
Query {
user(id: Int!): User
}
在我看来,我刚刚创造了无限递归的潜力:
query GetUser($userId: Int!) {
user(id: $userId) {
friends {
name
friends {
name
friends {
name
...etc
}
}
}
}
}
我如何防止在我的 user
解析器中出现这种情况?
解决方法
这可能是一个潜在的问题,但您可以使用自定义验证来阻止此类请求,例如 https://github.com/stems/graphql-depth-limit
import depthLimit from 'graphql-depth-limit'
import express from 'express'
import graphqlHTTP from 'express-graphql'
import schema from './schema'
const app = express()
app.use('/graphql',graphqlHTTP((req,res) => ({
schema,validationRules: [ depthLimit(10) ]
})))