Can’t make query: select() and with() - query-builder

So here is my query
await Order
.query()
.with('order_status')
.select('delivery_date')
.fetch()
The result would be:
[{
delivery_date: "2020-08-29"
order_status: null
}]
as you can see the order_status is null, but when I removed the .select the order_status is not null

Gave up using .with, just used .leftJoin got what I needed to do

Related

Is there a way to avoid returing keys with null values in Apollo GraphQL?

I have a nullable attribute in a type:
type Person {
job: String
}
And a query:
query Persons() {
persons() {
job
}
}
Whevener I query the object, I always get the job key, either with a string or a null value.
{
"job": null
}
But what I need is the job key to be present in the result only when the job is defined, otherwise I want it to be skipped altogether.
{}
I have tried returning null, undefined and skipping the key from the data, but Apollo always returns the key no matter what.
It is much easier to delete field afterwards:
if (result.job === null) {
delete result.job;
}
To achieve the same result with graphql you'll have to split Person into two types: PersonWithJob and PersonWithoutJob. Return a union from persons query and change your query to something like:
query Persons() {
persons() {
... on PersonWithJob {
job
}
}
}

Why can't i filter a Deep nested query in graphql?

So i just started using graphql recently (last week to be precise). I get the concept of being able to filter queries with the 'where' argument and all but i noticed when i try to get objects from a deep nested query it becomes somewhat impossible. So say i have 'domestic_worker_nextofkin' query that should get fullname, mobile_phone1, and relationship where 'archived' _is_null equals true as given below:
domestic_workers_nextofkin(where: {archived:{_is_null: true}}){
full_name
mobile_phone1
domestic_workers_relationships{
relationship
}
}
}
The query above does it's intended job and gets the full_name, mobile_phone1 and relationship.
But my current problem is that when this query is deeply nested as shown below (the asterisked code):
query User($userId: Int) {
domestic_workers_news{
title
date_created
summary
url
}
users_user(where: {id: {_eq: $userId}}) {
domestic_workers_pictures(order_by: {id: desc}, limit: 1) {
id
}
id
password
username
first_name
last_name
email
gender
birth_date
mobile_phone1
mobile_phone2
home_address_street
home_address_town
home_address_lga
home_address_state
home_address_country
home_address_postal_code
job_title
workplace
verified
agreed_terms
date_joined
last_modified
domestic_workers_workers {
domestic_workers_pictures(order_by: {id: desc}, limit: 1) {
id
}
id
first_name
last_name
other_name
email
mobile_phone1
mobile_phone2
home_town
home_state
service
birth_date
gender
date_created
current_employer_id
domestic_workers_relationships{
id
payment
payment_currency
start_date
relationship
domestic_workers_agent {
full_name
mobile_phone1
domestic_workers_relationships{
relationship
}
domestic_workers_pictures(order_by: {id: desc}, limit: 1) {
id
}
}
**domestic_workers_nextofkin (where: {archived:{_is_null: true}}) {
full_name
mobile_phone1
domestic_workers_relationships{
relationship
}
}
}**
domestic_workers_reviews {
id
score
summary
date_created
users_user {
first_name
last_name
domestic_workers_pictures(order_by: {id: desc}, limit: 1) {
id
}
}
}
employerRelationships: domestic_workers_relationships(order_by: {id: desc}, limit: 1, where: {relationship: {_eq: "Employer"}}) {
end_date
relationship
}
}
}
}
It shows the 'where' argument highlighted as red, which possibly means that the 'where' argument shouldnt be placed there which seems rather confusing to me. Can anyone explain why this happens and show me a workaround to pull off the task i'm trying to perform? Thanks.
From the docs:
The where argument can be used in array relationships as well to filter the nested objects. Object relationships have only one nested object and hence they do not expose the where argument.
users_user is an object relationship (it returns a single object instead of an array of them), so it cannot be filtered.

I can only query with primary keys in GraphQL on Appync?

This problem is boggling my mind.
I'm trying to query an RDS table with a non-primary key inside a graphql query.
Here is the example of two of my queries in appsync
query getloc{
getLocationByCounty(County: "County"){
LocationsID
State
County
}
}
query getLocations{
getLocation(LocationsID: 1){
LocationsID
State
County
}
}
The second query here works, and returns exactly the county with that LocationID.
The first returns the error message: "Cannot return null for non-nullable type: 'Int' within parent 'Locations' (/getLocationByCounty/LocationsID)"
If I change the schema for locations and make LocationsID non-nullable the error goes away, but it
returns null.
{
"data": {
"getLocationByCounty": {
"State": null
}
}
}
My request resolver for the getLocationsByCounty is:
"version": "2018-05-29",
"statements": ["select * from Locations where County = '$ctx.args.County'"]
Here is the response resolver:
## Raise a GraphQL field error in case of a datasource invocation error
#if($ctx.error)
$utils.error($ctx.error.message, $ctx.error.type)
#end
$utils.toJson($utils.rds.toJsonObject($ctx.result)[0])
Is this just not allowed in graphQL or is am I doing wrong???
It's worth noting that if I just do this query:
select * from Locations where County = 'County'
in mySQL, it works exactly as intended.

