Created
June 7, 2020 16:14
-
-
Save jagedn/8ae31cafe53835574b09a8d2c9f6b527 to your computer and use it in GitHub Desktop.
a command line CRUD for FaunaDB using GraphQL in groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Grab('com.github.grooviter:gql-core:0.3.5') | |
@Grab('io.github.http-builder-ng:http-builder-ng-core:1.0.4') | |
import groovy.transform.ToString | |
import static groovyx.net.http.HttpBuilder.configure | |
import static groovyx.net.http.ContentTypes.JSON | |
import gql.DSL | |
import graphql.schema.* | |
@ToString | |
class IpfsFile{ | |
String _id | |
String name | |
String hash58 | |
} | |
faunadbClient = configure{ | |
request.uri = 'https://graphql.fauna.com' | |
request.contentType = JSON[0] | |
request.headers['Authorization'] = "Basic "+"${new File('fauna.key').text}:".bytes.encodeBase64().toString() | |
} | |
switch( args[0] ){ | |
case 'insert': | |
def ipfsFile = saveFile(new IpfsFile(name:args[1], hash58:args[2])) | |
println ipfsFile | |
break | |
case 'find': //hash58 | |
def ipfsFile = findFile(args[1]) | |
println ipfsFile | |
break | |
case 'update': | |
def ipfsFile = updateFile(new IpfsFile(_id:args[1],name:args[2], hash58:args[3])) | |
println ipfsFile | |
break | |
case 'delete': | |
deleteFile(args[1]) | |
break | |
} | |
IpfsFile findFile(String hash58){ | |
String queryString = DSL.buildQuery { | |
query('fileByHash58', [hash58: hash58]) { | |
returns(IpfsFile) { | |
_id | |
name | |
} | |
} | |
} | |
def result = faunadbClient.post{ | |
request.uri.path = '/graphql' | |
request.body = [query:queryString] | |
} | |
if( !result.data.fileByHash58 ) | |
return null | |
new IpfsFile(_id:result.data.fileByHash58._id, name:result.data.fileByHash58.name, hash58:hash58) | |
} | |
IpfsFile saveFile(IpfsFile ipfsFile){ | |
String insertString = DSL.buildMutation{ | |
mutation('createIpfsFile', [data: [name:ipfsFile.name, hash58:ipfsFile.hash58]]){ | |
returns(IpfsFile){ | |
_id | |
name | |
hash58 | |
} | |
} | |
} | |
def result = faunadbClient.post{ | |
request.uri.path = '/graphql' | |
request.body = [query:insertString] | |
} | |
if( !result.data.createIpfsFile ) | |
return null | |
new IpfsFile(result.data.createIpfsFile) | |
} | |
IpfsFile updateFile(IpfsFile ipfsFile){ | |
String updateString = DSL.buildMutation{ | |
mutation('updateIpfsFile', [id: ipfsFile._id, data: [name:ipfsFile.name,hash58:ipfsFile.hash58]]){ | |
returns(IpfsFile){ | |
_id | |
} | |
} | |
} | |
def result = faunadbClient.post{ | |
request.uri.path = '/graphql' | |
request.body = [query:updateString] | |
} | |
if( !result.data.updateIpfsFile ) | |
return null | |
ipfsFile | |
} | |
boolean deleteFile(String id){ | |
String deleteString = DSL.buildMutation{ | |
mutation('deleteIpfsFile', [id: id]){ | |
returns(IpfsFile){ | |
hash58 | |
} | |
} | |
} | |
def result = faunadbClient.post{ | |
request.uri.path = '/graphql' | |
request.body = [query:deleteString] | |
} | |
result.data.deleteIpfsFile != null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fauna.key
From a command line execute:
$groovy faunadb.groovy insert test 123
$groovy faunadb.groovy find 123
$groovy faunadb.groovy update 2676847682312312312 test2 12343 // first number is the previous id
$groovy faundadb.groovy delete 2676847682312312312
check against your faundb collection every execution
schema.gql: