$ show taion/graphql-type-json
This repository provides two custom GraphQL scalar types, `JSON` and `JSONObject`, for use with GraphQL.js. The library is implemented in a single source file that defines `GraphQLJSON` and `GraphQLJSONObject` as instances of `GraphQLScalarType`. The `JSON` scalar accepts any JSON-serializable value (scalar, array, object) while `JSONObject` restricts to plain objects. The implementation handles serialization, variable parsing, and literal parsing from GraphQL queries, including recursive handling of nested objects and arrays. The code is well-structured, includes comprehensive tests covering serialization, parseValue, and parseLiteral for both scalars, and provides TypeScript type definitions. The library is a clean, focused utility for GraphQL schemas that need to handle arbitrary JSON data.
The LLM identified 1 files worth highlighting for reuse:
The single source file that implements both GraphQLJSON and GraphQLJSONObject scalar types. It demonstrates the correct pattern for creating custom GraphQL scalars: implementing serialize, parseValue, and parseLiteral. The parseLiteral function recursively handles all AST kinds (string, boolean, int, float, object, list, null, variable) using a switch on Kind. The ensureObject helper provides validation for JSONObject. This code is a clean, reusable reference for anyone implementing custom GraphQL scalars, and the library itself can be directly consumed as an npm package.