# How to convert GraphQL mutation to dql

**URL:** <https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234>\
**Category:** Dgraph Cloud\
**Tags:** graphql, kind:question, dql\
**Created:** [March 16, 2021, 2:31pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234 "2021-03-16T14:31:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdgamble555](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jdgamble555/32/7503_2.png) [@jdgamble555](https://discuss.dgraph.io/u/jdgamble555)\
**Post date:** [March 16, 2021, 2:31pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/1 "2021-03-16T14:31:23Z")

</div>

I am trying to convert this **graphql** :

```auto
mutation addPost($post: AddPostInput!) {
  addPost(input: [$post]) {
    post {
      id
      name
      description
      isPublished
    }
  }
}

```

**vars**

```auto
{
  name: 'some name',
  description: 'some description',
  isPublished: true
}

```

to **dql** in a lambda. I have this, but it does not work:

```auto
  const args = {
    "dgraph.type": "Post",
    "name": "some name",
    "description": "some description",
    "isPublished": true
  };

  const results = await dql.mutate("set": [args]);

```

Am I correctly inputting this?

Thanks,  
J

---

<div class="post-metadata">

**Author:** ![Nico\_Braun](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/nico_braun/32/7207_2.png) [@Nico\_Braun](https://discuss.dgraph.io/u/Nico_Braun)\
**Post date:** [March 16, 2021, 3:10pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/2 "2021-03-16T15:10:02Z")

</div>

I am fairly new so I don’t know much, but I have noticed that when using graphql schema , it will add prefixes to things.

Potentially your predicates are named like Post.name, Post.description.

If this is the case you should also name them as such in your object (probably)

```auto
  const args = {
    "dgraph.type": "Post",
    "Post.name": "some name",
    "Post.description": "some description",
    "Post.isPublished": true
  };

```

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [March 16, 2021, 3:35pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/3 "2021-03-16T15:35:12Z")

</div>

Yep, Nico\_Braun is correct.

---

<div class="post-metadata">

**Author:** ![jdgamble555](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jdgamble555/32/7503_2.png) [@jdgamble555](https://discuss.dgraph.io/u/jdgamble555)\
**Post date:** [March 17, 2021, 4:46am UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/4 "2021-03-17T04:46:06Z")

</div>

I am not getting this to work:

I have:

```auto
const args = {
  "dgraph.type": "Post",
  "Post.name": "some name",
  "Post.description": "some description",
  "Post.isPublished": true
};

const results = await dql.mutation(args);

```

And I get this error:

```auto
{"errors":[{"message":"while lexing [object Object] at line 1 column 0: 
Unexpected character while identifying mutation block: U+005B '['","extensions":
{"code":"ErrorInvalidRequest"}}]}

```

I tried something like this to no avail:

```auto
const results = await dql.mutate({"set": [args]});

```

How do you actually call the mutation using the [**JSON Format**](https://dgraph.io/docs//mutations/json-mutation-format/#json-syntax-using-raw-http-or-ratel-ui)?

Thanks,  
J

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [March 17, 2021, 1:42pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/5 "2021-03-17T13:42:23Z")

</div>

I think you are using Lambda, correct? I think it uses dgraph-js-http. You might need to stringify the object e.g [Add Running an Upsert by MichelDiz · Pull Request #13 · dgraph-io/dgraph-js-http · GitHub](https://github.com/dgraph-io/dgraph-js-http/pull/13/files)

BTW, no client uses the syntax “set” as they use methods. This syntax is necessary when doing raw queries.

---

<div class="post-metadata">

**Author:** ![jdgamble555](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jdgamble555/32/7503_2.png) [@jdgamble555](https://discuss.dgraph.io/u/jdgamble555)\
**Post date:** [March 17, 2021, 2:59pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/6 "2021-03-17T14:59:51Z")

</div>

@MichelDiz Correct, I am using lambda.

I get the same error with this:

```auto
const results = await dql.mutation({ mutation: JSON.stringify(args) });

```

This gives a different error:

```auto
const results = await dql.mutate(JSON.stringify(args));

```

```auto
{"errors":[{"message":"while lexing {\"dgraph.type\":\"Post\",\"Post.name\":
\"you have\",\"Post.description\":\"asdfasdf\",\"Post.isPublished\":true} at
 line 1 column 1: Unrecognized character inside mutation: U+0022 '\"'",
"extensions":{"code":"ErrorInvalidRequest"}}]}

```

I even tried the combinations **WITH SET** like so:

```auto
const args = {
  set: [
    {
      "dgraph.type": "Post",
      "Post.name": "some name",
      "Post.description": "some description",
      "Post.isPublished": true
    }
  ]
};

```

And I get one of the two errors.

any thoughts?

Thanks,  
J

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [March 17, 2021, 4:29pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/7 "2021-03-17T16:29:47Z")

</div>

_Note that I haven’t used Lambdas before._

Based on the lambda repo, I think the right would be

```auto
const results = await dql.mutate(JSON.stringify(args));

```

As you can see here [dql is now dql.query and dql.mutate · dgraph-io/dgraph-lambda@e20baf6 · GitHub](https://github.com/dgraph-io/dgraph-lambda/commit/e20baf63d13090847f92a15822427cb6e190549e#diff-c02e13b10981ce32f85b03f374d6b60e23e7275a02994de35c740f217bd7f271L24) the “query” and “mutate” are the only related methods.

I see that lambda doesn’t use dgraph-js-http. It uses RAW API query. Also, the mutation and the query (both DLQ) are strings. Not objects/JSON.

For some reason(PS. Now I know. It is RDF, not JSON), the mutation isn’t stringified [dgraph-lambda/dgraph.ts at 8bb34dd627d0522fe8c0516cf784a5ecadc89523 · dgraph-io/dgraph-lambda · GitHub](https://github.com/dgraph-io/dgraph-lambda/blob/8bb34dd627d0522fe8c0516cf784a5ecadc89523/src/dgraph.ts#L42) but the query is.

Ahhhhh! now I see what is the issue. Looks like lambdas doesn’t supports JSON mutations.

See the content type.

```auto
async function dqlMutate(mutate: string): Promise<GraphQLResponse> {
  const response = await fetch(`${process.env.DGRAPH_URL}/mutate?commitNow=true`, {
    method: "POST",
    headers: {
      "Content-Type": "pplication/rdf",
      "X-Auth-Token": process.env.DGRAPH_TOKEN || ""
    },
    body: mutate
  })
  if (response.status !== 200) {
    throw new Error("Failed to execute DQL Mutate")
  }
  return response.json();
}

```

---

<div class="post-metadata">

**Author:** ![jdgamble555](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jdgamble555/32/7503_2.png) [@jdgamble555](https://discuss.dgraph.io/u/jdgamble555)\
**Post date:** [March 17, 2021, 4:55pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/8 "2021-03-17T16:55:03Z")

</div>

Ok, so basically I need to use rdf **set** with triples instead of JSON?

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [March 17, 2021, 7:40pm UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/9 "2021-03-17T19:40:28Z")

</div>

Yes.

---

<div class="post-metadata">

**Author:** ![jdgamble555](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jdgamble555/32/7503_2.png) [@jdgamble555](https://discuss.dgraph.io/u/jdgamble555)\
**Post date:** [March 18, 2021, 4:08am UTC](https://discuss.dgraph.io/t/how-to-convert-graphql-mutation-to-dql/13234/10 "2021-03-18T04:08:19Z")

</div>

After hours and hours, finally got it like so:

```auto
const args = `
{
  set {
    _:blank-0 <Post.name> "${args.name}" .
    _:blank-0 <Post.description> "${args.description}" .
    _:blank-0 <Post.published> "${args.published}" .
    _:blank-0 <dgraph.type> "Post" .
  }
}
`;

```

Note: **dql** requires the newlines, unlike **graphql**.

```auto
const results = await dql.mutate(args);

```
