Search code examples
graphqlaws-appsync

Appsync mutatation query giving MalformedHttpRequestException via POST


I am trying out the default example for realtime updates on AWS appsync.

Schema

type Channel {
name: String!
data: AWSJSON!
}

type Mutation {
    publish(name: String!, data: AWSJSON!): Channel
}

type Query {
    getChannel: Channel
}

type Subscription {
    subscribe(name: String!): Channel
        @aws_subscribe(mutations: ["publish"])
}

Running this query through AWS query page gives success

mutation PublishData {
    publish(data: "{\"msg\": \"hello world!\"}", name: "channel") {
        data
        name
    }
}

When trying to execute the same through HTTP Post, it gives error.

curl --location --request POST 'https://XXXX.ap-south-1.amazonaws.com:443/graphql' \
--header 'x-api-key: XXXXX' \
--header 'Content-Type: application/graphql' \
--data-raw '{
    "query": "mutation PublishData { publish(data: \"{\"msg\": \"hello world!\"}\", name: \"broadcast\") { data name } }",
    "variables": "{}"
}'

Executing this query gives success

curl --location --request POST 'https://XXX.ap-south-1.amazonaws.com:443/graphql' \
--header 'x-api-key: XXXX' \
--header 'Content-Type: application/graphql' \
--data-raw '{
    "query": "mutation PublishData { publish(data: \"{}\", name: \"broadcast\") { data name } }",
    "variables": "{}"
}'

I am unable to figure out where is the syntax error.


Solution

  • I got it working using variables. This is the syntax.

    {
        "query": "mutation($data:AWSJSON!) { publish(data: $data, name: \"broadcast\") { data name } }",
        "variables": {"data":"{\"abs\":1}"}
    }