profusion / sgqlc

Simple GraphQL Client
https://sgqlc.readthedocs.io/
ISC License
506 stars 85 forks source link

Contextual control of auto select depth #203

Open dustymugs opened 2 years ago

dustymugs commented 2 years ago

This is purely a hack so as to contextually control DEFAULT_AUTO_SELECT_DEPTH as the default value of 2 may not be sufficient when sending the request and merging the response into the Operation object.

Example schema:

type Query {
        ...
    findProducts(input: ProductSearchInput): ProductSearchResults!
       ...
}

type ProductSearchResults {
    results: [ProductSearchResult!]!
    more: Boolean!
    nextLimit: Int!
    nextOffset: Int!
}

type ProductSearchResult {
    product: Product!
    instances: [Instance]!
}

type Product {
    id: ID!
    internalName: String!
    displayName: String
    description: String
    license: String
    reference: Json
    dataSteward: Json
    tags: [Tag!]!
    releases: [Release]!
}

type Instance {
    id: ID!
    productId: ID!
    productName: String!
    effectiveDateRange: DateRange
    ignoreTimezone: Boolean
    tags: [Tag!]
    reference: Json
    extent: GeoJson
    assets: [Asset!]!
    parents: [Instance!]
}

And a basic consumption...

op = Operation
op.find_products(input=...).__fields__()

results = op + endpoint.query(op)

With the default depth of 2, Product.tags and Product.releases (amongst other fields) are not included in the merged results.

With this PR, I can safely do the following and get Product.tags and Product.releases in the results

with select_depth(3):
  op = Operation
  op.find_products(input=...).__fields__()

  results = op + endpoint.query(op)

Obviously, a cleaner approach would be lovely (e.g. Operation(Query, max_select_depth=10) but this is the dirty solution to achieve my needs

barbieri commented 2 years ago

hi @dustymugs, why don't you simply pass the argument to the functions?

Other than that, I highly recommend to NOT use auto-selection since it goes against the GraphQL principles. It will bring you lots of data, it will make your code more subject to breakages and so on.

Note this is a pure "dev only" thing I added to help quick hacks... I regret adding it when I see some "misuses" :-P