I have the following query and i would like it to return the items with a specific parent name
query categorias ($name: String) {
categories(level: 1, first: 13 ) {
edges {
node {
id
name
level
parent {
name: $name
}
}
}
}
}
Can't quite understand how to make this work. I've tried the following as well but "it's not a valid argument"
query categorias ($name: String) {
categories(level: 1, first: 13 ) {
edges {
node {
id
name
level
parent (name:$name{
name
}
}
}
}
}
You need to pass the name variable to the actual query i.e categories.Not sure what the schema is but the basic idea is below. categorias is the name given by you (so that you can use multiple queries) not the actual query. Something like this.
query categorias ($name: String) {
categories(name: $name, level: 1, first: 13 ) {
edges {
node {
id
name
level
parent {
name: $name
}
}
}
}
}
Related
I have a schema below based on which I need to find defaultSubject with respect to SchoolCode
I have tried multiple permutations and combinations but I couldn't find out the relation between SchoolCode and defaultSubject
Schemas
type Query {
school(id: ID, schoolCode: String): School
}
type School {
optionalSubjects: [String!]
code: ID!
defaultSubjects: String!
defaultExams: String!
}
enum SchoolCode {
kvr
ttn
rgs
dps
ris
}
I tried:
query ($schId:ID, $schCode: String){
school(id:$schId,schoolCode:$schCode){
optionalSubjects, defaultSubjects, defaultExams, code, id, name
}
}
{
"schCode":"dps",
"schId":"23"
}
query myquery {
schools {
nodes {
name, id, defaultSubjects
}
}
}
How can I write a query that would help me find a relation between defaultSubjects and SchooCode?
I need your help in order to sort a GraphQL data by the field createdAt in descending order. When queering by ID, the API, doesn't allow you to sort the array of the given ID.
If i remove the sort key, the query runs ok, but the data is in ascending order (older to new) and I need to retrieve all the new values.
Do you know how can I sort the array?
Thank you in advance!
query GetCategory{
category(id: 4, sort: "createdAt:desc") {
data
{
id
attributes
{
name
reviews{
data
{
id
attributes
{
title
body
rating
createdAt
categories{
data
{
id
attributes{
name
}
}
}
}
}
}
}
}
}
}
I just need to add the sorting not at top level, but at fields level, because there is only one id (which you can't sort :)), but just because there are nested fields, you have to sort them at child/fields level i.e reviews (sort: "createdAt:desc")
The full code is:
const CATEGORY= gql`
query GetCategory{
category(id: 5) {
data
{
id
attributes
{
name
reviews (sort: "createdAt:desc") {. <== HERE
data
{
id
attributes
{
title
body
rating
createdAt
categories{
data
{
id
attributes{
name
}
}
}
}
}
}
}
}
}
}
`
I'm trying to update state inside a nested array with a map function and spread operator, but i don't understand how to get the key in the key/value pair to select a nested object.. There is an arrow in the code to the problematic part.
export default class ControlPanel extends Component {
state = {
words:
[
{
word: "a",
id: 1,
column: 1,
synonymns: {
all:[],
selected:[],
noneFound: false
}
}
]
}
}
updateSynonymnState = (wordId, theSynonymns) => {
const { words } = this.state
const newWords = words.map(word => {
if(word.id == wordId){
return {...word, synonymns.all: theSynonymns} //<--- synonymns.all is throwing an error, but that is the key that i need.
} else {
return word
}
})
this.setState ({words: newWords})
}
I could map the synonymns from the start but then I would loose the Id of the word which i need to select the right word..
How can I set synonymns.all = theSynonymns inside that words.map function, or is there a different way i should be able to set a nested key/value pair when mapping the parent parameter?
Immutable logic with nested values can get very tricky to get right. There are plenty of libraries that focus on that as well. For example: Immer, immutability-helper, immutable-js, and many more.
If you don't want to use another library for your state transitions, then you have to do a bit more work. You need to spread out each of the pieces of state from main object to the part you are modifying.
if (word.id == wordId) {
return { ...word, synonyms: { ...word.synonyms, all: theSynonymns } }; //<--- synonymns.all is throwing an error, but that is the key that i need.
} else {
return word;
}
You have to spread synonymns inside of word as well. Here is an example syntax of doing this:
const words = [{
word: "a",
id: 1,
column: 1,
synonymns: {
all: [],
selected: [],
noneFound: false
}
}]
const theSynonymns = ['b'];
const newWords = words.map(word => {
return {
...word,
synonymns: {
...word.synonymns,
all: theSynonymns
}
}
})
console.log(newWords)
I have a table 'Students' in my db(postgress with Hasura) with relation many-to-many with 'Subjects':
type Student = {
id: uuid
name: String
subjects: [subject]
}
type Subject = {
id: uuid
name: String
}
I have a Static QUERY1:
query FilteredStudents($subjects: [String!]) {
students(where: { subjects: { name: { _in: $subjects } } }) {
id
name
}
}
e.g.:
$subjects = ['Math', 'English', 'Physics']
It will find all the students that attend to ANY of those classes.
e.g.:
const student1 = {
id: 1,
name: 'Mike',
subjects: ['Physics', 'Chemistry'] // subjects mapped to names for simplicity
}
My problem is that I want another query which will find all the students that attend to EACH of those classes.
So it shouldn't fetch student1, but should fetch students like this:
const student2 = {
id: 2,
name: 'Walt',
subjects: ['Math', 'English', 'Physics', 'Some', 'other', 'subjects'] // subjects mapped to names for simplicity
}
My only idea was to create dynamic queries like this:
Dynamic QUERY2 (a new query is generated (on runtime) every time the $subject array changes):
query FilteredStudents {
students(where: { _and: [
subjects: { name: { _eq: "Math" } }
subjects: { name: { _eq: "English" } }
subjects: { name: { _eq: "Physics" } }
]}) {
id
name
}
}
But I would really want to avoid that and find some static solution. Is there a way to achieve this using filters offered by Hasura? (https://hasura.io/docs/1.0/graphql/manual/queries/query-filters.html#)
I can name your QUERY2 as static already and QUERY1 as dynamic - 1st is parametrized, 2nd is hardcoded.
You can construct and pass as variable entire where object for both.
You can read this and use _not with _nin to do the same (as '_and' usage). It stil will be a dynamic condition because where is an input type ... and almost the same condition object creation complexity.
update
const subset = ['Math', 'English'];
const condition = { _and: [] };
subset.map( el => {
condition._and.push( {
subjects: { name: { _eq: el } }
});
});
callSomeLazyQuery( { variables: {
where: condition
} );
the same should be valid
const condition = {
_not: {
_and: []
}
};
subset.map( el => {
condition._not._and.push( {
subjects: { name: { _nil: el } }
});
});
const MUTATION_QUERY = gql`
mutation MUTATION_QUERY(
$name: bigint!
) {
insert_name(
objects: {
name: $name
}
) {
returning {
id
name
}
}
}
`;
const [onClick, { error, data }] = useMutation<{}, {}>(MUTATION_QUERY, {
variables: {
name: 1234,
},
});
My mutation query is inserting name in my table and autogenerating the id. On console logging the data variable I can view the fields id and name in the data object. But I am not able to access them them individually. How can I console.log "id". Thank you.
the console.log(data) looks like : {insert_name: {...}}
which expands to :
insert_name:
returning: Array(1)
0: {id: 1, name: 1234}
length: 1
_proto_: Array(0)
_typename: "insert_name_mutation_response
You can access the fields of an object with .
For example, if your object looks like this -
data = {
id: 1,
name: 'Jane',
}
You can get just the id with data.id
This works no matter how many layers deep your object may go, so take this example -
data = {
person: {
id: 1,
name: 'Jane',
}
}
You could get the id of person with data.person.id.
console.log(data.insert_name.returning[0].id) will give you the id returned.
For it to work in typescript we need to change the query to add the return type of data
const [onClick, { error, data }] = useMutation<{ReturnInsertNameProps}, {}>(MUTATION_QUERY, {
variables: {
name: 1234,
},
});
interface ReturnInsertNameProps {
insert_name: ReturnQueryProps;
}
interface ReturnProps {
returning: MessageProps[];
}
interface NameProps {
id: number;
name: number;
}
We can also use onCompleted method provided in useMutation if we want to process the result of the query.