Adding directives to the schema in code-first GraphQL servers
Blog post from LogRocket
GraphQL servers can be created using two main approaches: SDL-first and code-first, each with its own advantages and disadvantages, particularly in handling schema-type directives. While SDL-first servers naturally support schema-type directives through the schema definition language, code-first servers face challenges since they lack an SDL, leading to difficulties in implementing directives like @deprecated. Despite graphql-js's decision not to support registering directives via code, some GraphQL servers, such as Gato GraphQL, circumvent this limitation by employing an IFTTT (if this, then that) approach, allowing directives to be added to queries at runtime. This strategy involves treating directives as middleware within a directive pipeline, transforming requested queries into executable ones by embedding directives like @validate and @resolveValueAndMerge. By leveraging IFTTT, the approach enables flexible API configurations, allowing developers to attach directives to specific fields or actions, enhancing the server's functionality without hardcoding directives into the schema. Additionally, the use of server-side aliases further refines the schema's versatility, accommodating multiple configurations and improving schema readability.