How to set custom schema for custom remote methods on Strongloop - methods

I'm newbie on Strongloop and I can't find information for how to customize my response class (model schema for a object I built) and I don't know how to show on the API explorer the object with custom data.
For example, I have a custom remote method called score
POST /Challenges/score
I want to show for the parameter data a custom model schema instead of single parameters, not the Model Schema for Challenge, the data on the body have all the parameters and show to the user on the Data Type: Model Schema, is this possible?
{
"id": "string",
"limit": 0,
"order": "string",
"userId": "string"
}
On the other side, in Response Class I want to show the schema for the response object. Something like this:
{
"id":"string",
"userId":"string",
"user": {},
"totalScore":0,
"tags": []
}
I looked different questions (this and this), but can not find something to solve this issues.
Update
Here is the definition of the remote method
Challenge.remoteMethod('score', {
accepts: { arg: 'data', type: 'object', http: { source: 'body' } },
returns: {arg: 'scores', type: 'array'},
http: {path: '/score', verb: 'post'}
});

I believe you might have gone through the official docs of strongloop. If not, here is the link that explains the remote methods and their accepted data types. https://docs.strongloop.com/display/public/LB/Remote+methods
Assuming your custom object is Challenge, to show the object in response you have to specify the type( the type can be one of the loopback's data type or you custom model). So to return Challenge you have to add following code :
Challenge.remoteMethod('score', {
accepts: { arg: 'data', type: 'object', http: { source: 'body' } },
returns: {arg: 'scores', type: 'Challenge', root: true},
http: {path: '/score', verb: 'post'},
});
The second arrow that you have specified is the default values that you want to try out with your API call. You can pass any custom string with default as the key.
For example, if you want to pass some object :
Challenge.remoteMethod('score', {
accepts: {
arg: 'data',
type: 'object',
default: '{
"id": "string",
"userId": "string",
"user": {},
"totalScore": 0,
"tags": []
}',
http: {
source: 'body'
}
},
returns: {
arg: 'scores',
type: 'Challenge'
},
http: {
path: '/score',
verb: 'post'
}
});
So, for response you can not customize the model. But to pass default values you can put anything in the string format.

In loopback, remote arguments can identify data models which have been defined using ds.define('YourCustomModelName', dataFormat);
so for you case, write a function in a Challenge.js file which will have a remote method (in ur case score) defined.
const loopback = require('loopback');
const ds = loopback.createDataSource('memory');
module.exports = function(Challenge) {
defineChallengeArgFormat() ;
// remote methods (score) defined
};
let defineChallengeArgFormat = function() {
let dataFormat = {
"id": String,
"userId": String,
"user": {},
"totalScore": Number,
"tags": []
};
ds.define('YourCustomModelName', dataFormat);
};
Under remote arguments type use 'type': 'YourCustomModelName'
Challenge.remoteMethod('score', {
accepts: {
arg: 'data',
type: 'YourCustomModelName',
http: {
source: 'body'
}
},
returns: {
arg: 'scores',
type: 'Challenge'
},
http: {
path: '/score',
verb: 'post'
}
});
You should see it reflecting on explorer after restarting server and refreshing :)

#jrltt, Instead of using default, use object structure pointing to type under accepts and it should work. Note, http source:body is needed.
With random object:
Challenge.remoteMethod('score', {
accepts: {
arg: 'data',
type: {
"id": "string",
"userId": "string",
"user": {},
"totalScore": 0,
"tags": []
},
http: {
source: 'body'
}
},
returns: {
arg: 'scores',
type: 'Challenge'
},
http: {
path: '/score',
verb: 'post'
}
});
With a defined model which is available in model-config or create using loopback model generator, then that model name can be used to point type.
So lets use User model to show in accepts parameter,
Challenge.remoteMethod('score', {
accepts: {
arg: 'data',
type: 'User',
http: {
source: 'body'
}
},
returns: {
arg: 'scores',
type: 'Challenge'
},
http: {
path: '/score',
verb: 'post'
}
});

The way I found to solve this problem is to create a new model in this way, with the helper slc loopback: model
? Enter the model name: ArgChallenge
? Select the data-source to attach ArgChallenge to: (no data-source)
? Select model's base class PersistedModel
? Expose ArgChallenge via the REST API? No
? Common model or server only? server
And I continue putting properties, then on Challenge.js:
Challenge.remoteMethod('score', {
accepts: { arg: 'data', type: 'ArgChallenge', http: { source: 'body' } },
returns: {arg: 'scores', type: 'array'},
http: {path: '/score', verb: 'post'}
});
And that works! If anyone knows a better way to do this, please share.

