Graphql dgraph schema error

Hello, I am following the quick start at graphql.dgraph.io

When I try to add the schema using curl, I get the following error…

C:\jq>jq -n --arg schema “$(cat schema.graphql)” ‘{ query: “mutation addSchema($sch: String!) { addSchema(input: { schema: $sch }) { schema { schema } } }”, variables: { sch: $schema }}’ | curl -X POST -H “Content-Type: application/json” http://localhost:9000/admin -d @- | jq -r
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Windows cmd shell quoting issues?) at , line 1:
'{
jq: 1 compile error
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 130 100 130 0 0 130 0 0:00:01 --:–:-- 0:00:01 4193
{
“errors”: [
{
“message”: “Not a valid GraphQL request body: EOF”
}
],
“extensions”: {
“requestID”: “78577a16-8999-49c3-b2c9-c2740d1288b0”


This is my schema, I copied the instructions exactly as asked and pasted this into a schema.graphql file

type Product {
productID: ID!
name: String @search(by: [term])
reviews: [Review] @hasInverse(field: about)
}

type Customer {
custID: ID!
name: String @search(by: [hash, regexp])
reviews: [Review] @hasInverse(field: by)
}

type Review {
id: ID!
about: Product! @hasInverse(field: reviews)
by: Customer! @hasInverse(field: reviews)
comment: String @search(by: [fulltext])
rating: Int @search
}

Hi, thanks for reporting this.

Are you using powershell ?

I have tried both command prompt and power shell.

This is the error from powershell (ran as admin)

PS C:\jq> jq -n --arg schema “$(cat schema.graphql)” ‘{ query: “mutation addSchema($sch: String!) { addSchema(input: { schema: $sch }) { schema { schema } } }”, variables: { sch: $schema }}’ | curl -X POST -H “Content-Type: application/json” http://localhost:9000/admin -d @- | jq -r
At line:1 char:264

Unrecognized token in source text.
+ CategoryInfo : ParserError: (:slight_smile: , ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnrecognizedToken

I also had trouble with the example graphql dgraph setup on Windows, with similar errors. I’m pretty new to this, but here’s some of the "watch out"s I found - not sure if it applies to your case or not…

  • I had a type with no defined attributes of its own (implemented an interface), which dgraph didn’t like
  • It was sensitive to single vs double quotes (e.g. ‘{ query: …’ had to be “{ query: …” Everything in the “mutation addSchema…” still had to be double quotes too, so I had to escape those as well)
  • I couldn’t figure out setting and getting Windows command prompt variables, so I wound up outputting the cleaned schema (with newline characters removed) to a temporary text file, and read that in as a variable to jq

I’m using node, so below is the final script that I wound up adding to my package.js:
"dgraph:migrate": "cat db-schema.graphql | tr '\\n' ' ' > db-schema-string.txt && jq -n --rawfile schema db-schema-string.txt \"{query: \\\"mutation addSchema($sch: String!) { addSchema(input: { schema: $sch }) { schema { schema } } }\\\", variables: { sch: $schema }}\" | curl -X POST -H \"Content-Type: application/json\" http://localhost:9000/admin -d @- | jq -r"