write a graphql query based on schema - reactjs

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?

Related

GraphQL - sort error - Unknown argument "sort"

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
}
}
}
}
}
}
}
}
}
}
`

How to filter by ID in this graphql query

I'm trying to add a filter to the following graphql query and I'm just not able to figure out where to add the filter parameter. I went through the documentation but I'm clueless how to do it. I should also retain the limit and offset to do the pagination. I would like to filter by ID.
const data = await axios.post("http://localhost:1337/graphql", {
query: `query {
newsPostsConnection(limit: ${limit}, start: ${start}) {
values {
id
title
body
writtenBy
imageUrl
created_at
}
aggregate {
totalCount
}
}
}`
I think this link answers your question
Schema
type NewsPostsConnection {
id: String! #id
title: String!
body: String
writtenBy: String
imageUrl: String
created_at: DateTime
}
Query
query {
getNewsPostsConnection(id: $id) {
values {
id
title
body
writtenBy
imageUrl
created_at
}
aggregate {
totalCount
}
}
}

Amplify GraphQL get item from other field than ID

I am implementing a webpage with React and AWS Amplify.
I have the following definition in my schema.graphql file:
type Calendar #model {
id: ID!
name: String
description: String
url: String!
intervals: [Interval] #connection(keyName: "byCalendar", fields: ["id"])
}
I would like to get a calendar from its URL string. Unfortunately, the following code throws an error:
import { API, graphqlOperation } from "aws-amplify";
import * as queries from "../../graphql/queries";
await API.graphql(graphqlOperation(queries.getCalendar, { url: "some-url"}));
Variable "$id" of required type "ID!" was not provided.
From the error, providing the id is mandatory. However, I would like to be able to get an object from just the url.
How can I do that?
I am using the queries automatically generated by the amplify's cli.
/* eslint-disable */
// this is an auto generated file. This will be overwritten
export const getCalendar = /* GraphQL */ `
query GetCalendar($id: ID!) {
getCalendar(id: $id) {
id
name
description
url
intervals {
nextToken
}
createdAt
updatedAt
}
}
`;
export const listCalendars = /* GraphQL */ `
query ListCalendars(
$filter: ModelCalendarFilterInput
$limit: Int
$nextToken: String
) {
listCalendars(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
name
description
url
createdAt
updatedAt
}
nextToken
}
}
`;
Found the solution. I had to add a "byURL" key to the model like so:
type Calendar #model #key(name: "byURL", fields: ["url", "id"], queryField: "calendarByURL") {
id: ID!
name: String
description: String
url: String
intervals: [Interval] #connection(keyName: "byCalendar", fields: ["id"])
}
Then write a custom query using that new key (or have amplify to regenerate the queries based on the updated schema.graphql file):
export const getCalendarByURL = /* GraphQL */ `
query calendarByURL($url: String!) {
calendarByURL(url: $url) {
items {
id
name
description
url
intervals {
nextToken
}
createdAt
updatedAt
}
}
}
`;
And this would let me do:
await API.graphql(graphqlOperation(customQueries.getCalendarByURL, { url: "some-url"}));

How to pass variables in Graphql Apollo Query

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
}
}
}
}
}

Use a beginWith or startWith filter in a graphQL query with Gatsby

Following Gatsby tutorial here https://www.gatsbyjs.org/docs/adding-tags-and-categories-to-blog-posts/ it is possible to filter posts by tag to create tag pages easily...
What I'm trying to achieve is to create index pages for posts having the same slug prefx :
/folder1/sub1/post-A
/folder1/sub1/post-B
/folder1/sub2/post-C
Will create 3 index pages :
/folder1/ (containing the three posts)
/folder1/sub1/ (containing post A and B)
/folder1/sub2/ (containing only post C)
This will use a query like :
export const query = graphql`
query tagListQuery($prefix: String, $skip: Int!, $limit: Int!) {
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
filter: { fields: { slug: { startsWith: $prefix } } }
limit: $limit
skip: $skip
) {
edges {
node {
id
frontmatter {
title
}
fields {
slug
}
}
}
}
}
`
But startsWith filtering does not exists :
"message": "Field \"startsWith\" is not defined by type
StringQueryOperatorInput."
Is there a way to filter using prefix matching with graphQL ?
Are you sure you got fields inside node? If so you should show us your schema (found in http://localhost:8000/___graphql), for example:
Anyway, I guess you want to query fileAbsolutePath:
query tagListQuery($prefix: String, $skip: Int!, $limit: Int!) {
allMarkdownRemark(sort: {order: DESC, fields: [frontmatter___date]}, fileAbsolutePath: {regex: $prefix}}, limit: $limit, skip: $skip) {
edges {
node {
id
frontmatter {
title
}
}
}
}
}
If you want to add startWith etc, you need to customize the schema.

Resources