admin管理员组文章数量:1431955
Let's say we have this GraphQL Schema:
gql`
type Department {
id: ID!
name: String!
programs(programId: ID): [Program]! # filter if an ID is provided
}
type Program {
id: ID!
name: String!
department: Department
}
extend type Query {
getDepartments: [Department]
}
`
The [probable] issue here is that you can get into this loop:
{
getDepartments {
name
programs(1) {
name
department {
name
program(1) {
...
}
}
}
}
}
I'm new to GraphQL so, first I would like to know if this is a problem ? I have that feeling but it might be OK.
I tried to use this alternative:
gql`
type Department {
id: ID!
name: String!
programs(programId: ID): [Program] # filter if an ID is provided
}
type Program {
id: ID!
name: String!
}
extend type Query {
getDepartments: [Department]
getDepartmentForProgram(programId: ID!): Department
}
`
With this, children can not get parents directly, it is now a top query. My second concern is to know if this is a good alternative, especially if the first one is a problem.
Thanks in advance.
Let's say we have this GraphQL Schema:
gql`
type Department {
id: ID!
name: String!
programs(programId: ID): [Program]! # filter if an ID is provided
}
type Program {
id: ID!
name: String!
department: Department
}
extend type Query {
getDepartments: [Department]
}
`
The [probable] issue here is that you can get into this loop:
{
getDepartments {
name
programs(1) {
name
department {
name
program(1) {
...
}
}
}
}
}
I'm new to GraphQL so, first I would like to know if this is a problem ? I have that feeling but it might be OK.
I tried to use this alternative:
gql`
type Department {
id: ID!
name: String!
programs(programId: ID): [Program] # filter if an ID is provided
}
type Program {
id: ID!
name: String!
}
extend type Query {
getDepartments: [Department]
getDepartmentForProgram(programId: ID!): Department
}
`
With this, children can not get parents directly, it is now a top query. My second concern is to know if this is a good alternative, especially if the first one is a problem.
Thanks in advance.
Share Improve this question edited Nov 20, 2019 at 7:59 acmoune asked Nov 20, 2019 at 7:40 acmouneacmoune 3,4535 gold badges31 silver badges53 bronze badges1 Answer
Reset to default 2This is indeed a potential problem, in that a malicious user can create a very nested query that will hurt your backend. Apollo has a blog detailing this and other potential security concerns here.
As you can read there, there are solutions, for instance capping graphql queries depths, as shown here.
As far as I can tell, your solution is also valid - making queries work in only one way, and implementing the other programatically. The only issue being that it requires you to be diligent in expanding your schema, whereas more automatic solutions may require less attention once implemented (by securing you in runtime or providing tests to stop you from making mistakes).
本文标签: javascriptHow to manage GraphQL query loopStack Overflow
版权声明:本文标题:javascript - How to manage GraphQL query loop? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745581044a2664623.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论