Is there an example of uploading a graphql schema with POST using node request or axios instead of Curl?

Here is what is currently working for us using fetch. There would be less need to convert to/from json with axios in general, but we also had to convert the schema.toString() before pushing it. We don’t have any @key fields, but we do have other directives and this is working for us. Maybe this will help get you going.


const updateSchemaQuery = `mutation($schema: String!) {
  updateGQLSchema(input: { set: { schema: $schema } }) {
    gqlSchema {
      schema
    }
  }
}
`
async function pushSchema() {
  // try {
  const data = await fs.promises.readFile('./src/schema.graphql', 'utf8')
  const schema = data.toString()

  const response = await fetch(`https://YOUR-ENDPOINT.us-west-2.aws.cloud.dgraph.io/admin`, {
    headers: {
      'Content-Type': 'application/json',
      'X-Auth-Token': 'XXXXX',
    },
    method: 'POST',
    body: JSON.stringify({
      query: updateSchemaQuery,
      variables: {
        schema: schema,
      },
    }),
  })
  console.log(response.statusText)
  
  const json = await response.json()
  
  if (json.errors) {
    json.errors.forEach((error) => console.log(error.message, error.locations))
    console.error(json.errors)
    throw JSON.stringify(json.errors)
  }
  //console.log(json)
  return json
}

2 Likes