How to add type via /alter

Hi

From doc:

To make it work again, add a type definition via the alter endpoint. Let’s assume the node in the previous example represents a person. Then, the basic Person type could be defined as follows:

type Person {
  name: string 
  age: int
}

I have tried:

curl localhost:8080/alter -d '{type Person {
  name: string
  age: int
 }
}'

and:

curl localhost:8080/alter -d {type Person {
  name: string
  age: int
 }
}

and:

curl localhost:8080/alter -d 'type Person {
  name: string
  age: int
 }
'

I’m not sure what is the question, but here is a doc for alter via Raw HTTP Get started with Dgraph

These typos are related to Curl usage. You can’t use the data flag without apostrophe or a file path.

see more in curl - How To Use
And http://www.mit.edu/afs.new/sipb/user/ssen/src/curl-7.11.1/docs/curl.html

-d/–data
(HTTP) Sends the specified data in a POST request to the HTTP server, in a way that can emulate as if a user has filled in a HTML form and pressed the submit button. Note that the data is sent exactly as specified with no extra processing (with all newlines cut off). The data is expected to be &zerosp;“url-encoded”. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F/–form. If this option is used more than once on the same command line, the data pieces specified will be merged together with a separating &-letter. Thus, using ‘-d name=daniel -d skill=lousy’ would generate a post chunk that looks like &zerosp;‘name=daniel&skill=lousy’.

If you start the data with the letter @, the rest should be a file name to read the data from, or - if you want curl to read the data from stdin. The contents of the file must already be url-encoded. Multiple files can also be specified. Posting data from a file named ‘foobar’ would thus be done with --data @foobar".

To post data purely binary, you should instead use the --data-binary option. -d/–data is the same as --data-ascii. If this option is used several times, the ones following the first will append data.
Source: http://www.mit.edu/afs.new/sipb/user/ssen/src/curl-7.11.1/docs/curl.html

Thanks @MichelDiz

Sorry I missed the actual question and I will do it like this

But I still think the documentation is a little misleading, as I got/get to think that it is for the /alter endpoint(“doc: via the alter endpoint”):

type Person {
  name: string 
  age: int
}

As it looks more something for the /graphql endpoint.

Our type system is very similar to GraphQL but is not GraphQL. Also, It gonna change a bit in the next releases.

@MichelDiz
Yes, so right.

The
https://docs.dgraph.io/query-language/#setting-the-type-of-a-node

This is for mutating, how would you or is it possible to create a type via /alter with something like?:

type Person {
  name: string 
  age: int
}

Hmm, this https://tour.dgraph.io/schema/1/#
gives me “TypeError: Not allowed to request resource”

Ok, totally stuck:

curl -X POST localhost:8080/alter -d 'type Person {

name: string

}'

{"errors":[{"message":"Schema does not contain a matching predicate for field name in type Person","extensions":{"code":"Error"}}]} **%**

@MichelDiz
Ok, I got it working - I realised I was missing “matching predicate” from the error message.

1 Like

hey JCzz, check this PR
https://github.com/dgraph-io/dgraph/pull/4382

You need to do something like this

curl -X POST localhost:8080/alter -d \
'name: string @index(term) .
type Person {
   name
}'

Note that the error msg says “Schema does not contain a matching predicate for field name” this means you didn’t define the “name” pred before using it.

1 Like

Hi @MichelDiz
Yes, you are totally right - thanks! And looks good with the pull request.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.