AWS Appsync : How use filter with an array? - aws-amplify

On my Amplify App, I am use this model:
type Post
#model {
id: ID!
title: String!
level: Int!
categories: [Int!]
}
It's easy to filter post by level.
Example: Retrieve all posts that have level 3
I am using this code:
const response = await API.graphql({
query: listPosts,
variables: {
filter: {
level: {
eq: 3
}
}
}
});
My problem is with the categories parameter.
Example: How retrieve all posts that have category 2? How retrieve all posts that have category 3 and 5?
Can you help me?
Regards

Related

How to get post by slug with Sanity GraphQL and NextJS

I am using Next JS 13 and Sanity 3 GraphQL API to build a simple portfolio site. My problem is to get a single post/project by slug instead of id.
The id url ends up looking like:
http://127.0.0.1:3000/projects/093e9421-3dec-4b22-a106-63dd31b0e685
but I want it to use the slug instead.
This is the getStaticFunction function
export async function getStaticProps({ params }: any) {
const GET_PROJECTS = gql`
query SingleProject($slug: String) {
Project(slug: $slug) {
title
_id
slug {
current
}
bodyRaw
summary
category {
title
}
projectImage {
asset {
url
}
}
}
}
`;
const response = await client.query({
query: GET_PROJECTS,
variables: {
slug: params.slug.current,
},
});
const project = response.data?.Project;
return {
props: {
project,
},
};
}
but this gives me:
ApolloError: Unknown argument "where" on field "Project" of type "RootQuery".
Field "Project" argument "id" of type "ID!" is required, but it was not provided.
I also tried to query using the where like so:
query SingleProject($slug: String!) {
Project(where: { slug: { current: { eq: $slug } } }) {
title
}
}
but I get a Unkown arugment "where" on field "Project" of type "RootQuery"

How to get nested data using AppSync generated query?

I have the following graphql schema:
type Post
#model {
id: ID!
title: String!
content: String!
comments: [Comment] #hasMany(indexName: "byPost", fields: ["id"])
}
type Comment
#model {
id: ID!
message: String
post: Post #belongsTo(fields: ["postID"])
postID: ID #index(name: "byPost")
}
AppSync generated the following query for this schema:
export const listPosts = /* GraphQL */ `
query ListPosts(
$filter: ModelPostFilterInput
$limit: Int
$nextToken: String
) {
listPosts(filter: $filter, limit: $limit, nextToken: $nextToken) {
items {
id
title
content
comments {
nextToken
}
createdAt
updatedAt
}
nextToken
}
}
`;
Now when I use it like this:
const postData = await API.graphql({
query: listPosts,
});
There are no comments in result, is returns comments: { nextToken: null } as part of a post, but there are comments, associated with the post in the database.
How can I get nested data in this case?

Does Hasura support returning array of arrays in a custom action?

I am trying to return an array of arrays with a Hasura custom action like this:
type Query {
getFeaturedSubcategories (
args: GetFeaturedSubcategoriesInput!
): [[GetFeaturedSubcategoriesOutput]]
}
where:
input GetFeaturedSubcategoriesInput {
featured : Int
}
type GetFeaturedSubcategoriesOutput {
id : uuid!
title : String
thumbnail : String
}
But I get this error when using this action:
"errors": [
{
"extensions": {
"path": "$[0]",
"code": "parse-failed"
},
"message": "parsing HashMap ~Text failed, expected Object, but encountered Array"
}
]
Does this mean that we can't return an array of arrays with Hasura? I can't find any information about this error or if this can be done in Hasura
This is actually a limitation from GraphQL.
GraphQL doesn't support tuples, but you can write something like this:
type Chart {
name: String
series: [Series]
}
type Series {
name: String!
data: [DataPoint]
}
type DataPoint {
x: Int
y: Float
}

WPGraphQL query fragments for multiple post types

I want to create a query fragment that I can use to query regular WordPress posts and custom post types which share the same properties. Assume I have the following code using graphql aliases:
query getData($includeCategory: Boolean!) {
wp {
data1: customPostTypes(where: {categoryName: "Exammple 1"}, first: 3) {
nodes {
...dataFragment
}
}
data2: posts(first:3) {
nodes {
...dataFragment
}
}
data3: customPostTypes(where: {categoryName:"Example 2"}, first: 3) {
nodes {
...dataFragment
}
}
}
}
and a single query fragment that looks like:
fragment dataFragment on WP_CustomPostType {
title
uri
status
id
categories #include(if: $includeCategory) {
nodes {
name
}
}
}
Because I have to define the type of field that the fragment will be used on, it prevents me from being able to use it for all the post types that I want to. The above example will work for customPostTypes only and not posts as the field that needs to be defined for that is WP_Post
This is purely for cosmetics but it would be awesome to reuse just one fragment for post types/custom post types with the same properties.
Is there a way I can use one query fragment for all of my post types?
UPDATE
This question is similar, however when working with WPGraphQL and custom post types, using code like this:
exports.sourceNodes = ({ actions }) => {
const { createTypes } = actions
const typeDefs = `
interface PostType {
title: String
uri: String!
status: String!
id: ID!
}
type Work implements Node & PostType {
title: String
uri: String!
status: String!
id: ID!
}
type Post implements Node & PostType {
title: String
uri: String!
status: String!
id: ID!
}
`
createTypes(typeDefs)
}
produces the error: UNHANDLED REJECTION Schema must contain uniquely named types but contains multiple types named "WP_Work"

AWS Amplify sorting a connection doesn't work

I just want to do a very simple sort by a single field. Not interested in any composite keys or anything. Just want a primary key, and then to be able to sort on the order field. The docs are pretty unclear on how to get this to happen, so I won't be surprised if I've done something wrong. An example of this very simple use-case would be nice.
My schema is:
type SurveyTemplate #model {
id: ID!
name: String!
sections: [SectionTemplate] #connection
}
type SectionTemplate #model #key(fields: ["id", "order"]) {
id: ID!
name: String!
order: Int!
allowMultiple: Boolean!
isSubsection: Boolean!
sections: [SectionTemplate] #connection
}
My query is:
query surveyQuery($surveyTemplateId: ID!) {
getSurveyTemplate(id: $surveyTemplateId) {
id
sections(limit: 20, sortDirection: ASC, filter: { isSubsection: { eq: false } }) {
items {
id
order
name
}
}
}
}
My understanding is I've created a primary key on id, and a secondary index on order that can be used to sort it, but the results do not come back sorted by the order field either ascending or descending. What am I missing?

Resources