How do you return GraphQL elements that are empty or have no value? - graphql

What's the recommended way to return the value of an element with no value? Do you exclude it from the selection list or set it to null/none? Irrespective, what's the best way to code this from the client's perspective?
try {
display (obj.name, obj.val);
} catch(e)
{
log(e);
}
or
if obj.name && obj.val
display(obj.name, obj.val);

Related

cypress: How can manage the application flow, if the element xpath is not present

I have the following scenario:
if the element is present, i have to do one activity and if not present will do another activity.
cy.xpath("//div[text()= 'button').its('length').then(res=> {
if (res > 0) {
return 1;
}
else {
cy.log ("Element is not present")
}
}
)} '''
if element is present = Code is working fine,
if the element xpath is not present = it try to search the element xpath (//div[text()= 'button') and throwing the error as 'Timed out retrying: Expected to find element: undefined, but never found it.'
if element is not present, Is there any way, i can handle the code ,
When using xpath you can (sort of) make it conditional by wrapping the xpath selector with count().
cy.xpath("count(//div[text()= 'button'])") // ok with async content
.then(count => {
if (count) {
//return 1; // not useful, returns a "chainer"
// ...but you can perform the required test here, e.g
cy.xpath("//div[text()= 'button']").click()
} else {
cy.log('not found')
}
})
The shorter syntax using built-in jQuery might be
const exists = !!Cypress.$("div:contains('button')").length
if (exists) {
cy.xpath("//div[text()= 'button']").click()
} else {
cy.log('not found')
}
Note that this is a partial match to 'button', where-as the xpath syntax is an exact match.
Also note - using Cypress.$ by-passes retry-ability, so it should not be used where the text is asynchronous.
From docs
This is a great way to synchronously query for elements when debugging from Developer Tools.
The implication is that it's more for debugging after the page has loaded.
The best practice is to try to construct the test and the app's data in such a way that you know that the button is present.
You can do something like this. With Cypress.$, you can validate the presence of the element with the help of the length attribute and then do further actions.
cy.get('body').then($body => {
const ele = $body.find('selector');
if (Cypress.$(ele).length == 0) {
//Do Something element is not present
}
else if (Cypress.$(ele).length > 0) {
//Do Something when element is present
}
})

How to store parameters for action to be used again later

I have a list view that can be sorted, searched and filtered. From that list view the user can edit items in multiple steps. Finally after editing and reviewing the changes the user goes back to the list. Now I want the list to use the same sorting, search term and filters that the user set before and show the correct results.
How can multiple paramters (sorting, search, filter) be stored and reused when showing the list action?
Possible unsatisfactory ways that I thought of:
pass through all the needed parameters. Does work hardly if there are multiple actions involved between the two list action calls
save the parameters in the session object. This seems to require a lot of code to handle multiple parameters (check if parameter was passed to action, store new value, if parameter was not passed, get old parameter from session, handle empty string parameters):
Long longParameter
if(params.containsKey('longParameter')) {
longParameter = params.getLong('longParameter')
session.setAttribute('longParameter', longParameter)
} else {
longParameter = session.getAttribute('longParameter') as Long
params['longParameter'] = longParameter
}
If you want to make it more generic you could use an Interceptor instead.
This could perhaps be generalized like this:
class SessionParamInterceptor {
SessionParamInterceptor() {
matchAll() // You could match only controllers that are relevant.
}
static final List<String> sessionParams = ['myParam','otherParam','coolParam']
boolean before() {
sessionParams.each {
// If the request contains param, then set it in session
if (params.containsKey(it)) {
session[it] = params[it]
} else {
// Else, get the value from session (it will be null, if not present)
params[it] = session[it]
}
}
true
}
}
The static sessionParams holds the parameters you want to store/retrieve from session.
If the params contains an element from the list, it is stored in session under the same name. If not, it is taken from session (given that it exists).
In your controller, you can now just access params.getLong('theParam') like you always would. You could also use Grails parameter conversion:
def myAction(Long theParam) {
}
Lots of LOC saved.
I use the session as well. Here is a sample that you may adapt to your needs:
def list() {
if (request.method == 'GET' && !request.queryString) {
if (session[controllerName]) {
// Recall params from memory
params.putAll(session[controllerName])
}
} else {
// Save params to memory and redirect to get clean URL
session[controllerName] = extractParams(params)
redirect(action: actionName)
return
}
// Do your actions here...
}
def extractParams(params) {
def ret = [:]
params.each { entry ->
if (entry.key.startsWith("filter_") || entry.key == "max" || entry.key == "offset" || entry.key == "sort" || entry.key == "order") {
ret[entry.key] = entry.value
}
}
return ret
}
Using session is your best bet. Just save the preference when preferred. I mean, when user sorts, or filter, just save that information in the session, for that particular <controller>.<action>, before returning the page. Next time, check the session, if it has anything related to that <controller>.<action>, apply those; otherwise render the default page.
You might like to use some Interceptor for this, as suggested by sbglasius, here.
I hope you're getting my point.

Elasticsearch: Return empty value from script field

I'm using Elasticsearch, and I have a script field written in Groovy. It needs to replace the value of myField for some values, but I want to let most of the values pass through unchanged. The script below works except for documents that don't have the field at all. How can I make the script field value empty?
if (some criteria) {
return ...; // modified value
}
// This returns 0 if myField doesn't exist
return doc['myField'].value
I can check the empty condition, but I haven't found a way to return an empty value:
if (doc['myField'].empty)
return ???
It's perfectly ok to return null. I think the problem is more in the sequencing of your different test conditions. I've tried to reproduce your issue and I'm able to return null with the following script:
"script_fields": {
"my_script_field": {
"script": "if (doc.myField.empty) { return null } else if (some criteria) { return modifiedValue } else { return doc.myField.value }"
}
}
First check for fields with empty values and return null.
Then, you're guaranteed to have non-empty and existing fields. So you can check for your criteria and return whatever modified field you want.
Finally, you can return the unmodified values for non-empty fields which do not meet your specified criteria.
The script looks like this:
if (doc.myField.empty) {
return null
} else if (some criteria) {
return modifiedValue
} else {
return doc.myField.value
}

dc.js add multiple filters at once

Is it possible to add a set of filters at once on a dc chart? For instance, say I have a pieChart, and an array of filter values to apply.
var osChart = dc.pieChart('#oschart');
And an input set of filters, say
var filters = ["linux", "mac osx", "windows", "solaris"]
How can I apply filters so that only one "filtered" event is generated?
I can do something like
for (var i=0; i < filters.length; i++) {
osChart.filter(filters[i]);
}
However that would generate 4 filtered event.
I am applying filters based on what a user typed on a text box. When a filter is applied, I am making some ajax calls, and this tends to slow down if I apply filters one by one. I can avoid extra ajax calls if the filters can be set at once.
crossfilter has a function filterFunction which can get this task done, but I am not sure how I can apply that on a dc chart. Apply filterFunction on osChart.dimension() did not work. With the latest dc release, I saw some functions like addFilterHandler and removeFilterHandler however I cannot test and deploy that version right now.
What other options do I have?
From looking at the code, which is somewhat convoluted, you can pass the array inside another array to .filter() (or the undocumented but unmagical .replaceFilter()) without a performance penalty, because it will apply all of the filters before invoking the filtered event.
From the latest .filter() source, which uses handlers but has the same behavior:
if (_ instanceof Array && _[0] instanceof Array && !_.isFiltered) {
_[0].forEach(function (d) {
if (_chart.hasFilter(d)) {
_removeFilterHandler(_filters, d);
} else {
_addFilterHandler(_filters, d);
}
});
} else if (_ === null) {
_filters = _resetFilterHandler(_filters);
} else {
if (_chart.hasFilter(_)) {
_removeFilterHandler(_filters, _);
} else {
_addFilterHandler(_filters, _);
}
}
applyFilters();
_chart._invokeFilteredListener(_);
So if it finds an array that does not have an isFiltered method, and that array's first element is also an array, it will iterate over the elements in the nested array.
For example, pass [["linux", "mac osx", "windows", "solaris"]] to filter on those four values. (Thanks #marcin for clarifying!)

How do I delete an element returned from the each enumerator?

I have an array containing objects. Each object has an id field. I want to implement a function that deletes an object by specifying the id. I use .each from prototypejs on the array to step through the objects and check the id. If it matches, how do I actually delete it? I've tried setting the object returned from .each to null, but using FireBug I see that the object is still the same in the array.
EDIT: the objects in the array may in turn contain arrays with objects that may need to be deleted. My function is fine for finding the object to be removed, and I use splice to remove it (using a counter). It seems to me that .each (and the other enumerators like .reject) returns a copy of the object. If I set the object to null then upon inspection the object is still in the array. How would I return a reference of the object that when set to null would actually operate on the object in the array, and not a copy?
Here is the function, the deleteChild function works on the same principal:
function removeControl(controlName) {
var counter = 0;
cont.each(function (existingControl) {
if (existingControl.id == controlName) {
existingControl.destroy();
cont.splice(counter, 1);
}
else { // not found, check control's children
existingControl.deleteChild(controlName);
}
counter++;
}, this);
}
Only use .each when you want to do something to every object. Semantically speaking, you should be using Enumerable.reject in this situation. Think how much easier it will be to understand when you have to fix it in years' time.
function deleteById(objects, id) {
return objects.reject(function(obj) {
return obj.id == id;
}).each(function(obj) {
obj.deleteChild(id);
});
}

Resources