Add an external function to pkl #1071
-
We have strings in our configs and want to be able to parse graphql from those strings and check things in PKL We don't really want or think it would make sense to create a graphql parser in pkl and given one already exists in kotlin, is it possible to add our own custom function into PKl that can call existing kotlin code? Or is this kind of extensibility not possible |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
There are two primary ways to extend Pkl with functionality like this: language binding libraries and external readers. Using a full language binding library provides Pkl evaluation as an API to your own codebase. Your application can register custom resource or module schemes via that API that your Pkl code can use via External readers are small-standalone binaries that Pkl spawns during evaluation to implement functionally identical extensions. Instead of hosting the evaluator via a language binding library, the standard In either case, your reader code would likely parse graphql into some sort of JSON representation that Pkl can parse and use. Usage would be something like this: import "pkl:json"
gqlFile = read("./path/to/schema.gql") // read the file in Pkl so we can do nice relative reads
gqlJson = read("gql2json:\(gqlFile.base64)") // pass the data through to your reader
gqlData = new json.Parser {}.parse(gqlJson) // parse the result as JSON and then use it! |
Beta Was this translation helpful? Give feedback.
There are two primary ways to extend Pkl with functionality like this: language binding libraries and external readers.
Using a full language binding library provides Pkl evaluation as an API to your own codebase. Your application can register custom resource or module schemes via that API that your Pkl code can use via
read
orimport
constructs. The disadvantage of this approach is that you can no longer use thepkl eval
CLI to test and debug your modules.External readers are small-standalone binaries that Pkl spawns during evaluation to implement functionally identical extensions. Instead of hosting the evaluator via a language binding library, the standard
pkl eval
CLI is used and the…