GraphQL dynamic query strategy best practices

What is a good strategy for dynamically building a query schema for objects and fields with Apollo Client and GraphQL?

We have T-Shirt, Pants, and Shoes objects in our schema. Each of these vary on the types of fields readily available.

There are two input fields, a single entity drop down for TShirt, Pants, and Shoes, and a multi-select drop down to select from the fields available from the schema.

The query schema is produced based on the user’s input.

I have it working with interpolation manipulation . However, you could imagine as the fields grow in abundance so does the amount of skip directives . Is there maybe a way to use @skip or @include to check if a GraphQL variable with a list of strings includes the field name?

const schema = gql
 `query {
    tshirt {
      logo
      brand
    }
    
    pants {
      length
      wasteSize
    }

    shoes {
      lacesStyle
      color
    }
  } 
}`
query {
    tshirt($logoSkipVal: Boolean! = false, $brandSkipVal: Boolean! = false) {
      logo @skip(if: $logoSkipVal)
      brand @skip(if: $brandSkipVal)
    }
} 
1 Like