GraphQL - Default values instead null - graphql

How to set global behavior of GraphQL with default values for object and array ?
Some examples
const AddressTC = schemaComposer.createObjectTC({
name: 'Address',
fields: {
street: {type: 'String',defaultValue: null},
number: {type: 'String', defaultValue: null},
postcode: {type: 'String', defaultValue: null},
city: {type: 'String', defaultValue: null},
comment: {type: 'String', defaultValue: null},
country: {type: 'String', defaultValue: null},
quality: {type: 'QualityEnum', defaultValue: 'BAD'},
}
});
const customerTC = schemaComposer.createObjectTC({
name: 'Customer',
fields: {
address: AddressTC
needs: {
type : ['objectID'],
defaultValue: []
},
documents: {
type: schemaComposer.getOTC('Document').List,
defaultValue: []
},
}
});
In my case I use noSQL database and when a field doesn't exist or is empty, the request GraphQL always returns null then crashed my app with wrongs data type.
INFO : I use graphql-compose
I found this post, whith the same problem, but this solution not useful for me with a large API and dynamics resolvers. Graphql, how to return empty array instead of null

Related

Validate datetime in Strapi

I create DateTime picker in Strapi with the required field validation but can't save. occurring following error.
apI Parametr
components: []
contentType: {name: "Events", description: "", connection: "default", collectionName: "events",…}
name: "Events"
description: ""
connection: "default"
collectionName: "events"
attributes: {title: {type: "string", required: true},…}
title: {type: "string", required: true}
created_by: {nature: "oneWay", target: "plugins::users-permissions.user", dominant: false, unique: false}
image: {type: "media", multiple: false, required: true}
seo_description: {type: "text"}
seo_title: {type: "string"}
description: {type: "richtext", required: true}
calendly_url: {type: "string"}
start_date: {type: "datetime", required: true}
type: "datetime"
required: true
end_date: {type: "datetime"}
PUT http://localhost:1337/content-type-builder/content-types/application::events.events 400 (Bad Request)```

GeoCode filter in mongoose elasticsearch

