-1

I'm a newbie to Hasura and need a little help trying to pull data via an http request.

Below is my request but I don't think my syntax is correct. I need to pass ajson object where the key is query and the value is the code block. How do I do this?

Shawn

query MyQuery {
  t_user(where: {id: {_eq: "2"}}) {
    name
    id
  }
}
virtualbis
  • 397
  • 1
  • 14

2 Answers2

0

You should post your question with more details. Like the actual code making the request to Hasura graphql endpoint

Here's an example request using the Browser fetch api

const query = `query MyQuery {
  t_user {
    name
    id  
  }
}`;

fetch('https://lasting-grub-95.hasura.app/v1/graphql', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'x-hasura-admin-secret': 'my-secret'
    },
    body: JSON.stringify({
        query
    })
})
    .then(res => res.json())
    .then(res => {
        console.log('Hasura response: ', res);
    })
    .catch(err => {
        console.error(err);
    })

For the admin-secret go to projects page of Hasura Cloud. There you would see your project listed with necessary details including Admin Secret

0

I found answer with a little help. I needed to put my query into a JSON object. So my query in the console was this: query MyQuery { t_user(where: {id: {_eq: "2"}}) { name id } }

And I needed to it to look like this: {"query":"query MyQuery {\n  t_user(where: {id: {_eq: \"2\"}}) {\n    name\n    id\n  }\n}\n","variables":null,"operationName":"MyQuery"}

virtualbis
  • 397
  • 1
  • 14
  • 1
    For what it's worth there is much better tooling available for making requests to GraphQL servers besides manually constructing the requests like this. This might be fine if you're just playing around this may be fine but I would strongly recommend that you take a look at something like Apollo Client or URQL depending on what kind of application you're building – Jesse Carter Jan 07 '21 at 19:54
  • Appreciated @JesseCarter. In this case I just needed a Post HTTP request for a scenario I have in [Integromat](https://www.integromat.com/). My next step is Apollo Client in a React app. Hopefully the learning curve isn't too steep. – virtualbis Jan 07 '21 at 23:10