Skip to content

Commit 0f59c91

Browse files
committed
Update docs for HTTP API
In /query and /mutate endpoint, we need to specify Content-Type -. * /query -> application/graphql or application/json * /mutate -> application/rdf or application/json
1 parent df7828b commit 0f59c91

File tree

10 files changed

+60
-47
lines changed

10 files changed

+60
-47
lines changed

dgraph/cmd/alpha/notes.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
curl localhost:8080/query -XPOST -d '{
1+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d '{
22
me(_xid_: m.06pj8) {
33
type.object.name.en
44
film.director.film {

dgraph/cmd/bulk/systest/run.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ for suite in $script_dir/suite*; do
3737
sleep 2
3838

3939
popd >/dev/null # out of tmp
40-
result=$(curl --silent localhost:8080/query -XPOST -d @$suite/query.json)
40+
result=$(curl --silent -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d @$suite/query.json)
4141
if ! $(jq --argfile a <(echo $result) --argfile b $suite/result.json -n 'def post_recurse(f): def r: (f | select(. != null) | r), .; r; def post_recurse: post_recurse(.[]?); ($a | (post_recurse | arrays) |= sort) as $a | ($b | (post_recurse | arrays) |= sort) as $b | $a == $b')
4242
then
4343
echo "Actual result doesn't match expected result:"

dgraph/cmd/bulk/systest/test-bulk-schema.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ predicate_with_default_type:default .
9999
predicate_with_index_no_uid_count:string @index(exact) .
100100
' &>/dev/null
101101

102-
curl localhost:$HTTP_PORT/mutate?commitNow=true -X POST -d $'
102+
curl -H "Content-Type: application/rdf" localhost:$HTTP_PORT/mutate?commitNow=true -X POST -d $'
103103
{
104104
set {
105105
_:company1 <predicate_with_default_type> "CompanyABC" .
@@ -112,7 +112,7 @@ function QuerySchema
112112
{
113113
INFO "running schema query"
114114
local out_file="schema.out"
115-
curl -sS localhost:$HTTP_PORT/query -XPOST -d'schema(pred:[genre,language,name,revenue,predicate_with_default_type,predicate_with_index_no_uid_count,predicate_with_no_uid_count]) {}' | python -c "import json,sys; d=json.load(sys.stdin); json.dump(d['data'],sys.stdout,sort_keys=True,indent=2)" > $out_file
115+
curl -sS -H "Content-Type: application/graphql" localhost:$HTTP_PORT/query -XPOST -d'schema(pred:[genre,language,name,revenue,predicate_with_default_type,predicate_with_index_no_uid_count,predicate_with_no_uid_count]) {}' | python -c "import json,sys; d=json.load(sys.stdin); json.dump(d['data'],sys.stdout,sort_keys=True,indent=2)" > $out_file
116116
echo >> $out_file
117117
}
118118

query/benchmark/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rm -Rf dumpsg
1212
NUMS="10 56 300"
1313

1414
for NUM in $NUMS; do
15-
curl localhost:8912/query -XPOST -d "{
15+
curl -H "Content-Type: application/graphql" localhost:8912/query -XPOST -d "{
1616
me(_xid_:m.08624h) {
1717
type.object.name.en
1818
film.actor.film(first: $NUM) {
@@ -42,7 +42,7 @@ rm -Rf dumpsg
4242
NUMS="10 31 100"
4343

4444
for NUM in $NUMS; do
45-
curl localhost:8912/query -XPOST -d "{
45+
curl -H "Content-Type: application/graphql" localhost:8912/query -XPOST -d "{
4646
me(_xid_:m.05dxl_) {
4747
type.object.name.en
4848
film.director.film(first: $NUM) {

query/benchmark/run.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dgraph -dumpsg dumpsg -port 8912 &
1919
sleep 2
2020

2121
for ACTOR in $ACTORS; do
22-
curl localhost:8912/query -XPOST -d "
22+
curl -H "Content-Type: application/graphql" localhost:8912/query -XPOST -d "
2323
{
2424
me(_xid_:${ACTOR}) {
2525
type.object.name.en
@@ -42,7 +42,7 @@ done
4242
rm -f dumpsg/*
4343

4444
for DIRECTOR in $DIRECTORS; do
45-
curl localhost:8912/query -XPOST -d "
45+
curl -H "Content-Type: application/graphql" localhost:8912/query -XPOST -d "
4646
{
4747
me(_xid_:${DIRECTOR}) {
4848
type.object.name.en

wiki/content/clients/index.md

+14-7
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,10 @@ Similar to the Go client example, we use a bank account transfer example.
470470

471471
A client built on top of the HTTP API will need to track three pieces of state
472472
for each transaction.
473-
473+
474474
1. A start timestamp (`start_ts`). This uniquely identifies a transaction,
475475
and doesn't change over the transaction lifecycle.
476-
476+
477477
2. The set of keys modified by the transaction (`keys`). This aids in
478478
transaction conflict detection.
479479

@@ -535,12 +535,14 @@ new transaction is initiated.**
535535

536536
### Run a query
537537

538-
To query the database, the `/query` endpoint is used.
538+
To query the database, the `/query` endpoint is used. Remember to set the content
539+
type header to `application/graphql` in order to ensure that the body of the request
540+
is correctly parsed.
539541

540542
To get the balances for both accounts:
541543

542544
```sh
543-
$ curl -X POST localhost:8080/query -d $'
545+
$ curl -H "Content-Type: application/graphql" -X POST localhost:8080/query -d $'
544546
{
545547
balances(func: anyofterms(name, "Alice Bob")) {
546548
uid
@@ -605,10 +607,12 @@ Note that we have to refer to the Alice and Bob nodes by UID in the RDF format.
605607

606608
We now send the mutations via the `/mutate` endpoint. We need to provide our
607609
transaction start timestamp as a path parameter, so that Dgraph knows which
608-
transaction the mutation should be part of.
610+
transaction the mutation should be part of. We also need to set content type
611+
header to `application/rdf` in order to specify that mutation is written in
612+
rdf format.
609613

610614
```sh
611-
$ curl -X POST localhost:8080/mutate?startTs=4 -d $'
615+
$ curl -H "Content-Type: application/rdf" -X POST localhost:8080/mutate?startTs=4 -d $'
612616
{
613617
set {
614618
<0x1> <balance> "110" .
@@ -732,6 +736,7 @@ Example of a compressed request via curl:
732736
```sh
733737
$ curl -X POST \
734738
-H 'Content-Encoding: gzip' \
739+
-H "Content-Type: application/rdf" \
735740
localhost:8080/mutate?commitNow=true --data-binary @mutation.gz
736741
```
737742

@@ -740,6 +745,7 @@ Example of a compressed request via curl:
740745
```sh
741746
$ curl -X POST \
742747
-H 'Accept-Encoding: gzip' \
748+
-H "Content-Type: application/graphql"
743749
localhost:8080/query -d $'schema {}' | gzip --decompress
744750
```
745751

@@ -759,13 +765,14 @@ $ zcat query.gz # query.gz is gzipped compressed
759765
$ curl -X POST \
760766
-H 'Content-Encoding: gzip' \
761767
-H 'Accept-Encoding: gzip' \
768+
-H "Content-Type: application/graphql"
762769
localhost:8080/query --data-binary @query.gz | gzip --decompress
763770
```
764771

765772
{{% notice "note" %}}
766773
Curl has a `--compressed` option that automatically requests for a compressed response (`Accept-Encoding` header) and decompresses the compressed response.
767774

768775
```sh
769-
$ curl -X POST --compressed localhost:8080/query -d $'schema {}'
776+
$ curl -X POST --compressed -H "Content-Type: application/graphql" localhost:8080/query -d $'schema {}'
770777
```
771778
{{% /notice %}}

wiki/content/get-started/index.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ Ratel's default port is 8081, so override it using -p 8000.
201201
## Step 3: Run Queries
202202
{{% notice "tip" %}}Once Dgraph is running, you can access Ratel at [`http://localhost:8000`](http://localhost:8000). It allows browser-based queries, mutations and visualizations.
203203

204-
The mutations and queries below can either be run from the command line using `curl localhost:8080/query -XPOST -d $'...'` or by pasting everything between the two `'` into the running user interface on localhost.{{% /notice %}}
204+
The mutations and queries below can either be run from the command line using `curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'...'` or by pasting everything between the two `'` into the running user interface on localhost.{{% /notice %}}
205205

206206
### Dataset
207207
The dataset is a movie graph, where and the graph nodes are entities of the type directors, actors, genres, or movies.
@@ -211,7 +211,7 @@ Changing the data stored in Dgraph is a mutation. The following mutation stores
211211

212212

213213
```sh
214-
curl localhost:8080/mutate?commitNow=true -XPOST -d $'
214+
curl -H "Content-Type: application/rdf" localhost:8080/mutate?commitNow=true -XPOST -d $'
215215
{
216216
set {
217217
_:luke <name> "Luke Skywalker" .
@@ -277,7 +277,7 @@ curl localhost:8080/alter -XPOST -d $'
277277
Run this query to get all the movies. The query works below all the movies have a starring edge
278278

279279
```sh
280-
curl localhost:8080/query -XPOST -d $'
280+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'
281281
{
282282
me(func: has(starring)) {
283283
name
@@ -291,7 +291,7 @@ Run this query to get "Star Wars" movies released after "1980". Try it in the u
291291

292292

293293
```sh
294-
curl localhost:8080/query -XPOST -d $'
294+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'
295295
{
296296
me(func:allofterms(name, "Star Wars")) @filter(ge(release_date, "1980")) {
297297
name

wiki/content/howto/index.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ $ curl localhost:8080/alter -d '
118118
```
119119

120120
```sh
121-
$ curl localhost:8080/mutate?commitNow=true -d '{
121+
$ curl -H "Content-Type: application/rdf" localhost:8080/mutate?commitNow=true -d '{
122122
set {
123123
_:dgraph <name> "Dgraph" .
124124
_:dgraph <url> "https://github.com/dgraph-io/dgraph" .
@@ -128,7 +128,7 @@ $ curl localhost:8080/mutate?commitNow=true -d '{
128128
```
129129

130130
```sh
131-
$ curl localhost:8080/mutate?commitNow=true -d '{
131+
$ curl -H "Content-Type: application/rdf" localhost:8080/mutate?commitNow=true -d '{
132132
set {
133133
_:badger <name> "Badger" .
134134
_:badger <url> "https://github.com/dgraph-io/badger" .

wiki/content/mutations/index.md

+16-10
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ Mutations can be done over HTTP by making a `POST` request to an Alpha's `/mutat
288288
To run a `set` mutation:
289289

290290
```sh
291-
curl -X POST localhost:8080/mutate?commitNow=true -d $'
291+
curl -H "Content-Type: application/rdf" -X POST localhost:8080/mutate?commitNow=true -d $'
292292
{
293293
set {
294294
_:alice <name> "Alice" .
@@ -299,7 +299,7 @@ curl -X POST localhost:8080/mutate?commitNow=true -d $'
299299
To run a `delete` mutation:
300300

301301
```sh
302-
curl -X POST localhost:8080/mutate?commitNow=true -d $'
302+
curl -H "Content-Type: application/rdf" -X POST localhost:8080/mutate?commitNow=true -d $'
303303
{
304304
delete {
305305
# Example: Alice's UID is 0x56f33
@@ -310,8 +310,8 @@ curl -X POST localhost:8080/mutate?commitNow=true -d $'
310310
311311
To run an RDF mutation stored in a file, use curl's `--data-binary` option so that, unlike the `-d` option, the data is not URL encoded.
312312

313-
```
314-
curl -X POST localhost:8080/mutate?commitNow=true --data-binary @mutation.txt
313+
```sh
314+
curl -H "Content-Type: application/rdf" -X POST localhost:8080/mutate?commitNow=true --data-binary @mutation.txt
315315
```
316316

317317
## JSON Mutation Format
@@ -646,15 +646,21 @@ Deletion operations are the same as [Deleting literal values]({{< relref "#delet
646646

647647
### Using JSON operations via cURL
648648

649+
First you have to configure the HTTP header to specify content-type.
650+
651+
```sh
652+
-H 'Content-Type: application/json'
653+
```
654+
649655
{{% notice "note" %}}
650656
In order to use `jq` for JSON formatting you need the `jq` package. See the
651657
[`jq` downloads](https://stedolan.github.io/jq/download/) page for installation
652658
details. You can also use Python's built in `json.tool` module with `python -m
653659
json.tool` to do JSON formatting.
654660
{{% /notice %}}
655661

656-
```BASH
657-
curl -X POST localhost:8080/mutate?mutationType=json&commitNow=true -d $'
662+
```sh
663+
curl -H "Content-Type: application/json" -X POST localhost:8080/mutate?commitNow=true -d $'
658664
{
659665
"set": [
660666
{
@@ -670,8 +676,8 @@ curl -X POST localhost:8080/mutate?mutationType=json&commitNow=true -d $'
670676

671677
To delete:
672678

673-
```BASH
674-
curl -X POST localhost:8080/mutate?mutationType=json&commitNow=true -d $'
679+
```sh
680+
curl -H "Content-Type: application/json" -X POST localhost:8080/mutate?commitNow=true -d $'
675681
{
676682
"delete": [
677683
{
@@ -683,6 +689,6 @@ curl -X POST localhost:8080/mutate?mutationType=json&commitNow=true -d $'
683689

684690
Mutation with a JSON file:
685691

686-
```
687-
curl -X POST localhost:8080/mutate?mutationType=json&commitNow=true -d @data.json
692+
```sh
693+
curl -H "Content-Type: application/json" -X POST localhost:8080/mutate?commitNow=true -d @data.json
688694
```

wiki/content/query-language/index.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -1930,8 +1930,8 @@ For the purposes of debugging, you can attach a query parameter `debug=true` to
19301930
- `start_ts`: The logical start timestamp of the transaction.
19311931

19321932
Query with debug as a query parameter
1933-
```
1934-
curl "http://localhost:8080/query?debug=true" -XPOST -d $'{
1933+
```sh
1934+
curl -H "Content-Type: application/graphql" http://localhost:8080/query?debug=true -XPOST -d $'{
19351935
tbl(func: allofterms(name@en, "The Big Lebowski")) {
19361936
name@en
19371937
}
@@ -2512,7 +2512,7 @@ curl localhost:8080/alter -XPOST -d $'
25122512
```
25132513

25142514
```sh
2515-
curl localhost:8080/mutate?commitNow=true -XPOST -d $'
2515+
curl -H "Content-Type: application/rdf" localhost:8080/mutate?commitNow=true -XPOST -d $'
25162516
{
25172517
set {
25182518
@@ -2885,7 +2885,7 @@ curl localhost:8080/alter -XPOST -d $'
28852885
```
28862886

28872887
```sh
2888-
curl localhost:8080/mutate?commitNow=true -XPOST -d $'
2888+
curl -H "Content-Type: application/rdf" localhost:8080/mutate?commitNow=true -XPOST -d $'
28892889
{
28902890
set {
28912891
_:a <friend> _:b (weight=0.1) .
@@ -2901,8 +2901,8 @@ curl localhost:8080/mutate?commitNow=true -XPOST -d $'
29012901
```
29022902

29032903
The shortest path between Alice and Mallory (assuming UIDs 0x2 and 0x5 respectively) can be found with query:
2904-
```
2905-
curl localhost:8080/query -XPOST -d $'{
2904+
```sh
2905+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'{
29062906
path as shortest(from: 0x2, to: 0x5) {
29072907
friend
29082908
}
@@ -2939,8 +2939,8 @@ Which returns the following results. (Note, without considering the `weight` fac
29392939
```
29402940

29412941
The shortest two paths are returned with:
2942-
```
2943-
curl localhost:8080/query -XPOST -d $'{
2942+
```sh
2943+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'{
29442944
path as shortest(from: 0x2, to: 0x5, numpaths: 2) {
29452945
friend
29462946
}
@@ -2953,8 +2953,8 @@ curl localhost:8080/query -XPOST -d $'{
29532953
Edges weights are included by using facets on the edges as follows.
29542954

29552955
{{% notice "note" %}}One facet per predicate in the shortest query block is allowed.{{% /notice %}}
2956-
```
2957-
curl localhost:8080/query -XPOST -d $'{
2956+
```sh
2957+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'{
29582958
path as shortest(from: 0x2, to: 0x5) {
29592959
friend @facets(weight)
29602960
}
@@ -3012,8 +3012,8 @@ curl localhost:8080/query -XPOST -d $'{
30123012
```
30133013

30143014
Constraints can be applied to the intermediate nodes as follows.
3015-
```
3016-
curl localhost:8080/query -XPOST -d $'{
3015+
```sh
3016+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'{
30173017
path as shortest(from: 0x2, to: 0x5) {
30183018
friend @filter(not eq(name, "Bob")) @facets(weight)
30193019
relative @facets(liking)
@@ -3027,8 +3027,8 @@ curl localhost:8080/query -XPOST -d $'{
30273027

30283028
The k-shortest path algorithm (used when numPaths > 1)also accepts the arguments ```minweight``` and ```maxweight```, which take a float as their value. When they are passed, only paths within the weight range ```[minweight, maxweight]``` will be considered as valid paths. This can be used, for example, to query the shortest paths that traverse between 2 and 4 nodes.
30293029

3030-
```
3031-
curl localhost:8080/query -XPOST -d $'{
3030+
```sh
3031+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'{
30323032
path as shortest(from: 0x2, to: 0x5, numpaths: 2, minweight: 2, maxweight: 4) {
30333033
friend
30343034
}
@@ -3067,8 +3067,8 @@ Some points to keep in mind while using recurse queries are:
30673067

30683068
`fragment` keyword allows you to define new fragments that can be referenced in a query, as per [GraphQL specification](https://facebook.github.io/graphql/#sec-Language.Fragments). The point is that if there are multiple parts which query the same set of fields, you can define a fragment and refer to it multiple times instead. Fragments can be nested inside fragments, but no cycles are allowed. Here is one contrived example.
30693069

3070-
```
3071-
curl localhost:8080/query -XPOST -d $'
3070+
```sh
3071+
curl -H "Content-Type: application/graphql" localhost:8080/query -XPOST -d $'
30723072
query {
30733073
debug(func: uid(1)) {
30743074
name@en

0 commit comments

Comments
 (0)