Skip to content

Commit

Permalink
change tc
Browse files Browse the repository at this point in the history
  • Loading branch information
1e16miin committed Aug 30, 2024
1 parent b981d63 commit 32a7511
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 70 deletions.
3 changes: 2 additions & 1 deletion dev-resources/complexity-analysis-error.edn
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
:Seller
{:implements [:Node :User]
:fields {:id {:type (non-null ID)}
:name {:type (non-null String)}
:name {:type (non-null String)
:resolve :resolve-name}
:products
{:type (non-null :ProductConnection)
:args {:first {:type Int}}
Expand Down
147 changes: 78 additions & 69 deletions test/com/walmartlabs/lacinia/complexity_analysis_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -43,107 +43,116 @@
[_ _ _]
nil)

(defn ^:private resolve-name
[_ _ _]
"name")

(def ^:private schema
(utils/compile-schema "complexity-analysis-error.edn"
{:resolve-products resolve-products
:resolve-followings resolve-followings
:resolve-reviews resolve-reviews
:resolve-likers resolve-likers
:resolve-node resolve-node}))
:resolve-node resolve-node
:resolve-name resolve-name}))

(defn ^:private q [query variables]
(utils/simplify (execute schema query variables nil {:max-complexity 10})))
(utils/simplify (execute schema query variables nil {:analyze-query true})))

(deftest over-complexity-analysis
(testing "It is possible to calculate the complexity of a query in the Relay connection spec
by taking into account both named fragments and inline fragments."
(is (= {:data {:node nil}
:extensions {:warnings [{:message "Over max complexity! Current number of resources to be queried: 27"}]}}
:extensions {:analysis {:complexity 32}}}
(q "query ProductDetail($productId: ID){
node(id: $productId) {
... on Product {
...ProductLikersFragment
seller{
id
products(first: 5){
node(id: $productId) {
... on Product {
...ProductLikersFragment
seller{
id
products(first: 5){
edges{
node{
id
}
}
}
}
reviews(first: 5){
edges{
node{
id
author{
id
name
}
product{
id
}
}
}
}
}
reviews(first: 5){
edges{
node{
}
}
fragment ProductLikersFragment on Product {
likers(first: 10){
edges{
node{
... on Seller{
id
}
... on Buyer{
id
author{
id
}
}
}
}
}
}
}
fragment ProductLikersFragment on Product {
likers(first: 10){
edges{
node{
... on Seller{
}" {:productId "id"}))))
(testing "If no arguments are passed in the query, the calculation uses the default value defined in the schema."
(is (= {:data {:node nil}
:extensions {:analysis {:complexity 22}}}
(q "query ProductDetail($productId: ID){
node(id: $productId) {
... on Product {
...ProductLikersFragment
seller{
id
products(first: 5){
edges{
node{
id
}
}
}
}
... on Buyer{
id
reviews(first: 5){
edges{
node{
id
author{
id
}
}
}
}
}
}
}
}" {:productId "id"}))))
(testing "If no arguments are passed in the query, the calculation uses the default value defined in the schema."
(is (= {:data {:node nil}
:extensions {:warnings [{:message "Over max complexity! Current number of resources to be queried: 22"}]}}
(q "query ProductDetail($productId: ID){
node(id: $productId) {
... on Product {
...ProductLikersFragment
seller{
id
products(first: 5){
edges{
node{
id
}
}
}
}
reviews(first: 5){
edges{
node{
id
author{
id
}
}
}
}
}
}
}
fragment ProductLikersFragment on Product {
likers{
edges{
node{
... on Seller{
id
}
... on Buyer{
id
}
}
}
}
}" {:productId "id"})))))
fragment ProductLikersFragment on Product {
likers{
edges{
node{
... on Seller{
id
}
... on Buyer{
id
}
}
}
}
}" {:productId "id"})))))

(comment
(run-test over-complexity-analysis))

0 comments on commit 32a7511

Please sign in to comment.