graphql-js icon indicating copy to clipboard operation
graphql-js copied to clipboard

How to pass query with variables to graphql function directly?

Open mjasnikovs opened this issue 9 years ago • 4 comments

There is example documentiton, how to pass variables from client to server

var query = `mutation CreateMessage($input: MessageInput) {
  createMessage(input: $input) {
    id
  }
}`;
xhr.send(JSON.stringify({
  query: query,
  variables: {
    input: {
      author: author,
      content: content,
    }
  }
}));

But how to pass varibles directly to

const {graphql} = require('graphql')

function?

Im curently ussing graphql as sort of ORM for my rest api. Have to do it, to provide smothe transition. What is the best whay to pass querys to graphql with varibles? I have spend countles hours, but alweys get errors. Like "Expected Name, found String "query" and etc...

mjasnikovs avatar Sep 20 '17 13:09 mjasnikovs

See here: https://github.com/graphql/graphql-js/blob/master/src/tests/starWarsQuery-test.js#L232-L240

      const query = `
        query FetchSomeIDQuery($someId: String!) {
          human(id: $someId) {
            name
          }
        }
      `;
      const params = { someId: '1000' };
      const result = await graphql(StarWarsSchema, query, null, null, params);

IvanGoncharov avatar Oct 02 '17 15:10 IvanGoncharov

Thank you! I Was blindly looking at source.

mjasnikovs avatar Oct 02 '17 15:10 mjasnikovs

My PostgreSQL column datatype is 'timestamptz'

This is my query where I am passing variables and datatype I am sending is Date. But I know I am wrong, but what should I use as a datatype there, if I use the String then that is not working nor Date is working.

const CONSUMED_ENERGY_QUERY = gql`
  query ($startDate: Date, $endDate: Date){
    schneider (
      where: { 
        _and: [
          {device_time: {_gte: $startDate }},
          {device_time: {_lte: $endDate }}
        ] 
      }
    ) {
      active_energy_delivered
    }
  }`;

Help

vishalshettydev avatar Oct 04 '18 13:10 vishalshettydev

@webmastervishal From your query I'm assuming you are using some kind of graphql to PostgreSQL tool or library, so please contact its authors.

IvanGoncharov avatar Oct 05 '18 08:10 IvanGoncharov