Sadık Ekin Özbay Sadık Ekin Özbay - 1 month ago 10
Ruby Question

Ruby on Rails, taking data from Graphql and posting on it to Facebook-messenger

$message_text = message.text
module STAPI
HTTP = GraphQL::Client::HTTP.new("API_ADDRESS")
Schema = GraphQL::Client.load_schema(HTTP)
Client = GraphQL::Client.new(schema: Schema,execute: HTTP)
end

module Sticker
Query = STAPI::Client.parse <<-'GRAPHQL'
{
stickers(query: "#{$message_text}", first: 21, after: "") {
edges {
node {
fileUrl(fullWatermarked: true)
}
}
}
}
GRAPHQL
end

result = STAPI::Client.query(Sticker::Query)


First of all this is an messenger bot and with this messenger bot i am taking users input and i am searching the input in our database then i post something related with the input.

So, in the code i can't search beacuse, in my opinion
query: "#{$message_text}"
is broken.

I take users input with
message.text
. This input comes like this 'Hello' (with '). But i need to give
query: "#{$message_text}"
part like this "Hello"(with "). how can i give messenger.text to query part like this "Hello"

Answer

I believe you already know how to make this work based on what you wrote in your other question. For others facing the same problem, take a look at variables. Your example would become as follows:

$message_text = message.text
module STAPI
  HTTP = GraphQL::Client::HTTP.new("API_ADDRESS")
  Schema = GraphQL::Client.load_schema(HTTP)
  Client = GraphQL::Client.new(schema: Schema,execute: HTTP)
end

module Sticker
  Query = STAPI::Client.parse <<-'GRAPHQL'
    query Q($message: String) {
      stickers(query: $message, first: 21, after: "") {
        edges {
          node {
            fileUrl(fullWatermarked: true)
          }
        }
      }
    }
  GRAPHQL
end

result = STAPI::Client.query(Sticker::Query, variables: {message_text: $message_text})