oslabs-beta / GraphQL-Gate

A GraphQL rate limiting library with query complexity analysisfor Node.js

Home Page:http://graphqlgate.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement a function to calculate the complexity of a field returning a list of determinate size. Update tests

evanmcneely opened this issue · comments

See the notes below for implementation. The type weight object used in testing should be updated to reflect the implementation.

  • type weight object used for all complexity analysis tests
  • buildTypeWeights test 'fields returning lists of objects of determinate size'
  • buildTypeWeights test 'fields returning lists of objects of indeterminate size'

Notes

The end goal is to have a function that can be used to calculate the total type complexity for a field by multiplying the arg value times the weight of the Resolved Type.

For example, if we receive a query reviews(episode: 'NEWHOPE', first: 5), the type complexity algorithm would call the function with the value of first the weight of a Review to get 5 * 1 = 5;

This function could be global that receives both arguments or a unique function could be assigned to each applicable field as below.

When looking up a field weight, we would check the type of the value. If the weight is a number, simply return the number otherwise call the associated function with relevant args.

We will also need to determine if the relevant information (variable names and resolve types) is available in the GraphQLSchemaObject or the AST created by parsing the schema object

first, last, limit are conventional keywords for limiting results. Later on we could make these keywords configurable to provide a more unopinionated solution.

Query {
  weight: 1,
  fields: {
    reviews: (type) => args[multiplierName] * typeWeightObject[type].weight
  },
}, 

Review {
  weight: 1,
  fields: {
    stars: 0,
    commentary: 0,  
  }
}

This is partially addressed by #33 which handles list of determinate size. The current implementation creates a weight function whenever one of the arguments is first, last, or limit. The produced function accepts an ArgumentNode[] and then computes the weight based on the limiting arg value and the weight of the resolve type.

The following task should be completed as well.

  • Support variables while parsing arguments
  • Add test in buildTypeWeights to test implementation of the weight function

Unbounded lists are not supported at this time.

completed