This project allows you to expose a Google Sheet as a GraphQL API using a free Google Apps Script.
Note that the exposed parts are accessible without authentication.
This is an early proof of concept made for fun, while I was learning about GraphQL. It only supports reading at the moment, and it is not optimized for large datasets.
- Due to Apps Script limitations, there is always a redirect before the API returns a result, and that confuses some GraphQL clients.
- Due to Apps Script limitations, the Content-Type header in the response is "application/json; charset=utf-8", and that confuses some GraphQL clients as they expect "application/json".
- Apps Script has quotas. (https://developers.google.com/apps-script/guides/services/quotas)
- Apps Script response times are in general not great for exposed services.
-
Install clasp, and enable the Google Apps Script API.
-
Login with your google account.
$ clasp login
$ git clone https://github.com/kalmi/sheets-apps-script-serverless-graphql.git
After cloning this repository:
$ cd sheets-apps-script-serverless-graphql
$ yarn install
$ clasp create --type sheets --rootDir ./dist
Alternatively you could add it to a preexisting sheet if you specify --parentId instead of --type.
{
"timeZone": "Europe/Paris",
"dependencies": {
},
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets.currentonly"
],
"webapp": {
"access": "ANYONE_ANONYMOUS",
"executeAs": "USER_DEPLOYING"
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
$ yarn build
$ yarn push -f
$ yarn deploy
$ clasp open
- Open the "Publish" menu, and click "Deploy as web app.."
- Click "Update" in the dialog
- Click "Review permissions"
- Follow the instructions displayed to authorize your application to access the spreadsheet
- Close the script editor
- Open your spreadsheet in Google Sheets
- Rename your sheet to graphql:Customer (Only sheets with the
graphql:
prefix are exposed) - Add headers, such as id name, age
- Add some data
wget https://script.google.com/macros/s/`cat .deploymentId`/exec -q -O- --post-data '{ "query": "{ City { name }}"}'
This should result in:
{
"data": {
"City": [
{
"name": "Budapest"
},
{
"name": "Bucharest"
}
]
}
}
$ yarn build
$ yarn push
$ yarn deploy
If you want to use watch mode, run the following command:
$ yarn watch
In watch mode, the build file is automatically pushed and deployed when saving the contents in the src
folder.