graphene_server 0.2.0+21
graphene_server: ^0.2.0+21 copied to clipboard
A GraphQL inspired server and BSON based Schema.
Graphene Server #
A GraphQL inspired server. Hecho en 🇵🇷 por Radamés J. Valentín Reyes
Major Overhaul #
- This program now sends and recieves data in binary. More speciffically **BSON**. **JSON is no longer the format used.**
- Can now handle Get requests easily
- isolateVariables was added to startServer to pass variable values to the isolates (because everything is in another thread)
New #
- Switched to BSON file for request and response
HTTP POST Request body #
In BSON format Important This version no longer uses JSON. It now uses BSON.
Query #
{
"variables": {
"variable1": 2,
"variable2": "Hello World"
},
"query": "functionName"
}
Mutation #
{
"variables": {
"variable1": 2,
"variable2": "Hello World"
},
"mutation": "functionName"
}
Library use examples #
Dart Server #
import 'dart:typed_data';
import 'package:graphene_server/graphene_server.dart';
import 'dart:io';
void main()async{
String message = "Hello World";
await startServer(
server: await HttpServer.bind(InternetAddress.loopbackIPv4, 8080),
getHandler: GetHandler(
handler: (arguments)async{
return Uint8List.fromList(arguments["path"].codeUnits);
},
),
query: GrapheneQuery(
resolver: {
"helloWorld": (arguments)async{
return {
"message": message,
};
},
},
),
mutations: GrapheneMutation(
resolver: {
"helloWorld": (arguments)async{
message = arguments["newMessage"];
return {
"message": message,
};
},
},
),
redirectHandler: (variables){
return Redirect(
mimeType: "text/plain",
url: variables["url"],
);
},
);
}
Dart request example #
Pass an empty BSON.
Request method is always of type POST.
Request body example below. Request body must always be of type JSON.
Request URL
Always send the requests to your ip:port/graphene
http://localhost:5432/graphene
Request Body
{
"variables": {
"newMessage": "New Message"
},
"mutation": "helloWorld"
}
Authentication #
A simple authentication system that stores user accounts in the designated authDatabase folder by making use of objective_db. Here simple auth functions are provided to streamline the creation of servers that require such functionality.
Import authentication functions #
import 'package:graphene_server/auth.dart';
- Create account
createAccount(
authDatabase: authDatabase,
username: "valentin.radames@gmail.com",
password: "12345",
);
- Login
String accessToken = login(
authDatabase: authDatabase,
username: "valentin.radames@gmail.com",
password: "12345",
);
- Logout
logout(
authDatabase: authDatabase,
accessToken: accessToken,
);
- Token is valid
bool validToken = tokenIsValid(
authDatabase: authDatabase,
accessToken: accessToken,
);
- Update password
updatePassword(
authDatabase: authDatabase,
username: "valentin.radames@gmail.com",
password: "12345",
newPassword: "012345",
);
- Add role
addRole(
authDatabase: authDatabase,
username: "valentin.radames@gmail.com",
role: "Admin",
);
- Remove role
removeRole(
authDatabase: authDatabase,
username: "valentin.radames@gmail.com",
role: "Admin",
);
- Has Role
bool itHasRole = hasRole(
authDatabase: authDatabase,
accessToken: accessToken,
role: "Admin",
);
- Delete Account
deleteAccount(
authDatabase: authDatabase,
username: "valentin.radames@gmail.com",
);
- Add custom field
addCustomField(
authDatabase: authDatabase,
username: "valentin.radames@gmail.com",
customFieldName: "typeOfAnimal",
customFieldValue: "Human",
);
- Get all stored accounts
List<Map<String,dynamic>> allAccounts = getAllAccounts(authDatabase: authDatabase);
print(allAccounts);
- Get single stored account
Map<String,dynamic> updatedAccount = getSingleAccount(
authDatabase: authDatabase,
username: "valentin.radames@gmail.com",
);
