javascript - How to manage GraphQL query loop? - Stack Overflow

Let's say we have this GraphQL Schema:gql`type Department {id: ID!name: String!programs(programId:

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 badges
Add a ment  | 

1 Answer 1

Reset to default 2

This 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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745581044a4634257.html

相关推荐

  • javascript - How to manage GraphQL query loop? - Stack Overflow

    Let's say we have this GraphQL Schema:gql`type Department {id: ID!name: String!programs(programId:

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信