How can I import a Schema in graphql file using API

Hi, I am trying to configure my schema without using the Dgraph Cloud UI. I’ve been looking for this solution in the documentation, but I can’t find it and I’m a bit stuck with this problem.

The thing is that I’ve found the solution but only when is Dgraph is self hosted. In this article, It sais that you can create a graphql file and set your schema inside, and then using this request curl -X POST localhost:8080/admin/schema --data-binary '@schema.graphql' you can set it by http protocol but, when you are using cloud that endpoint does not exist. They gave you the following solution but, is there any way of combining this two? I do not have much experience using Dgraph and curl.

#!/usr/bin/env bash

DEPLOYMENT_URL="polished-violet.us-east-1.aws.cloud.dgraph.io"
DEPLOYMENT_JWT="<deployment-jwt>"

curl "https://${DEPLOYMENT_URL}/admin" \
  -H "Content-Type: application/json" \
  -H "X-Auth-Token: ${DEPLOYMENT_JWT}" \
  --data-binary '{"query":"mutation($sch: String!) {\n updateGQLSchema(input: { set: { schema: $sch } })\n {\n gqlSchema {\n schema\n }\n }\n}","variables":{"sch": "type Person { name: String! }"}}' \
  --compressed

Thank you.

have you tried https://${DEPLOYMENT_URL}/admin/schema?

Hi @smolto you’re right that in Dgraph Cloud the endpoint /admin/schema isn’t available. Building on top of your script, you can use the following command using both curl and jq to read your schema.graphql file and load it via the /admin endpoint.

#!/usr/bin/env bash

DEPLOYMENT_URL="polished-violet.us-east-1.aws.cloud.dgraph.io"
DEPLOYMENT_JWT="<deployment-jwt>"

jq -n --rawfile schema schema.graphql '{ query: "mutation updateGQLSchema($sch: String!) { updateGQLSchema(input: { set: { schema: $sch }}) { gqlSchema { schema } } }", variables: { sch: $schema }}' | 
    curl "https://${DEPLOYMENT_URL}/admin" \
         -H "Content-Type: application/json" \
         -H "X-Auth-Token: ${DEPLOYMENT_JWT}" \
         --data-binary @- \
         --compressed

Hi guys, /admin/schema is now available in cloud. Do let me know if face any issues.

1 Like