I am executing a GraphQL query:
export const userByName = /* GraphQL */ `
query employee(
$first_name: String
$sortDirection: ModelSortDirection
$filter: ModelUserFilterInput
$limit: Int
$nextToken: String
) {
employee(
first_name: $first_name
sortDirection: $sortDirection
filter: $filter
limit: $limit
nextToken: $nextToken
) {
items {
id
employee_type
first_name
last_name
createdAt
updatedAt
}
nextToken
}
}
`;
When I execute this query, I get this error:
sortDirection is not supported for List operations without a Sort key defined.
I am executing this query using this code:
const userInfo = await API.graphql(
graphqlOperation(employee, {
first_name: name,
sortDirection: 'DESC'
})
)
I'm trying to retrieve data, but the above error arises. I have also tried removing sortDirection, but I just get an empty array as a response.
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 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
}
}
}
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"}));
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.