Is it possible to set the schema programmatically?

To facilitate better answering of questions, if you have a question, please fill in the following info. Otherwise, please delete the template.

What I want to do

I want to define a schema.graphql file in my repository and use that to set the schema in the cloud database programmatically through a script without using the dashboard.
Please list the high level idea of what you want to do

What I did

I tried to do that with the following code:

import { DgraphClient, clientStubFromCloudEndpoint, Operation } from "dgraph-js";
import fs from 'fs'
import path from 'path';
import dotenv from 'dotenv';

const dbURl = process.env.DATABASE_URL || "http://localhost:8080";
const apiKey = process.env.DGRAPH_CLOUD_API_KEY;
const clientStub = clientStubFromCloudEndpoint(dbURl, apiKey)
const dgraphClient = new DgraphClient(clientStub);

async function updateSchema() {
const schemaPath = path.join(__dirname, '../schema/schema.graphql');
const schema = fs.readFileSync(schemaPath, 'utf8')
const op = new Operation();
await dgraphClient.alter(op);


and the following schema:

type Space {
  id: ID!
  name: String!

I get an error message.

Error: 2 UNKNOWN: line 2 column 10: Expected new line after field declaration. Got @

When I set the same schema in the dashboard, it works. What do I do wrong?

All Dgraph’s clients are DQL only. In order to upload your schema you have to use either a GraphQL client(any one in the market) or with some admin procedures like Administrative API - GraphQL

You can also use Postman, Insomnia and others. Even VsCode has an extension to send GraphQL queries.


If using the Dgraph Cloud, like I thought you say recently somewhere, you can use the admin API:

Edit: Cerebro API is for launching and deleting cloud instances