Giter VIP home page Giter VIP logo

Comments (8)

varrodan avatar varrodan commented on April 28, 2024 1

One can provide reasonably faithful cost estimations for evaluating a graph query, if the average branching factors of edge types (i.e. fields) can be calculated from the contents of the data store.

In case of the Star Wars example, if you have a query like
hero { name friends { name } }
then you can estimate the number of friends expected to be retrieved by calculating the average branching factor for friends which is (4+1+3+4+1+4+3)/7 = 2.85 in case of the Star Wars knowledge base. One can further customize this estimate for friends edges running between specific types of nodes.

In case of nested queries, like
{ hero { name friends { name appearsIn friends { name } } } }
one can estimate the size of the expected result set by a sum-product.
The result set is expected to contain

  • H nodes for heros, plus
  • H * F nodes by navigating along friends from each hero (where F=2.85 above), plus
  • H * F * A nodes by further navigating along appearsIn from each of the previous nodes.

We described this estimate 10 years ago in an academic paper, so better estimates are likely to exist since then.

(I am new to GraphQL as a language, sorry if I misused its terminology - or gave a far too academic answer...)

from graphql-spec.

leebyron avatar leebyron commented on April 28, 2024

I think this would be an awesome project for someone to start up if anyone in the community is looking for ideas.

from graphql-spec.

AndrewIngram avatar AndrewIngram commented on April 28, 2024

I just found out that this has been implemented in Sangria:

http://sangria-graphql.org/learn/#query-complexity-analysis

To that end, it might not be necessary to define it in the spec, and leave it up to implementations instead.

from graphql-spec.

dylanahsmith avatar dylanahsmith commented on April 28, 2024

I think this provides too narrow a view of how the cost might be calculated. For instance, the cost would at least depend on the arguments in a lot of cases, like of a paginated field (e.g. users(first: $count)). The cost might also depend on other similar selections, which might even re-use the same data, e.g. when using dataloader to batch load and cache datastore queries.

Trying to calculate the cost of a query before it is made can actually be quite difficult, so another possible approach would be to use time as the unit of cost, where a timeout would be used to abort expensive queries.

I think it should just be an implementation detail.

from graphql-spec.

leebyron avatar leebyron commented on April 28, 2024

This is great additional context and insight, thanks!

from graphql-spec.

dylanahsmith avatar dylanahsmith commented on April 28, 2024

I wouldn't put faith in cost estimations that rely on averages with respect to branching factors. The average might be very low but if you are trying to protect against abuse, then you really want to pay attention the upper bound on the branch factor.

Also, be careful about calculating the cost of queries that require server-side filtering, since they could scan a large number of objects and return very few results. The cost of a request isn't always about the amount of data that is returned.

Maybe this discussion can be moved to the graphql-js repo, since it doesn't seem like something to standardize in the graphql spec.

from graphql-spec.

stanams avatar stanams commented on April 28, 2024

Here's an interesting package that does a pretty good cost analysis for graphql queries: https://github.com/pa-bru/graphql-cost-analysis

from graphql-spec.

leebyron avatar leebyron commented on April 28, 2024

Closing this aging issue.

Seems like the consensus is that field cost alone is not always enough to estimate a queryโ€™s cost. Different approaches by different libraries indicate that there are many useful approaches with different trade offs, so there is not much to incorporate into the spec to account for this

from graphql-spec.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.