How to convert GraphQL mutation to dql

I am trying to convert this graphql:

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

vars

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

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

  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

1 Like

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)

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

Yep, Nico_Braun is correct.

I am not getting this to work:

I have:

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:

{"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:

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

How do you actually call the mutation using the JSON Format?

Thanks,
J

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

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

@MichelDiz Correct, I am using lambda.

I get the same error with this:

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

This gives a different error:

const results = await dql.mutate(JSON.stringify(args));
{"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:

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

Note that I haven’t used Lambdas before.

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

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 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 but the query is.

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

See the content type.

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();
}

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

Yes.

1 Like

After hours and hours, finally got it like so:

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.

const results = await dql.mutate(args);
1 Like