KeystoneJS as back-end with Flutter as front-end - flutter

Is it possible/feasible to use KeystoneJS along with a front-end developed in Flutter for web? Would it make sense to do so? Any hint would be appreciated.

Keystonejs is any other backend which provides headless cms capability. You can use this like any other backend using GraphQL client if available in Flutter.
I see Flutter does support http request you can use this feature to call GraphQL using plain http query (http package). example query to get all users from User list
var client = http.Client();
try {
var response = await client.post('https://keystoneproject.com/admin/api',
body: { 'query': 'query { allUsers { id name email isAdmin }}' });
// this gives you result in JSON format `{ data: { allusers { id: "id value", name: "name of user", email: "email of user", isAdmin: false } }
print(await client.get(response.bodyFields['data']));
} finally {
client.close();
}
body should also contain variable field if your query has any parameter (see GraphQL reference on how that work, I don't think that is scope of the answer)

Related

Add a new field for already existing resource using PUT

I implemented google auth in my NextJS app. The idea is: user makes some progress working with my web app, I store this progress in local storage as an array. If he decides to register I receive the session back, then I send PUT request to db to update the document by inserting a new field (array) from local storage.
I implemented GET request that returns registered user data by email and it works. The question is, how to insert a new field using PUT method? In my case this field is array and calls progress. I'm not sure if I should use update.
This is the record from my mongodb:
_id: 63cc85641624a77f17ca5f29
name: "John P"
email: "john.p#gmail.com"
image: "https://lh3.googleusercontent.com/a/AEdFTp7xzF4eYtyhTgRxmgP4vYdCqDa6zW…"
emailVerified: null
I want to add a new field: progress: ['some data']
This is my PUT request:
case 'PUT':
const updateData = await fetch(`${baseUrl}/updateOne`, {
...fetchOptions,
body: JSON.stringify({
...fetchBody,
filter: { email: email },
update: {} <---------------!!!!!
}),
})
const updateDataJson = await updateData.json()
res.status(200).json(updateDataJson.documents)
break
If you want to update your document using updateOne, and add a progress key, you can use:
filter: { email: email },
update: {$set: {progress: arr}}

A platform returns a POST method to an url and I want to retrieve the data to firestore

I want to retrieve the data from a third party platform that generates a POST call to an url that I can define. But I can only configure the url. I cannot configure the headers or the body of the POST action.
I want to store the JSON into a firestore collection.
The platform calls a POST action to a URL y can define, and that POST action has a JSON with the parameters I want to store.
In another post they directed me to the firestore API
https://stackoverflow.com/a/50061155/18276916
But there it is stated that the body of the POST action must have the following structure:
{
"writes": [
{
object (Write)
}
],
"labels": {
string: string,
...
}
}
The body of the POST action is defined in the third party aplication, and I cant modify it.
Is it posible to do this with Firestore, or is there another method I can use?
What would be the path of the url?:
https://firestore.googleapis.com/v1/projects/[my-project-id]/databases/(default)/documents/[my-collection-name]
Also, can I set the firestore rules to allow everyone to perform create actions so the call doesn't need to be authenticated
I found a solution that works
https://www.jeansnyman.com/posts/google-firestore-rest-api-examples/
With the following structure:
Url: https://firestore.googleapis.com/v1/projects//databases/(default)/documents/
Body:
{
fields: {
title: { stringValue: title },
category: { stringValue: category }
}
}
But here the body has its defined structure. I would need the url to accept:
{
title: title,
category: category
}

Missing conversationId for making Transactions on the new Actions SDK

I'm trying to POST a request to https://actions.googleapis.com/v3/packages/{packageName}/skus:batchGet as described in section 2. b. in the non-consumable digital transactions guide. Pasting the relevant snippet here:
return jwtClient.authorize((err, tokens) => {
if (err) {
throw new Error(`Auth error: ${err}`);
}
const packageName = 'com.example.projectname';
request.post(`https://actions.googleapis.com/v3/packages/${packageName}/skus:batchGet`, {
'auth': {
'bearer': tokens.access_token,
},
'json': true,
'body': {
'conversationId': conversationId,
'skuType': 'APP',
// This request is filtered to only retrieve SKUs for the following product IDs
'ids': ['nonconsumable.1']
},
}, (err, httpResponse, body) => {
if (err) {
throw new Error(`API request error: ${err}`);
}
console.log(`${httpResponse.statusCode}: ${httpResponse.statusMessage}`);
console.log(JSON.stringify(body));
});
});
});
The request body should have a conversationId field. While this field exists in the Dialogflow and legacy Actions SDK, it's missing from the new Actions SDK webhook requests as far as I can tell.
The new Actions SDK documentation links to that digital transactions guide so I have assumed it should be compatible, but have found no mention of required adaptations to be able to use it.
So my question is, how can that conversationId be fetched when making transactions from a webhook fulfilling requests from the new Actions SDK?
The snippet provided in the documentation is incorrect.
Please use the session ID. You can access this value via conv.session.id.

