WunderGraph allows you to extend the GraphQL Schema of an origin and replace specific fields with a custom type. This is useful when you're integrating with a GraphQL API that uses custom scalars. Instead of using a generic
JSON scalar, you can replace it with a dedicated type definition to improve type-safety and create a better developer experience.
WunderGraph allows you to directly integrate with Databases like PostgreSQL, MySQL or MongoDB who support JSON data types. Thanks to custom schema extensions, you can give these
JSON scalars much more meaning and make them more intuitive to use.
But that's not all. As we're defining more specific types for the input type as well, we automatically get input validation. With a generic
JSON scalar, users can use any valid JSON value as the input. With Custom Schema Extensions, you'll automatically get JSON Schema validation for the input, while being able to store the value in a JSON/JSONB column.
To enable Schema Extensions, you need to set the
schemaExtension property in the introspection configuration for a data source in the
wundergraph.config.ts file. Additionally, you need to specify which fields should be replaced with a custom type, using the
schemaExtension defines the new type and input type that will be added to the GraphQL schema.
replaceCustomScalarTypeFields defines which fields should be replaced with the new type.
entityName is the name of the GraphQL type either database table or object.
fieldName is the name of the field that should be replaced.
responseTypeReplacement is the name of the type that should be used as the response type replacement. This type must be defined in the
inputTypeReplacement (optional) is the name of the type that should be used as the input type replacement. This type must be defined in the
Let's have a look at an examples.
GraphQL data-source with custom scalar
geography is a custom scalar, so the client has to know how to handle it.
Now the operation looks like this:
Database with JSON/JSONB columns
Let's say have a table
users with a JSONB column
And the record looks like this:
To be able to query the
contact column, we can use the following configuration:
The Query operation will look like this:
Without the Custom Schema Extension, you couldn't be able to select the
As we're generating an input type as well (
db_ContactInput), users cannot use any arbitrary JSON data as input, but need to specify the
REST API with arbitrary JSON type
In a similar way to the previous examples, we can use the introspection configuration to replace the
contact field with a custom type.