grooviter / gql

Groovy GraphQL library
http://grooviter.github.io/gql/
Apache License 2.0
49 stars 6 forks source link

How to specify an address where to do query? #21

Closed lynx-r closed 6 years ago

lynx-r commented 6 years ago

I haven't found any clues in docs about how I can specify address where queries should be send. I.e. I want to send a query to another server. How can I specify an address and a port?

mariogarcia commented 6 years ago

Hi @lynx-r , thanks for the feedback.

If you're talking about the Query Builders they were created for two use cases:

import gql.DSL
import graphql.GraphQLSchema
import graphql.ExecutionResult
import graphql.schema.DataFetchingEnvironment

GraphQLSchema schema = ...
ExecutionResult result = DSL.execute(schema) {
  query('byYear', [year: '1962']) { 
    returns(Film) { 
      title
      year
    }

    alias 'first' 
  }
}
import gql.DSL
import com.mashape.unirest.http.Unirest
import groovy.json.JsonOutput

String queryString = DSL.buildQuery {
  query('byYear', [year: '1962']) {
    returns(Film) {
      title
      year
    }
    alias 'first'
  }

  query('byYear', [year: '2015']) {
    returns {
      title
      year
      bond
    }
    alias 'last'
  }
}

// Then you can use an HTTP client like e.g Unirest (http://unirest.io/java.html)
Unirest
  .post("http://anyotherservice.com/graphql")
  .body(JsonOutput.toJson([query: queryString]))
  .asJson()

I hope this helps

lynx-r commented 6 years ago

Excellen! Exactly what I want to see. I want to use this approach in the code https://github.com/piomin/sample-graphql-microservices .