Skip to content

Instantly share code, notes, and snippets.

@crisu83

crisu83/pull-schema.ts

Last active Dec 8, 2020
Embed
What would you like to do?
Scripts for pulling and pushing schema to and from DGraph.
import {writeFileSync} from 'fs';
import fetch from 'isomorphic-unfetch';
const [, , schemaPath] = process.argv;
const dgraphUrl = process.env.DGRAPH_URL;
const dgraphToken = process.env.DGRAPH_TOKEN;
if (!dgraphUrl) {
throw new Error('You need to specify `DGRAPH_URL` in your environment.');
}
if (!dgraphToken) {
throw new Error('You need to specify `DGRAPH_TOKEN` in your environment.');
}
const pullSchema = async (url: string, token: string, schemaPath: string) => {
const query = {
query: `
query GetGQLSchema {
getGQLSchema {
schema
}
}`,
variables: {},
};
console.log(`Pulling schema from "${url}".`);
const result = await fetch(`${url}/admin`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token,
},
body: JSON.stringify(query),
});
const {data} = await result.json();
const path = `${process.cwd()}/${schemaPath ?? 'dgraph-schema.graphql'}`;
console.log(`Writing schema to "${path}".`);
writeFileSync(path, data.getGQLSchema.schema);
};
pullSchema(dgraphUrl, dgraphToken, schemaPath);
import {readFileSync} from 'fs';
import fetch from 'isomorphic-unfetch';
const [, , schemaPath] = process.argv;
const dgraphUrl = process.env.DGRAPH_URL;
const dgraphToken = process.env.DGRAPH_TOKEN;
if (!dgraphUrl) {
throw new Error('You need to specify `DGRAPH_URL` in your environment.');
}
if (!dgraphToken) {
throw new Error('You need to specify `DGRAPH_TOKEN` in your environment.');
}
const pushSchema = async (url: string, token: string, schemaPath: string) => {
const path = `${process.cwd()}/${schemaPath ?? 'dgraph-schema.graphql'}`;
console.log(`Reading schema from "${path}".`);
const schema = readFileSync(path, 'utf8');
const query = {
query: `
mutation UpdateGQLSchema($sch: String!) {
updateGQLSchema(input: { set: { schema: $sch } }) {
gqlSchema {
schema
}
}
}`,
variables: {sch: schema},
};
console.log(`Pushing schema to "${url}"`)
await fetch(`${url}/admin`, {
body: JSON.stringify(query),
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': token,
},
method: 'POST',
redirect: 'follow',
});
};
pushSchema(dgraphUrl, dgraphToken, schemaPath);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment