Hi,
Problem
The GraphQL-types for ConstentSelector
and ImageSelector
is Content
. Which is a very wide type that encompasses all content-types in the system, when in reality the content can only be of one or two types.
This is especially unelegant when the data in reality only can be of one type, and we have to use an inline fragment to query the data, instead of just doing it directly.
Problem 2
I just shared a post about how to Generate frontend TypeScript-types from GraphQL Schema.
It is a very useful technique, but it would be even better if the Schemas would be a bit more precise.
My big problem is that GraphQL-queries generate MegaBytes of unnecessary TypeScript-types that are hard to work with.
That is why I’m “hacking” this myself, by overwriting the GraphQL-types of ContentSelectors
and ImageSelectors
with more presise types, like this:
import {
createWebSocketData,
initWebSockets,
createSchema,
type Context
} from "/lib/guillotine";
import {
reference,
list,
execute as executeGraphQL,
type GraphQLResolverEnvironment,
type CreateObjectTypeParams,
} from "/lib/graphql";
...
const schema = createSchema({
applications: ["com.nerdforge"],
creationCallbacks: {
// ARTICLE DATA
com_nerdforge_Data: (context: Context, params: CreateObjectTypeParams) => {
// Authors is a ContentSelector, with two "allowContentTypes"
params.fields.authors.type = list(
context.schemaGenerator.createUnionType({
name: context.uniqueName("com_nerdforge_Article_Data_Authors"),
types: [
reference("com_nerdforge_Employee"),
reference("com_nerdforge_ExternalPerson")
],
typeResolver: (content: Content) => context.contentTypeMap[content.type] ?? context.types.untypedContentType,
})
);
// ImageSelector always has type "media_Image"
params.fields.image.type = reference("media_Image");
},
I wish that Guillotine would do this automatically for me!
Solution for ImageSelector
So I suggeset changing the type for ImageSelector
in Guillotine to reference("media_Image")
instead of reference("Content")
.
Solution for ContentSelector
Take allowContentType
into account, and set the GraphQL type of that field to be either the one allowed content type, or a union
of the allowed content types.
Thanks,
– Tom