graphql-cost-analysis
graphql-cost-analysis copied to clipboard
Questions about complexity and batch operations
I've been looking at the documentation, and this seems like a decent tool for our use case.
However, we have certain complexity characteristics, that I'm not sure if we can calculate correctly. Please advise if these scenarios are supported:
1. Groups of fields with combined complexity
We're using apollo server as a gateway, that resolves all queries by making API calls to a back-end. For some types, there are some fields that are always included with the resource (basically zero cost), and there are other fields that require an additional round-trip to the back-end. If one or more of these fields are included, we need to make that extra round-trip, so basically any additional field will have zero cost:
type User {
# "zero-cost" fields
id: String!
name: String!
email: String!
# one or more of these fields will add a cost, but if we include one,
# it doesn't matter if we include all of them
address: String!
phone: String!
birthDate: String!
}
We want a significant complexity value to be added, if any of the fields address
, phone
or birthDate
are included in the query, but we don't want it to add up, if several of these fields are included.
2. Batch requests for nested resources
Another scenario is like this:
type Query {
allUsers: [User!]!
}
type User {
id: String!
messages: [Message!]!
}
type Message {
# always included
id: String!
text: String!
# extra round-trip needed
seenBy: [User!]!
}
In this scenario, we have a back-end API method that returns all users. We have another API method that returns all messages to all users. If we have a query like this:
{ allUsers {
id
messages { id text }
}
We only need to make two API calls. So the cost of including the messages
doesn't depend on the number of users. This seems like a use-case for setting useMultipliers
to false
.
However, if we include the seenBy
field, it will trigger an extra API call for every message, so in that case, we do want multipliers for the number of users and messages to apply. Is this scenario possible with this library?