How to identify unique users with Diagflow

I am trying to make an assistant app and was using the cloud firestore service of firebase to send the response back to my app using webhook as fulfilment. I have used 'session' parameter in request JSON according to this documentation and sending fulfilmentText as response to the user. But whenever user launches the app, a new session is created which I don't want. I simply want, just a single entry for each user in my database so how to achieve that using dialogflow.
In Alexa Skill, we have deviceId as parameter by which we can uniquely identify the user irrespective of the session id but is there any parameter in the dialogflow request JSON. If not, then how to achieve this task without it.
The request JSON I am getting from Dialogflow has a userID in it, so can I use the userId or should I go with userStorage provided the userStorage parameter is not available in the request JSON.
request.body.originalDetectIntentRequest { source: 'google', version: '2', payload: { surface: { capabilities: [Object] },
inputs: [ [Object] ],
user:
{ locale: 'en-US',
userId: 'ABwppHG5OfRf2qquWWjI-Uy-MwfiE1DQlCCeoDrGhG8b0fHVg7GsPmaKehtxAcP-_ycf_9IQVtUISgfKhZzawL7spA' },
conversation:
{ conversationId: '1528790005269',
type: 'ACTIVE',
conversationToken: '["generate-number-followup"]' },
availableSurfaces: [ [Object] ] } }
EDIT : Thank You #Prisoner for the answer but I am unable to send the random ID generated in the response and set in in the payload. Below is the code where I am generating the uuid and storing it in firestore. What I am doing wrong in the below code due to which new uuid is generated for returning user and therefore response is shown as No document found in the database. I suppose I am not sending uuid appropriately. Please help.
exports.webhook = functions.https.onRequest((request, response) => {
console.log("request.body.queryResult.parameters", request.body.queryResult.parameters);
console.log("request.body.originalDetectIntentRequest.payload", request.body.originalDetectIntentRequest.payload);
let userStorage = request.body.originalDetectIntentRequest.payload.user.userStorage || {};
let userId;
console.log("userStorage", userStorage);
if (userId in userStorage) {
userId = userStorage.userId;
} else {
var uuid = require('uuid/v4');
userId = uuid();
userStorage.userId = userId
}
console.log("userID", userId);
switch (request.body.queryResult.action) {
case 'FeedbackAction': {
let params = request.body.queryResult.parameters;
firestore.collection('users').doc(userId).set(params)
.then(() => {
response.send({
'fulfillmentText' : `Thank You for visiting our ${params.resortLocation} hotel branch and giving us ${params.rating} and your comment as ${params.comments}.` ,
'payload': {
'google': {
'userStorage': userStorage
}
}
});
return console.log("resort location", params.resortLocation);
})
.catch((e => {
console.log('error: ', e);
response.send({
'fulfillmentText' : `something went wrong when writing to database`,
'payload': {
'google': {
'userStorage': userStorage
}
}
});
}))
break;
}
case 'countFeedbacks':{
var docRef = firestore.collection('users').doc(userId);
docRef.get().then(doc => {
if (doc.exists) {
// console.log("Document data:", doc.data());
var dat = doc.data();
response.send({
'fulfillmentText' : `You have given feedback for ${dat.resortLocation} and rating as ${dat.rating}`,
'payload': {
'google': {
'userStorage': userStorage
}
}
});
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
response.send({
'fulfillmentText' : `No feedback found in our database`,
'payload': {
'google': {
'userStorage': userStorage
}
}
});
}
return console.log("userStorage_then_wala", userStorage);
}).catch((e => {
console.log("Error getting document:", error);
response.send({
'fulfillmentText' : `something went wrong while reading from the database`,
'payload': {
'google': {
'userStorage': userStorage
}
}
})
}));
break;
}
You have a couple of options, depending on your exact needs.
Simple: userStorage
Google provides a userStorage object which is persisted across conversations when it can identify a user. This lets you store your own identifier when you need to track when a user returns.
The easiest way to do this is to check the userStorage object for the identifier when your webhook is called. If it doesn't exist, create one using something like a v4 UUID and save it in the userStorage object.
If you are using the actions-on-google library, the code might look something like this:
let userId;
// if a value for userID exists un user storage, it's a returning user so we can
// just read the value and use it. If a value for userId does not exist in user storage,
// it's a new user, so we need to generate a new ID and save it in user storage.
if (userId in conv.user.storage) {
userId = conv.user.storage.userId;
} else {
// Uses the "uuid" package. You can get this with "npm install --save uuid"
var uuid = require('uuid/v4');
userId = uuid();
conv.user.storage.userId = userId
}
If you are using the dialogflow library, you can use the above, but you'll need this line first:
let conv = agent.conv();
If you're using the multivocal library, it does all of the above for you and will provide a UserID in the environment under the path User/Id.
If you're handling the JSON directly, and you are using the Dialogflow v2 protocol, you can get the userStorage object by examining originalDetectIntentRequest.payload.user.userStorage in the JSON request object. You'll set the payload.google.userStorage object in the JSON response. The code is similar to the above and might look something like this:
let userStorage = body.originalDetectIntentRequest.payload.user.userStorage || {};
let userId;
// if a value for userID exists un user storage, it's a returning user so we can
// just read the value and use it. If a value for userId does not exist in user storage,
// it's a new user, so we need to generate a new ID and save it in user storage.
if (userId in userStorage) {
userId = userStorage.userId;
} else {
// Uses the "uuid" package. You can get this with "npm install --save uuid"
var uuid = require('uuid/v4');
userId = uuid();
userStorage.userId = userId
}
// ... Do stuff with the userID
// Make sure you include the userStorage as part of the response
var responseBody = {
payload: {
google: {
userStorage: JSON.stringify(userStorage),
// ...
}
}
};
Note the first line of the code - if userStorage doesn't exist, use an empty object. It won't exist until you send a response that includes storing something in it for the first time, which will happen in the last few lines of this code.
Advanced: Account Linking
You can request users to sign in to your Action using Google Sign In. This can be done just using voice for the simplest of cases and would only interrupt the flow the first time.
After this, your Action is given a JWT which contains their Google ID which you can use as their identifier.
If you're using the actions-on-google library, you can get the ID from the decoded JWT with a line such as:
const userId = conv.user.profile.payload.sub;
In the multivocal library, the ID from the decoded JWT is available in the environment under the path User/Profile/sub
Deprecated: Anonymous User ID
You'll see some answers here on StackOverflow that reference an Anonymous User ID. Google has deprecated this identifier, which was not always a reliable way to verify returning users, and will be removing it 1 Jun 2019.
This code is currently still being sent, but this will be removed starting 1 Jun 2019.

Custom REST url for specific Model

Is there a way in Ember to configure a custom REST url for a specific Model?
Like with this model:
App.Post = DS.Model.extend({
title: DS.attr('string'),
comments: DS.hasMany('App.Comment')
});
App.Comment = DS.Model.extend({
content: DS.attr('string'),
post: DS.belongsTo('App.Post')
});
And this Store:
app.Store = DS.Store.extend({
revision : 11,
adapter : DS.RESTAdapter.extend({
namespace : 'rest'
})
});
I want that the comments are retrieved via /rest/post/{id}/comments instead of /rest/comments which is the default behaviour.
Is it possible to configure a rest-url for one specific Model?
You can register an additional adapter and 'scope' it to your model.
App.Store.registerAdapter('App.Post', DS.RESTAdapter.extend({
url: "/rest/post/"
}));
Is this for literally just for one model across your entire app or is this the default "hasMany" uri that your REST backend uses? I ask because my api (django rest framework) uses this exact uri and it required a full blown pull request on the ember-data project because to build the URL the adapter needs the related "parent" or "owner" (something rails devs never needed so it didn't exist).
I would write your own adapter (just subclass the base adapter so you only override the single hasMany that is different). The method I wrote for my adapter is below and here is my full blown adapter for reference.
This is ember-data revision 11 friendly btw (have not upgraded to 12 yet)
https://github.com/toranb/ember-data-django-rest-adapter/blob/master/tests/adapter.js
findMany: function(store, type, ids, parent) {
var json = {}
, root = this.rootForType(type)
, plural = this.pluralize(root)
, ids = this.serializeIds(ids)
, url = this.buildFindManyUrlWithParent(store, type, ids, parent);
this.ajax(url, "GET", {
success: function(pre_json) {
json[plural] = pre_json;
Ember.run(this, function(){
this.didFindMany(store, type, json);
});
}
});
},