I have a list year wise inside a node. Is it possible to validate key using .validate or something?
like I have a list like this:
"list": {
"year-2015": {
// data
},
"year-2016": {
//data
}
// etc etc
}
Can I validate key using regex /year-[0-9]{4}/ in security rules?
Yes. Validation rules can be used to validate keys.
You could define a rule like this:
{
"rules": {
"list": {
"$key": {
".validate": "$key.matches(/^year-[0-9]{4}$/)"
}
}
}
}
Related
I have a GraphQL API and want to filter the results based on the nested tag object. The object looks like this:
{
name
...
tags [
{
name
},
{
name
},
...
]
}
I now want to get all documents that have both tags, a tag with the name "invoice" AND a tag with the name "open". How would I do that?
I already tried doing it like this:
query {
documents (
where: { tags: { some: { name: { and: { in: [ "invoice", "open" ] } } } } }
) {
nodes {
name
tags {
name
}
}
}
}
and some other stuff, but I can't seem to get it to work.
Thanks in advance :)
I believe that you will implement that on the Backend, because the GraphQL is only a descriptive way to do "Queries and Mutations"
😁
I'm querying a scheme that looks like this:
{
collections {
listings {
tokenid
price
}
tokens {
id
collection
}
}
}
How can I loop through the tokens field and get listings with the tokenid?
I imagine it looks something like this:
{
collections {
tokens {
$id: id
collection
listings(where: {tokenid: $id}) {
tokenid
price
}
}
}
}
If the API supports introspection, you should be able to view the schema documentation and see how it works. You can try to use a GraphQL-capable client, like Insomnia or GraphiQL.
While the exact implementation might require a different structure, my guess will be something like this:
query($id: ID!) {
collections {
listings(tokenid: $id) {
tokenid
price
}
}
}
and you'll need to specify id as a parameter inside the client.
(From Strapi) I am trying to get all "acts" with a certain age (can return multiple) and with a certain place (can return multiple). I can't figure out how to filter that.
This is what I am trying in GraphQL-playground (works without the variables), but it says "Unknown argument "age" on field "Act.ages"." (and "place" respectively).
query GetActs ($age:Int, $place:String) {
acts {
data {
id
attributes {
Title
ages (age: $age) {
data {
id
attributes {
age
}
}
}
places (place: $place) {
data {
id
attributes {
place
}
}
}
}
}
}
}
I just ran into this same issue. I can't make out the error you're reporting, but here is what worked for me.
You can use filter at the collection level to drill down to nested fields for the corresponding attributes. This follows the GraphQL example at the bottom of this Strapi resource on filtering nested fields.
Solution
query GetActs ($age:Int, $place:String) {
acts (filters: {ages: {age: {eq: $age}}, places: {place: {eq: $place}}}) {
data {
id
attributes {
Title
ages {
data {
id
attributes {
age
}
}
}
places {
data {
id
attributes {
place
}
}
}
}
}
}
}
I'm using strapi V4 plugin system and I want to query all comments relative to a specific post.
The default graphql request look like that :
query () {
findAllFlat (relation: "api::post.post:77") {
data {
...
}
}
}
But I need to add a variable instead of 77, ideally I would like to do something like this :
query ($postId:ID!) {
findAllFlat (relation: "api::post.post:${postId}") {
data {
...
}
}
}
But of course, string template doesn't work with graphQL. Is there another way ?
This might be impossible in graphql/contentful or introduce too much complexity but I'm trying to query a collection and filter on a collection field, something like the following...
query {
eventCollection(
where: {
OR: [
{ categoryCollection: { key: "fashion" } }
]
}
) {
items {
slug
}
}
My back up plan is to query all the events and filter in the client but I thought it would be possible to do the above.
Contentful DevRel here. 👋
Currently, that's not possible. But what you can do is flip the query around and filter on the categoryCollection and then use linkedFrom to request the items linking to it.
query {
categoryCollection(where: {
key: "fashion
}) {
items {
title
linkedFrom {
eventCollection {
items {
slug
}
}
}
}
}
}