Write schema from playground YAML
# spicedb
s
Hello, it is me again, so I have found this api function for writing schemas https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.SchemaService.WriteSchema it takes a string as a schema, the thing is I have a file that I would load into java and then pass it to this function, the file was created on the authzed playground so it should be following the general file structure. But when I pass it I get the following answer: Failed to write schema: INVALID_ARGUMENT: parse error in
schema
, line 1, column 1: Unexpected token at root level: TokenTypeIdentifier This is the code I am using to pass it to the api
Copy code
java
    private static void createSchema() {

        String schema = "";
        try {
                schema = new String(Files.readAllBytes(Paths.get("schemas_example/final.yaml")));
        } catch (IOException e) {
                e.printStackTrace();
        }

        WriteSchemaRequest request = WriteSchemaRequest.newBuilder()
                .setSchema(schema)
                .build();

        try {
                WriteSchemaResponse response = schemaService.writeSchema(request);
                System.out.println(response.toString());
        } catch (Exception e) {
                System.out.println("Failed to write schema: " + e.getMessage());
        }
        
    }
Is there a special way I should be parsing this or maybe a different API function? the schema starts with schema: |- as it does when I download it from the playground I see there is a bulk import relationships, but I am looking for something where I could just pass the file if possible
v
Hi 👋 the YAML from the playground is not supported via
WriteSchema
. You need to extract the
schema
field, that's what you want to put in
WriteSchemaRequest.Schema
The YAML from the playground is supported as a startup argument for
spicedb server
command, though
so you can start a server from the contents of your playground YAML
s
yes, the general idea of my java app (that will later be used as service) is to on startup check if a schema exists and if not load it in. But I can work around this, another question. Can the writeSchema be used to load the whole schema definition or do I need to do it resource by resource?
if so this should be fine, I will just split my file into schema and relationships and then load using writeSchema and bulkRelationships
v
you can send the whole schema as part of
WriteSchema
and it is in fact expect to be that way. If you miss any definitions, they will be considered deleted from the schema
s
that is perfect, thank you!
just a question, how would you recommend I approach loading the relationships in, can I somehow use a similar approach or? since like I said I had the playground file it has about 90 relationships that I need to load after loading the schema, I have them in the format of the yaml file, maybe somehow copying them out like the schema, but what would you say would be the best approach?
v
It depends on how you are running SpiceDB. If this is just a demo, you can mount the yaml file into the SpiceDB container, and use the
--boostrap
flags to start it up with the schema and rels from the YAML file
alternatively you can use
zed
command line to run a one-off command, as it understands the playground YAML and can write the schema and relationships for you
Copy code
Use:   "import <url>",
    Short: "import schema and relationships from a file or url",
    Example: `
    From a gist:
        zed import https://gist.github.com/ecordell/8e3b613a677e3c844742cf24421c08b6
    From a playground link:
        zed import https://play.authzed.com/s/iksdFvCtvnkR/schema
    From pastebin:
        zed import https://pastebin.com/8qU45rVK
    From a devtools instance:
        zed import https://localhost:8443/download
    From a local file (with prefix):
        zed import file:///Users/zed/Downloads/authzed-x7izWU8_2Gw3.yaml
    From a local file (no prefix):
        zed import authzed-x7izWU8_2Gw3.yaml
    Only schema:
        zed import --relationships=false file:///Users/zed/Downloads/authzed-x7izWU8_2Gw3.yaml
    Only relationships:
        zed import --schema=false file:///Users/zed/Downloads/authzed-x7izWU8_2Gw3.yaml
    With schema definition prefix:
        zed import --schema-definition-prefix=mypermsystem file:///Users/zed/Downloads/authzed-x7izWU8_2Gw3.yaml
s
this is the thing, it is a demo, but we are deciding to overcomplicate things (idk why), anyway what I am doing is basically automating that if spicedb has been started without a schema in it. My app checks that and automatically loads the schema and permissions into it. The app is in java. So I would struggle using the cli
we are basically building a failsafe, if the first start with file fails, the service (spring-boot) loads a schema and perms in
do you maybe have some kind of example available or something on using this? https://buf.build/authzed/api/docs/main:authzed.api.v1#authzed.api.v1.BulkImportRelationshipsRequest Seems like maybe I could use it to load my relationships, but from the docs I don't really get how I would pass my 90 relationships to it, so some kind of example would be pretty helpful
v
you can use it to load your relationships yes. All gRPC requests are documented in our API docs, and what the client call looks like is language specific. I'm sorry but I don't have an example around.
s
thank you
3 Views