Core Helpers
#
prepassA common pattern to do while using gqless
is to just access manually some specified fields, so gqless
can pick up your requirements faster.
That allows preventing some possible "waterfalling", or simply to make sure to get all the fields requested in the same query,
specially while using conditionals on possible nulls in your GraphQL Schema.
For that use case we offer a very useful function called prepass
, in which you can give it an object or array, and given some paths, it will traverse them,
with some specific features:
- You can specify the path with different syntaxes
"user.dog.name"
,["user", "dog", "name"]
or["user", { field: "pet", variables: { type: "dog" } }, "name"]
- You don't have to specify an array index, since if it finds an array, it will pass-through the first non-null value of it
- If you have a field with arguments that you want to access, you can also specify it's value and it will automatically call it's function with that value.
The only disadvantages we see are basically:
- If it finds a function, and you don't specify it's variables, it can't check if all variables are actually optional, so it will call the function anyways without any variable, and that might result in a GraphQL error.
- It doesn't have autocomplete 😔
#
selectFieldsGiven an object or an array of objects, create a new object/array with the specified fields.
#
Features and Limitations- Allows to select every field of the proxy with a simple
"*"
. - But when it encounters a field with arguments, it only returns the function back, since it can't assume your intent, even if all arguments are optional.
- Specified keys can be composed via
foo.bar
- Specify recursive depth (
1
by default) - Bad Type-safety, it just returns the same type as the input
#
Examples#
getFieldsGiven an object, pre-access the specified fields, and returns the object back.
#
Features and Limitations- Good Type-Safety
- Allows keeping the proxies around.
- If no keys are specified, it selects all the scalars of the object
- You can only specify the first level of scalars
- It doesn't do anything for objects inside objects
- It skips fields with arguments
#
getArrayFieldsWe also have a helper to work with arrays, and works the same way as getFields
.
#
Type CastersIf you find too annoying having to deal with undefined
everywhere when
by logic you can be completely sure that in runtime you are not dealing with actual undefined
s, gqless
also exports no-op functions
with useful types that removes your undefined
s.
caution
Use with caution, since you might encounter runtime errors if they are used unwisely
tip
You can also use the types that are being used for this functions for types-only usage.
#
castNotSkeletonRemoves all the undefined
s in 1 level of your object or array.
This next example is a perfectly safe place to use it, since here id
and name
will always be actual values in it's return value, not undefined
.
#
castNotSkeletonDeepRemoves all the undefined
s recursively of your object or array.
This next example is a perfectly safe place to use it, since here all the fields returned will always be actual values in it's return value, not undefined
.