How can i search by field of joined table in graphql and nestjs

i create two table tag and tagTranslation.
following is field of each
Tag
id, type, transloations, creaed_at, updated_at
TagTranslation
id, tag_id, name, language
I use graphql, i want to get tag list by type, name and language
{ tags(name:"tag1", language:"en, type:3){
id,
type,
translations{
id,
name,
language,
}
}
}
so I create resolver like following
#Query(returns => [Tag])
tags(#Args() tagArgs: TagArgs): Promise<Tag[]> {
const where = {
...(tagArgs.type) && {type: tagArgs.type}
};
const include_where = {
...(tagArgs.name) && {name: { [Op.like]: `%${tagArgs.name}%` }},
...(tagArgs.language) && {language: tagArgs.language}
};
return this.tagService.findAll({
where: where,
include: {
as: 'translations',
model: TagTranslation,
where: include_where,
required: true,
}
});
}
#Query(returns => Tag)
tag(#Args({name: 'id', type: ()=> Int}) id: number): Promise<Tag>{
return this.tagService.get(id)
}
#ResolveProperty()
async translations(#Parent() tag): Promise<TagTranslation[]>{
const { id } = tag;
return await this.tagTranslationService.findAll({tag_id: id});
}
when i call tags, the query is called twice
first, A query is executed to get the results I want.
but second,
SELECT `id`, `tag_id`, `name`, `language`, `created_at`, `updated_at` FROM `tag_translation` AS `TagTranslation` WHERE `TagTranslation`.`tag_id` = 1;
query is called once more, so i can't get results what i want.
I think second query is called because of ResolveProperty, I remove ResolveProperty. after that, tag query is not include tagtranslation info...
how can i solve that problem ? or is there another idea??
how can i solve that problem ? or is there another idea??
Relations between entities should be resolved on a field resolver (#ResolveProperty()) level because when someone requests only id and type, you will still perform additional, not needed join on TagTranslation in sql query.

order by in sub document in DocumentDB

I have a json document like following with sub-document
[
{
"id": "73e799df-b7a5-7470-4f25-ee6c1811a5b2",
"tblType": "search",
"memberId": 2,
"results": [
{"prcnt": 89,"distance": 8867775.747141607},
{"prcnt": 30,"distance": 11010216.470879028},
{"prcnt": 96,"distance": 9128590.716183286},
{"prcnt": 41,"distance": 9652043.937920697}
]
}
]
i want to get the 'results' tag data only in the query with order by prcnt
SELECT top 10 m.results FROM m join r in m.results where m.memberId=2
and m.tblType='search' order by r.prcnt
when i am executing the query, getting the error as follows..
Order-by over correlated collections is not supported.
how to get the data per my requirement.
Thanks in advance!!
According to your requirement, I have checked this issue. Here are my understanding about this issue:
If the memberId and tblType could locate the single document, then you could refer to this similar issue:
UDF
function sortByPrcntNumber (results) {
return results.sort(function(a,b){
return a.prcnt-b.prcnt;
});
}
QUERY
SELECT value udf.sortByPrcntNumber(c.results)
from c
where c.memberId=2 and c.tblType='search'
If the memberId and tblType could retrieve multiple documents, I assumed that you could leverage STORED PROCEDURES as follows:
function sample(top,tblType,memberId){
var collection=getContext().getCollection();
var isAccepted=collection.queryDocuments(
collection.getSelfLink(),
"SELECT value {\"prcnt\":r.prcnt,\"distance\":r.distance} from c join r in c.results where c.tblType='"+tblType+"' and c.memberId="+memberId+"",
function(err,feed,options){
if(err) throw err;
if(!feed||!feed.length) getContext().getResponse().setBody("no docs found");
else {
//order by prcnt
var orderedFeed=feed.sort(function(a,b){
return a.prcnt-b.prcnt;
});
//select top
var topFeed=orderedFeed.slice(0,top);
getContext().getResponse().setBody(topFeed);
}
});
if(!isAccepted) throw new Error("The query was not accepted by the server.");
}
Result

Resources