graphql-ppx is offering some directives to customize how the ppx is handling
operations or fragments.
If you add the directive ppxAs to a GraphQL object, it will be casted using that record. Be careful this is unsafe and should only be used as an excape hatch.
ppxCustom is a great way to transform the output of a Query to a value that is
more useful. For instance,
custom scalars are always
Js.Json.t, we could use custom scalars for instance for dates. To make them
directly useful in our apps, using the
ppxCustom directive we can make a
little annotation to convert this to an actual date. Here is an example.
As you can see we need to define a type
t and two functions,
serialize. These two functions are to go from
t and how to go
t back to
In customFields in
bsconfig.json you can
actually configure certain types (such as the custom date type, that always
gets converted, so you never have to use the
@ppxCustom annotation for them.
It's even possible to apply this to other than scalars. Any GraphQL value can
"customized". The problem is that if you'd like to transform a value from a type
defined in the generated module, to your own type, this sets up a recursive
relationship between those module. In Reason you need to explicitly mark these
two modules as recursive. However it is possible to use the
point as a recursive module. In below example we transforming a user record into
a customized record:
For enums and unions we always generate a
#FutureAddedValue(_) variant to make
sure that your app doesn't raise exceptions if enums are unions are added to the
GraphQL schema at some later point in time. This is necessary to make sure your
app is typesafe and doesn't run into runtime exceptions. If you are absolutely
sure that the schema won't change, you can add this directive to remove this
If you've got an GraphQL object which in practice behaves like a variant, where
you either get a user or a list of errors - you can add a
directive to the field to turn it into a polymorphic variant:
This helps with the fairly common pattern for mutations that can fail with user-readable errors.
If you'd like to specify which field the fragment uses in the record, you can do
it by using
@ppxField. (By default it used the name of the fragment with the
first letter decapitalized.)
The standard skip directive is also supported within GraphQL ppx. This makes sure the field is always an option type.
The standard include directive is also supported within GraphQL ppx. This makes sure the field is always an option type.
Convert a specific field to a record when graphql-ppx is generating objects instead of record. It is marked as deprecated because in a future release support for objects is going to be removed.
If the default is to generate objects, using this directive you can force a record to be generated. It is marked as deprecated because in a future release support for objects is going to be removed.
This was the old name for