I am trying to do geo point filter in mongodb in meanjs. i have used mongoosastic modules but i am not able to perform geo point filter.
here below are the mongoose schema and controller code for filter.
Mongoose schema
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
mongoosastic = require('mongoosastic');
var BusinessSchema = new Schema({
name: {type: String, unique: 'Name already exists', trim: true, required: 'Name is required.', es_indexed: true},
searchTags: {type: [String], es_indexed: true},
alias: {type: Array, es_indexed: true},
// geoLocation: { type: [Number], /*/ [<longitude>, <latitude>]*/ index: '2d', /*/ create the geospatial index,*/ required: 'GeoLocation is required.', es_indexed:true,es_type:'geo_point'},
geo_cords: {
type: Array
},
address: {
address1: {type: String, required: 'Address is required', trim: true},
address2: String,
city: {type: String, required: 'City is required', trim: true},
// state: {type: String, required: 'State is required', trim: true},
country: {type: String, required: 'Country is required', trim: true},
postalCode: {type: String, required: 'Postal code is required', trim: true},
neighbourhood: String
},
isActive: {
type: Boolean,
default: true,
es_indexed: true
},
dateUpdated: {
type: Date
, es_indexed: true
},
dateCreated: {
type: Date,
default: Date.now
, es_indexed: true
}
});
controller code for filter and query
var mongoose = require('mongoose'),
Business = mongoose.model('Businesses');
var query = {
"query_string": {
"multi_match": {
"query": categoryIds.join(' OR '),
"fields": ["categoryIds", "relatedCategoryIds"]
}
},
"filter": {
"bool": {
"should": [
{"term": {"address.postalCode": "110016"}},
{"geo_distance": {
"distance": "50km",
"geo_cords": [-122.3050, 37.9174]
}
}
],
}
}
}
Business.search(query, function (err, results) {
// sendResponse(req, res, err, results)
if (!err) {
res.json(results);
} else {
res.status(400).send({message: 'Business Not Found'})
}
});
while doing this i am getting a long error saying
QueryParsingException[[businessess] failed to find geo_point field [geo_cords]
According to the documentation of mongoosastic
Geo mapping
Prior to index any geo mapped data (or calling the synchronize), the mapping must be manualy created with the createMapping (see above).
First, in your schema, define 'geo_cords' this way:
geo_cords: : {
geo_point: {
type: String,
es_type: 'geo_point',
es_lat_lon: true
},
lat: { type: Number },
lon: { type: Number }
}
Add an es_type: 'object' to each Array or embbeded type
alias: {type: Array, es_indexed: true, es_type: 'object'}
Then call .createMapping() on the model just after you've created it.

Extjs proxy just post dirty fields to server, not all filelds

I have a model:
Ext.define('CrudTest.model.User', {
extend: 'Ext.data.Model',
idProperty: 'Id',
fields: [
{ name: 'Id', type: 'int' },
{ name: 'Name', type: 'string' },
{ name: 'PhoneNumber', type: 'int' },
{ name: 'Address', type: 'string' },
{ name: 'StateId', type: 'int', reference: 'State' },
],
validators: [
{ type: 'presence', field: 'Name', message: 'define name, please' },
{ type: 'length', field: 'PhoneNumber', max: 8, messsage: 'lower than 8 digit' },
],
proxy: {
type: 'ajax',
api: {
create: 'home/new',
read: 'home/users',
update: 'home/Edit',
destroy: 'home/Delete'
},
},
});
and a form that load data to form by loadRecord() and my handler code for submit button is:
var form = this.up('form').getForm();
if (form.isValid()) {
form.getRecord().save();
}
it make a post request through my proxy model good. but the body of request just have dirty(edited) fields. why i don't have other fields?
but in request body i have just dirty fields. why? i know updateRecord() uses getFieldValues([onlyDirty=false]), how can send all fields values?
I use extjs 5
Finally find the problem. Ext.data.writer.Writer has a config property writeAllFields
So i change the proxy to this:
proxy: {
writer:{ writeAllFields:true },
type: 'ajax', //also works with type: 'direct',
api: {
create: 'home/new',
read: 'home/users',
update: 'home/Edit',
destroy: 'home/Delete'
},
You can set crtical: true on the Model to any fields you always want written whether changed or not, e.g.
{ name: 'title', type: 'string', critical: true }

Validations in Sencha Touch Architect

I am very new to sencha touch, and i am using Architect. I have a controller "RegisterUser"(controlleractiontap) which has a function.. when i fill in some user data and I click register button in my formview it will write the user to my local database.
I have a model called "userModel" which contains the following:
fields: [
{
name: 'Username',
type: 'string'
},
{
name: 'Password',
type: 'string'
},
{
name: 'Firstname',
type: 'string'
},
{
name: 'Lastname',
type: 'string'
},
{
name: 'Phonenumber',
type: 'string'
},
{
name: 'Email',
type: 'string'
}
],
validations: [
{
type: 'presence',
field: 'Username'
},
{
type: 'email',
field: 'Email'
},
{
type: 'length',
field: 'PhoneNumber',
max: 10,
min: 10
}
the problem: how do i call my validators in the controller so it will validate the fields in my form?
if i missing some information to make it more clear just let me now.
Thanks in advance!
var val=Ext.create('talkbag.model.Registration',new Ext.getCmp("registration").getValues());
where "registration" is the id assigned to the view that extends Ext.form.Panel
var check=val.validate();
now you can check like this if (!check.isValid())

ExtJS Model Validations: Dates (How to)

What's the best way to go about adding validation to a date field in a model
Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'name', type: 'string'},
{name: 'age', type: 'int'},
{name: 'phone', type: 'string'},
{name: 'gender', type: 'string'},
{name: 'username', type: 'string'},
{name: 'alive', type: 'boolean', defaultValue: true}
],
validations: [
{type: 'presence', field: 'age'},
{type: 'length', field: 'name', min: 2},
{type: 'inclusion', field: 'gender', list: ['Male', 'Female']},
{type: 'exclusion', field: 'username', list: ['Admin', 'Operator']},
{type: 'format', field: 'username', matcher: /([a-z]+)[0-9]{2,3}/}
]
});
Let's say the above code contained a 'dob' field for date of birth. How would I go about adding a validation for it?
My assumption is that I would use :
{type: 'format', field: 'dob', matcher: /([a-z]+)[0-9]{2,3}/}
but would use a regex that's designed to validate a date. Is there a better way to do this? I noticed that date fields on forms use their own validation methods to highlight a date field. Is there something like that for date fields in models?
add validate method to Ext.data.validations (singleton),
and it will be use at Ext.data.Model.validate().
take a look at src

Resources