Sending Twilio bulk SMS with GraphQL resolvers


#1

Hello Everyone! Please see my question below. Your help is greatly appreciated in advance.

Objective
I would like to send a single text to multiple recipients using the twilio api module. The module is a revolver function.

Question
How do I query multiple records from graphcool and send that array of data to the revolver function?

Hunch
Before the revolver function is called, run a graphql query, then pass that data to revolver function. I guess this is usually the case with most apps, but for graphcool in particular I am having a hard time understanding the structure, where to call things, etc.


#2

Your resolver function would look something like:

extend type Query {
  sendMessage(message: String! recipients: [Id!]!): SendMessagePayload
}

The id’s would either come from your client app directly, or from running another query first to determine the recipients. Of course, you could also change this to be a [String!]!, if you want to pass in phone numbers directly.


#3

Thanks for your response @agartha! I think in the current situation I’m using, I’d like to do the query from the backend. My confusion is where does the query for the ID’s come from before being passed into sendMessage?


#4

Hey @wontwon, welcome to the Forum :wave:

There are many different ways. To better answer your question, could you describe in more detail what you want to do?

Note that the Twilio example should rather be seen as a template for you to modify after adding it to your application (we’re still iterating on terminology here - the modules term doesn’t precisely describe what it is, see: https://github.com/graphcool/graphcool/issues/720).

So basically, it’s up to you which of the two approaches you take:

  • the client provides a list of phone numbers as an argument to the sendSms mutation
  • the client provides some information that can be used to somehow get a list of phone numbers inside the resolver code for sendSms. For example, a list of user ids, a list of email address, or a group tag. Maybe you get the phone numbers with another GraphQL query against your Graphcool API, or maybe from an external system.

Note that Twilio does not support bulk sending SMS (for all I know), and the Twilio API looks to be rate limited to 1 SMS per second per sending number, according to https://stackoverflow.com/a/21104432/1176596.


#5

In both options you have mentioned the “client” must pass the information in. For many people, I believe they may be wondering how they can build a recurring cron job to possibly send an email or message to multiple (or all) users on some sort of schedule. Usually, one would consider that entire process as a backend job. I presume you would have to write a backend (most likely in node) that acts as the aforementioned “client” to the graph cool api. Some people may get confused by the term “client” and think that it it must be triggered from a frontend UI but I think in this context it just means an application that is interacting with the graph cool api in general.

First, one would have to create a graph cool query such as

{ allUsers(id, name, email) }

and then calling the graphcool function, passing in those recipients in something similar to…

extend type Query {
  sendMessage(message: String!, emails: [string!]!): SendMessagePayload
}

It might be super useful to have a scenario like this documented in an example project. Many people new to the graphql paradigm might be wondering how to perform such a common function. :slight_smile: