Last active
December 8, 2020 09:28
-
-
Save crisu83/d1bf18368b3667410853b0a3a2e06cde to your computer and use it in GitHub Desktop.
Scripts for pulling and pushing schema to and from DGraph.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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