I found a way to solve this problem by changing the type parameter in the accepts array.
when we create remoteMethod; we provide accepts array. there is arg, type, required, http. then we can give our request object into type parameter.
Example code
UserModel.remoteMethod(
'login',
{
description: 'Login a user with username/email and password.',
accepts: [
{
arg: 'credentials',
type: {'email': 'string', 'password': 'string'},
required: true,
http: {source: 'body'},
},
{
arg: 'include', type: ['string'], http: {source: 'query'},
description: 'Related objects to include in the response. ' +
'See the description of return value for more details.',
},
],
returns: {
arg: 'accessToken', type: 'object', root: true,
description:
g.f('The response body contains properties of the {{AccessToken}} created on login.\n' +
'Depending on the value of `include` parameter, the body may contain ' +
'additional properties:\n\n' +
' - `user` - `U+007BUserU+007D` - Data of the currently logged in user. ' +
'{{(`include=user`)}}\n\n'),
},
http: {verb: 'post'},
},
);

Related

Dynamically creating graphql schema with circular references

By using graphql-js, I need to create graphql schema dynamically by iterating over array of some data, for example:
[{
name: 'author',
fields: [{
field: 'name'
}, {
field: 'books',
reference: 'book'
}]
}, {
name: 'book',
fields: [{
field: 'title'
}, {
field: 'author',
reference: 'author'
}]
}]
The problem is circular references. When I'm creating AuthorType I need BookType to be already created and vise versa.
So resulting schema should look like:
type Author : Object {
id: ID!
name: String,
books: [Book]
}
type Book : Object {
id: ID!
title: String
author: Author
}
How can I solve this?
Quoted from official documentation
http://graphql.org/docs/api-reference-type-system/
When two types need to refer to each other, or a type needs to refer
to itself in a field, you can use a function expression (aka a closure
or a thunk) to supply the fields lazily.
var AddressType = new GraphQLObjectType({
name: 'Address',
fields: {
street: { type: GraphQLString },
number: { type: GraphQLInt },
formatted: {
type: GraphQLString,
resolve(obj) {
return obj.number + ' ' + obj.street
}
}
}
});
var PersonType = new GraphQLObjectType({
name: 'Person',
fields: () => ({
name: { type: GraphQLString },
bestFriend: { type: PersonType },
})
});
Also look at this related answer of circular Category-Subcategory types
I solved this problem by using a thunk for the fields field.
const User = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLID }
})
});
When you make your fields a thunk rather than an object literal you can use types that are defined later in the file.
Please see this post for more info Is there a way to avoid circular type dependencies in GraqhQL?
Based on that post I think this is the correct way to do it.

retrieve data for grid/chart from httpwebrequest call to url

