Graphql-js: How to pass query with variables to graphql function directly?

Created on 20 Sep 2017  路  4Comments  路  Source: graphql/graphql-js

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...

Most helpful comment

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

All 4 comments

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

Thank you! I Was blindly looking at source.

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

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

Was this page helpful?
0 / 5 - 0 ratings