How to add @id directive

To facilitate better answering of questions, if you have a question, please fill in the following info. Otherwise, please delete the template.

What I want to do

I want to use uniq field like username according https://dgraph.io/docs/graphql/schema/ids/

Please list the high level idea of what you want to do
To have unique vertexes (like person e.g.) that adding independently in parallel threads (or in even servers) in one moment.

What I did

Please list the things you have tried.

const Schema = `
		username: string  @index(term, fulltext) @upsert @count .
		type Username  {
	   		username: String! @id
        }
`

`	if err := dg.Alter(context.Background(), &api.Operation{
		Schema:          Schema,
		RunInBackground: false,
	}); err != nil {
		return err
	}`

I’m getting error:
panic: rpc error: code = Unknown desc = line 34 column 24: Expected new line after field declaration. Got @

Dgraph metadata

dgraph version

Dgraph version : v21.12.0
Dgraph codename : zion
Dgraph SHA-256 : 078c75df9fa1057447c8c8afc10ea57cb0a29dfb22f9e61d8c334882b4b4eb37
Commit SHA-1 : d62ed5f15
Commit timestamp : 2021-12-02 21:20:09 +0530
Branch : HEAD
Go version : go1.17.3
jemalloc enabled : true

It looks like you’re mixing DQL and GraphQL?

This is a GraphQL type:

type Username {
  username: String! @id
}

This looks like a DQL statement to update the graph schema.

username: string @index(term, fulltext) @upsert @count .

@id is a GraphQL directive. When you add a GraphQL schema to the database, it converts it into a graph schema. I believe it just gets converted to a string predicate with upsert set to true.

When you add a GraphQL type to the database schema, Dgraph represents this in the graph with a <dgraph.type> predicate(field) on the nodes, and when you save a new instance of a type it’ll save the name of the type to the <dgraph.type> predicate (field).

If you’re using GraphQL, you should save your GraphQL schema in a file, e.g. schema.graphql. To add your updated schema to the database, you can use this command:

curl -X POST localhost:8080/admin/schema --data-binary '@src/lib/graphql/schema.graphql'

The @ is important. Just replace with the path to your schema file.

If there are no errors you should see the following:

❯ curl -X POST localhost:8080/admin/schema --data-binary '@src/lib/graphql/schema.graphql'
{"data":{"code":"Success","message":"Done"}}%
1 Like