Created
December 19, 2021 17:28
-
-
Save Akifcan/944c0c5aa2b1af55d8cb0f946975f439 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class GraphqlResponse { | |
final bool success; | |
final data; | |
GraphqlResponse({required this.success, required this.data}); | |
} | |
class GraphqlService { | |
GraphqlService._privateConstructor(); | |
static final GraphqlService _instance = GraphqlService._privateConstructor(); | |
static GraphqlService get instance => _instance; | |
Uri url = Uri.parse('http://localhost:3000/graphql'); | |
sendRequest(String query, Map variables) async { | |
SharedPreferences preferences = await SharedPreferences.getInstance(); | |
var response = await http.post( | |
url, | |
headers: { | |
"Content-Type": "application/json", | |
"Accept": "application/json", | |
"token": preferences.getString('token').toString() | |
}, | |
body: jsonEncode({"query": query, "variables": variables}), | |
); | |
var data = jsonDecode(response.body); | |
print(data); | |
if(data['data'] != null){ | |
return GraphqlResponse(data: data['data'], success: true); | |
}else{ | |
return GraphqlResponse(data: ['errors'][0], success: false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment