gql: delete row by id only if relations empty - graphql

I want delete row by id only if it not related with other tables.
My SQL (postgres) is:
DELETE FROM material m
WHERE NOT EXISTS (
SELECT FROM material m
LEFT JOIN project_material pm on m.id = pm.material
LEFT JOIN project_materialset_material pmm on m.id = pmm.material
LEFT JOIN materialset_material mm on m.id = mm.material
WHERE (pmm.id IS NOT NULL OR
pm.id IS NOT NULL OR
mm.id IS NOT NULL ) AND m.id=13
)
AND m.id=13
RETURNING m.id;
What GQL mutation should I use for this query?
UPDATE
GQL (result is [], but not related material exists):
query M {
material(where: {
project_materials: {
id: {
_is_null: true
}
}
project_materialset_materials: {
id: {
_is_null: true
}
}
materialset_materials: {
id: {
_is_null: true
}
}
}
)
{
id
project_materials {
id
}
project_materialset_materials {
id
}
materialset_materials {
id
}
}
}
When I replaced id to material in where, result was empty too.

Related

How to use a condition in multiple nested objects in GrapgQl

I am a newbie in GraphQL and just started to use it in a react project. I have this query to fetch products data by a condition and pagination:
query ProductSearch($displayText_contains: String = " ", $skip: Int, $first: Int) {
product(
where: {displayText_contains: $displayText_contains}
skip: $skip
orderBy: {displayText: ASC}
first: $first
) {
displayText <------------- first
id
bag {
contentItems {
... on ProductAdvantage {
displayText <------------- add where condition
htmlBody {
html
}
productId {
contentItems {
... on Product {
id
}
}
}
}
}
}
}
}
As it's shown I want to add a condition to displayText of ProductAdvantage object but I don't know how. While the condition on the first displayText works fine.

apollo client cache for nested queries

I have a nested query (query inside query) with apollo client.
Everything works great, I do the request and get the correct data, but the issue is when I'm trying to use the cache, the cache returns undefined for the nested query prop.
My query:
query GetStudents($first: Int!, $after: String) {
me {
id
email
firstName
lastName
students(first: $first, after: $after) {
edges {
node {
id
created
number
status
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
When I try to use the inMemoryCache, the students is always undefined :
new InMemoryCache({
typePolicies: {
Query: {
fields: {
me: {
keyArgs: false,
merge(existing = {}, incoming = {}, { readField }) {
const id = readField("id", incoming);
const email = readField("email", incoming);
const students = readField("students", incoming);
return {
...
};
},
},
}
}
}
});
I can read correctly the id and email from the cache, but the students (which is the nested query) will be always undefined.
Do I need to read the cache students in a different way because it is a query?

Sorting GraphQL query on multiple queries in Gatsby

I'm using Gatsby as my static generator and Contentful as my datasource.
We've got multiple contentTypes in Contentful (blog, event, whitepaper) and I want to return these in within one query and sorted by createdAt date. So far I have the following which returns each contentType in order of each contentType but not in order of date overall.
Is there a way I can do a sort across the entire query?
{
whitepapers: allContentfulWhitepaper(sort: { order: DESC, fields: createdAt }) {
edges {
node {
id
slug
title
}
}
}
blogs: allContentfulBlogPost(sort: { order: DESC, fields: createdAt }) {
edges {
node {
id
slug
title
}
}
}
events: allContentfulEventPage(sort: { order: DESC, fields: createdAt }) {
edges {
node {
id
slug
title
}
}
}
}
I don't think GraphQL query is able to do the sorting across multiple fields, but you can sort in component
import React from 'react';
import { graphql } from 'gatsby';
const IndexPage = ({ data }) => {
const { whitepapers, blogs, events } = data;
const allDataInDesc = [
...whitepagers.edges.map(e => e.node),
...blogs.edges.map(e => e.node),
...events.edges.map(e => e.node),
].sort((a, b) => { return new Date(a.createdAt) > new Date(b.createdAt) ? -1 : 1; });
return <>...</>
}
export const query = graphql`
{
whitepapers: allContentfulWhitepaper(sort: { order: DESC, fields: createdAt }) {
edges {
node {
id
slug
title
createdAt
}
}
}
blogs: allContentfulBlogPost(sort: { order: DESC, fields: createdAt }) {
edges {
node {
id
slug
title
createdAt
}
}
}
events: allContentfulEventPage(sort: { order: DESC, fields: createdAt }) {
edges {
node {
id
slug
title
createdAt
}
}
}
}
`;
export default IndexPage;
Sure you can sort by multiple fields. Just pass fields and sort order as an array to your query:
query MyQuery {
allContentfulPost(
sort: { fields: [featured, updatedAt], order: [ASC, DESC] }) {
edges {
node {
featured
updatedAt(formatString: "d MM yyyy")
}
}
}
}

Retrieve value from Database using cypress

Can anyone help me with the below scenario?
I have to execute a select statement and store the the result of the query in a variable using Cypress.
Below is the code I tried. I want to store the result of the query
select id from invoices where INumber = '.invoiceNumber.' to variable rec.
Please help me to achive this.
cy.task('sqlServer:execute',"select id from invoices where INumber = '.invoiceNumber.'")
.then(function (recordset) {
var rec = recordset
})
I can give you an example.
in test sepc.js
describe('test', () => {
it("test", () => {
cy.task("query", {
sql: `
select
id
from
invoice
`
}).then((resp) => {
console.log(resp.rows)
});
in index.js
const pg = require("pg");
module.exports = ( on,config ) => {
on( "task", {
query ({ sql, values }) {
const pool = new pg.Pool(config.db);
try {
return pool.query(sql, values)
} catch (e) {
}
}
});
}

Linq to Entities - limiting the results of a search form to only those results that match all choices for a many to many relationship

I have 3 tables in my application
Employee
EmployeeID
EmployeeName
DOB
Skills
SkillID
Description
EmployeeSkills
EmployeeID
SkillID
YearsExperience
What's the best way to write a linq query to allow a user to simultaneously search on information in the Employee table and limit the results to those who match all skills chosen? I've got no problem when it's one skill, or any skill out of those chosen, but I'm stuck on how to only return when they match all. The best I've managed so far is a hideous mass of intersects.
You could loop over the sleected skills, then add a Where(x=>x.Skills.Contails(skill)) to the query for each skill requested. This will eliminate each employee that doesn't have every skill.
Here is the solution I came up with using a very simple version of your table structure:
var q = from e in Employee
join es in EmployeeSkills
on e.EmployeeID equals es.EmployeeID
join s in Skills
on es.SkillID equals s.SkillID
group s by e into grouping
where !skillsToMatch.Except(grouping.Select(x => x.SkillID)).Any()
select grouping.Key;
Where 'skillsToMatch' is an IEnumerable of SkillID's you want the Employee's to have. It will return all Employees that at least have all the skills in 'skillsToMatch'.
If you have linqpad installed you can see how this works with my dummy data with this script:
var Employee = new [] {
new { EmployeeID = 1 },
new { EmployeeID = 2 },
new { EmployeeID = 3 },
new { EmployeeID = 4 },
new { EmployeeID = 5 },
};
var Skills = new [] {
new { SkillID = 1 },
new { SkillID = 2 },
new { SkillID = 3 },
new { SkillID = 4 },
new { SkillID = 5 },
};
var EmployeeSkills = new [] {
new { EmployeeID = 1, SkillID = 4 },
new { EmployeeID = 1, SkillID = 5 },
new { EmployeeID = 3, SkillID = 4 },
new { EmployeeID = 3, SkillID = 1 },
new { EmployeeID = 5, SkillID = 3 },
};
var skillsToMatch = new [] { 4, 5 };
var q = from e in Employee
join es in EmployeeSkills
on e.EmployeeID equals es.EmployeeID
join s in Skills
on es.SkillID equals s.SkillID
group s by e into grouping
where !skillsToMatch.Except(grouping.Select(x => x.SkillID)).Any()
select grouping.Key;
q.Dump();
Play around by changing the 'skillsToMatch' array to see if it provides the behavior you seek.

Resources