I am trying to get a grid filled with the json response I would receive when making a httpwebrequest call to a url endpoint which requires authentication. The data will be in json form:
{
"data": [
{
"value": "(\"Samsung Health\")",
"tag": "ME"
},
{
"value": "(\"Samsung Galaxy Tab\")",
"tag": "HIM"
},
{
"value": "(\"Amazon fire\")",
"tag": "ME"
}
]
}
I am not sure how to even start and whether to use Ext.Ajax.Request or some type of call from code behind. I am using vb.net in code behind. Any suggestions appreciated. Sample code for ajax call;
function getMembers() {
var parameters = {
node: dynamicNodeId
}
Ext.Ajax.Request({
url: 'https://data.json',
method: 'GET',
jsonData: Ext.encode(parameters),
success: function (response, opts) {
alert('I WORKED!');
//decode json string
var responseData = Ext.decode(response.responseText);
//Load store from here
memberStore.loadData(responseData);
},
failure: function (response, opts) {
alert('I DID NOT WORK!');
}
});
}
The grid formation:
var grid = Ext.create('Ext.grid.Panel', {
store: store,
stateful: true,
stateId: 'stateGrid',
columns: [
{
text: 'Query',
flex: 1,
sortable: false,
dataIndex: 'query'
},
{
text: 'Last Updated',
width: 85,
sortable: true,
renderer: Ext.util.Format.dateRenderer('m/d/Y'),
dataIndex: 'lastChange'
},
Here query would be the value from the json response and lastChange the current datetime. I tried the proxy request call and realized that since I am calling an endpoint on a different domain I needed to use jsonp.
var myStore = Ext.create('Ext.data.Store', {
model: 'User',
proxy: {
type: 'jsonp',
extraParams: {
login: 'username',
password: 'password'
},
url: 'https://api.data/rules.json',
reader: {
type: 'json',
root: 'rules'
},
callbackParam: 'callback'
},
autoLoad: true
});
I might have to just figure out some other way to do by making sure all the data I needed is called to a database by some other function.
The best approach for your situation would be to create store that is configured with a remote proxy. See the example at the top of this documentation page: http://docs.sencha.com/extjs/4.2.2/#!/api/Ext.data.Store
The remote proxy will take care of the AJAX request to retrieve the data, and the store will automatically manage casting the data results to Ext.data.Model instances. Once the store is loaded with data, the grid to which the store is bound will also automatically handle rendering the data that has been populated into the store.

Kendo Grid calls 'create' operation instead of 'update' after adding new record

I've setup a basic Kendo Grid and I'm using the DataSourceResult class from the PHP Wrapper library on the sever side.
I've come across a strange issue... if I create a new record and then edit it (without refreshing the page), the create operation is called again, rather than the update operation.
If the page is refreshed after adding the new record, the update operation is called correctly after making changes to the record.
I can confirm that the DataSourceResult class is returning the correct data after the create operation, including the id of the new record.
Any ideas why this is happening (and how to stop it)?
Thanks
Update: Here's the datasource code. The query string in the url is just to easily distinguish the requests in Chrome's console. The additional data passed with each request is used by ajax.php to distinguish the different actions requested.
data = new kendo.data.DataSource({
transport: {
create: {
url: '/ajax.php?r=gridCreate',
dataType: 'json',
type: 'post',
data: { request: 'grid', type: 'create' }
},
read: {
url: '/ajax.php?request=gridRead',
dataType: 'json',
type: 'post',
data: { request: 'grid', type: 'read' }
},
update: {
url: '/ajax.php?r=gridUpdate',
dataType: 'json',
type: 'post',
data: { request: 'grid', type: 'update' }
},
destroy: {
url: '/ajax.php?r=gridDestroy',
dataType: 'json',
type: 'post',
data: { request: 'grid', type: 'destroy' }
},
parameterMap: function(data, operation) {
if (operation != "read"){
data.expires = moment(data.expires).format('YYYY-MM-DD HH:mm');
}
return data;
}
},
schema: {
data: 'data',
total: 'total',
model: {
id: 'id',
fields: {
id: { editable: false, nullable: true },
code: { type: 'string' },
expires: { type: 'date' },
enabled: { type: 'boolean', defaultValue: true }
}
}
},
pageSize: 30,
serverPaging: true,
serverSorting: true,
serverFiltering: true
});
Best solution
Set to update, create or delete different Call Action
From Telerik Support :
I already replied to your question in the support thread that you
submitted on the same subject. For convenience I will paste my reply
on the forum as well.
This problem occurs because your model does not have an ID. The model
ID is essential for editing functionality and should not be ommited -
each dataSource records should have unique ID, records with empty ID
field are considered as new and are submitted through the "create"
transport.
schema: {
model: {
//id? model must have an unique ID field
fields: {
FirstName: { editable: false},
DOB: { type: "date"},
Created: {type: "date" },
Updated: {type: "date" },
}
} },
For more information on the subject, please check the following
resources:
http://docs.kendoui.com/api/framework/model#methods-Model.define
http://www.kendoui.com/forums/ui/grid/request-for-support-on-editable-grid.aspx#228oGIheFkGD4v0SkV8Fzw
MasterLink
I hope this information will help
I have also the same problem & I have tried this & it will work.
.Events(events => events.RequestEnd("onRequestEnd"))
And in this function use belowe:
function onRequestEnd(e) {
var tmp = e.type;
if (tmp == "create") {
//RequestEnd event handler code
alert("Created succesully");
var dataSource = this;
dataSource.read();
}
else if (tmp == "update") {
alert("Updated succesully");
}
}
Try to Use this code in onRequestEnd event of grid
var dataSource = this;
dataSource.read();
Hope that it will help you.
Pass the auto-incremented id of the table when you call the get_data() method to display data into kendo grid, so that when you click on the delete button then Deledata() will call definitely.
Another variation, in my case, I had specified a defaultValue on my key field:
schema: $.extend(true, {}, kendo.data.transports["aspnetmvc-ajax"], {
data: "Data",
total: "Total",
errors: "Errors",
model: kendo.data.Model.define({
id: "AchScheduleID",
fields: {
AchScheduleID: { type: "number", editable: true, defaultValue: 2 },
LineOfBusinessID: { type: "number", editable: true },
Not sure what I was thinking but it caused the same symptom.

Kendo UI does not call create if a function is specified

Using Kendo.web.js versions 2013.2.716 and 2012.3.1315, I am trying to use a function in my transport.create rather than calling a URL. What I find is that the function does not get called. Instead a default URL is called and the resulting HTML appears to cause an error in the bowels of kendo because it is expected to be JSON instead.
I assume that this is some type of configuration error, but I can't figure out where the problem is.
Here is a snippet of the code:
var clientListDS = new kendo.data.DataSource({
transport: {
read: {
url: window.baseUrl + 'HealthCheck/ClientSummary',
dataType: 'json',
type: 'POST'
},
create: function(a,b,c) { alert('Create'); },
createY: window.baseUrl + 'HealthCheck/DontCallMe',
createX: {
url: window.baseUrl + 'HealthCheck/DontCallMe',
dataType: 'json',
type: 'POST'
},
whatWeWantCreateToDo: function () {
showChooseDialog('Some Random String', 'Select Client', OnRefreshInactiveClientList);
},
destroy: function () {
alert('destroy');
},
update: function () {
alert('update');
}
},
autoSync: true,
schema: {
model: {
id: 'ID',
fields: {
ID: { required: false, type: 'number', nullable: true },
ClientName: { type: 'string' },
ClientTag: { type: 'string' },
Status: { type: 'string' }
}
}
}
});
Then I use the resulting data source to build a grid like this:
$('#cClientGrid').kendoGrid({
dataSource: clientListDS,
columns: [
{ field: 'ClientTag', title: 'Tag'},
{ field: 'ClientName', title: 'Name' },
{ field: 'Status' }
],
editable: {
mode: 'incell',
createAt: 'bottom'
},
edit: function (pEvent) {
if (pEvent.model.isNew())
alert('create');
else
alert('Edit');
},
toolbar: ['create']
});
Some behavior that is worthy of note:
You see several attempts at the create configuration. If I use CreateY or CreateX, it will call the resulting URL. If I use Create or WhatWeWantCreateToDo, I end up downloading the containing page with each element of my schema as get string items (I assume this is some type of default behavior as I can't find a reference to the URL which is downloaded).
When I turn off autoSync, the grid will call its edit function when I use the toolbar to create a new item. When I turn on autoSync, the edit function does not get called. Instead the data source create functionality runs.
Any thoughts or insight on how I might be able to call a function instead of a URL will be appreciated.
First make in transport everything being an URL or a function, do not mix them up.
If you need to implement read as a function, you simply do:
transport: {
read : function (options) {
$.ajax({
url: window.baseUrl + 'HealthCheck/ClientSummary',
dataType: 'json',
type: 'POST',
success : function (result) {
options.success(result);
}
});
},

Configuring Sencha's Rest Proxy not to send model's ID on a POST

I have a simple Sencha Touch 2.1 model class:
Ext.define('App.model.User', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'id', type: 'number', defaultValue: 0 },
{ name: 'first', type: 'string' },
{ name: 'last', type: 'string' },
{ name: 'email', type: 'string' },
{ name: 'lastUpdated', type: 'auto' }
]
},
fullName: function () {
var d = this.data,
names = [d.first, d.last];
return names.join(" ");
}
});
A collection of these models is in a store that I've configured to use sencha's Rest Proxy. When I add a new model to the store, and call the Sync method on the store, the new model is posted to a ASP.NET WebAPI Users controller and the following action is hit:
// POST api/Default1
public HttpResponseMessage PostUser(User user)
{
// this is a new user -- get rid of the ID
user.Id = 0;
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, user);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.Id }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
The problem is that the a string is being sent as the ID for the model, so it's not passing the ModelState.IsValid check on the controller. This is what's actually getting sent up:
{"id":"ext-record-5","first":"c","last":"d","email":"e","lastUpdated":null}
Any idea why the id field is being set to a string? Also, any idea how I can tell the post action in the Users controller not to validate the id field (as it should be handling creating a new item, it makes sense for the server to create the ID for the new item, not the client).
This link helped me figure it out. The updated model should be:
Ext.define('App.model.User', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'id', type: 'auto', persist: false },
{ name: 'first', type: 'string' },
{ name: 'last', type: 'string' },
{ name: 'email', type: 'string' },
{ name: 'lastUpdated', type: 'auto' }
],
},
fullName: function () {
var d = this.data,
names = [d.first, d.last];
return names.join(" ");
}
});
You need to set idProperty on the model. The field for id should also be set to type auto. See documentation here.

Resources