Strapi Graphql deep sort - graphql

I would like to sort with strapi's graphql
I can do that by
{
users (sort: "id:asc") {
id
username
email
address {
id
city
state
street
}
}
}
I can sort by the first level of columns but how do I go deeper, for example address.city, because
". / :" don't work, graphql playground returns:
"errors": [
{
"message": "select \"users\".* from \"users\" order by \"address\".\"city\" desc limit $1 - missing FROM-clause entry for table \"address\"",

Related

How to limit the number of items of nested array in Apollo graphQL / Apollo Link Rest?

I Have a query like this
`
query {
doctors #rest(type: "doctors", path: "/doctors/") {
Result {
Id
FirstName
LastName
Items (limit: 10) {
id
title
}
}
}
`
I want to get only 10 items from the Items, but I get the full list!
Is This doable or not?
Thank you.

How can I create a relationship between `json` column and a `int` (id) column in Hasura + Postgres?

I have 2 tables users and post
Table users has columns id and post, column contains an array of the form [1, 2, 3, 4, 5] - where 1, 2, 3, 4, 5 is id in table post
In the table posts the following columns id and text
Table users:
https://i.stack.imgur.com/ywdS7.png
Table posts:
https://i.stack.imgur.com/IBdpb.png
in hasura made an array relation
https://i.stack.imgur.com/311sd.png
Next I made the following request
{
users_test {
postz {
id
}
}
}
I would like to receive such data in response:
postz: [
   {
     text: 'qwe'
   },
   {
     text: 'sdf'
   }
]
But with such a request, I get a trace. error:
{
"errors": [
{
"extensions": {
"internal": {
"statement": "SELECT coalesce(json_agg(\"root\" ), '[]' ) AS \"root\" FROM (SELECT row_to_json((SELECT \"_5_e\" FROM (SELECT \"_4_root.ar.root.postz\".\"postz\" AS \"postz\" ) AS \"_5_e\" ) ) AS \"root\" FROM (SELECT * FROM \"public\".\"users_test\" WHERE ('true') ) AS \"_0_root.base\" LEFT OUTER JOIN LATERAL (SELECT coalesce(json_agg(\"postz\" ), '[]' ) AS \"postz\" FROM (SELECT row_to_json((SELECT \"_2_e\" FROM (SELECT \"_1_root.ar.root.postz.base\".\"id\" AS \"id\" ) AS \"_2_e\" ) ) AS \"postz\" FROM (SELECT * FROM \"public\".\"posts\" WHERE ((\"_0_root.base\".\"post\") = (\"id\")) ) AS \"_1_root.ar.root.postz.base\" ) AS \"_3_root.ar.root.postz\" ) AS \"_4_root.ar.root.postz\" ON ('true') ) AS \"_6_root\" ",
"prepared": true,
"error": {
"exec_status": "FatalError",
"hint": "No operator matches the given name and argument type(s). You might need to add explicit type casts.",
"message": "operator does not exist: json = integer",
"status_code": "42883",
"description": null
},
"arguments": [
"(Oid 114,Just (\"{\\\"x-hasura-role\\\":\\\"admin\\\"}\",Binary))"
]
},
"path": "$",
"code": "unexpected"
},
"message": "postgres query error"
}
]
}
What am I doing wrong and how can I fix it?
A few suggestions:
There are some typos in your query, as far as I can tell. Try:
{
users {
id
posts {
text
}
}
}
You don't need the post column on the users table. You just need a user_id column on the posts table, and a foreign key constraint from the posts table to the users table using the user_id and id columns of the tables respectively. Check out the docs here:
https://docs.hasura.io/1.0/graphql/manual/schema/relationships/create.html#step-3-create-an-array-relationship
https://docs.hasura.io/1.0/graphql/manual/schema/relationships/database-modelling/one-to-many.html
If you have to have the post array column for some reason, you can use computed fields to create a "relationship" between a json array and another table’s id.
https://docs.hasura.io/1.0/graphql/manual/schema/computed-fields.html#table-computed-fields
Your function would:
Take in the json array column
Extract the id's
Return select * from table where id in id's
Example:
https://jsonb-relationships-hasura.herokuapp.com/console/api-explorer
Computed field definition at: https://jsonb-relationships-hasura.herokuapp.com/console/data/schema/public/tables/authors/modify
Run these queries:
# Get list of articles for each author
query {
authors {
id
name
articles
}
}
# Get actual articles for each author
query {
authors {
id
name
owned_articles {
id
title
}
}
}

Handle graphql schema stitching error in child query

I am new to graphql and want to understand the concept here. I have this graphql schema (stitched using graphic-tools). Not all cars have registration. So if I query for 5 cars and one car doesn’t have a registration (no id to link between cars and registration), my whole query fails.
How do I handle this and return null for that 1 car and return registration details for the other 4?
{
Vehicles {
Cars {
id
registration {
id
}
}
}
}
If you mark a field as non-null (by appending ! to the type) and then resolve that field to null, GraphQL will always throw an error -- that's unavoidable. If it's possible for a field to end up null in the normal operation of your API, you should probably make it nullable.
However, errors "bubble up" to the nearest nullable field.
So given a schema like this:
type Query {
cars: [Car!]!
}
type Car {
registration: Registration!
}
and this query
{
cars {
registrations
}
}
resolving the registration field for any one Car to null will result in the following because the cars field is non-null and each individual Car must also not be null:
{
"data": null,
"errors": [...]
}
If you make the cars field nullable ([Car!]), the error will stop there:
{
"data": {
"cars": null
},
"errors": [...]
}
However, you can make each Car nullable (whether the field is or not), which will let the error stop there and result in an array of objects and nulls (the nulls being the cars that errored). So making the cars type [Car]! or [Car] will give us:
{
"data": {
"cars": [{...}, {...}, null]
},
"errors": [...]
}

How can I filter by uid of a linked document / relationship using the prismic graphql api?

I am trying to list a set of articles by their categories, by uid, and I'm assuming that I would have to use the where query, but I'm not able to get that to worked on linked documents.
The issue seems to be that where only accepts a string on a field, but in the case of a linked document you would need to dig down to the uid field.
I'm not sure if I'm using the wrong query, but struggling to find anything in the documentation to help me out.
I tried digging into the category object:
{
allDirectoryServices(
where: { category: { _meta: { uid: "developers" } } }
) {
edges {
node {
name
city
region
country
category {
...on DirectoryTaxonomy {
_meta {
uid
}
name
}
}
}
}
}
}
But that returns an error that it's expecting a string:
"message": "Expected type String, found {_meta: {uid: \"developers\"}}.",
{
allDirectoryServices(
where: { category: "developers"}
) {
edges {
node {
name
city
region
country
category {
...on DirectoryTaxonomy {
_meta {
uid
}
name
}
}
}
}
}
}
This returns no results, obviously.
I asked this question on the Prismic Slack group too, and got the answer from them:
In order to query by a Content Relationship / Link field like this, you need to use the document ID.
where: { category: "WBsLfioAABNUo9Kk" }
Unfortunately it isn’t possible to query by the UID (or any other field).
I imagine they will be updating their documentation soonish, as this isn't covered by it.
Thanks to the Prismic guys!

Graphql - How to perform where clause

I am new to graphql and I am struggling with a query.
I want to return a user by their email address
I have a type defined call V1User and it has the following fields
id,
email,
password,
role
What needs to change in this query to return a user based on email?
query GetAllV1User {
viewer {
allV1Users{
edges {
node {
id
email
role
createdAt
modifiedAt
}
}
}
}
}
I tried this query
query getV1UserQuery($email: String!) {
getV1User(email: $email) {
id
email
}
}
With these params
{"email": "test#test.com"}
But get the following errors
{
"errors": [
{
"message": "Unknown argument \"email\" on field \"getV1User\" of type \"Query\".",
"locations": [
{
"line": 2,
"column": 13
}
],
"name": "GraphQLError"
},
{
"message": "Field \"getV1User\" argument \"id\" of type \"ID!\" is required but not provided.",
"locations": [
{
"line": 2,
"column": 3
}
],
"name": "GraphQLError"
}
]
}
My Schema is as follows
Name Type Constraints
id ID NonNull Unique
modifiedAt DateTime NonNull
createdAt DateTime NonNull
role String NonNull
password String NonNull
email String NonNull Unique Indexed
Thanks
Hi
This query solved my issue
query getUserForEmailAddressAndPassword($where: V1UserWhereArgs) {
viewer {
allV1Users(where: $where) {
edges {
node {
email
id
createdAt
password
modifiedAt
role
}
}
}
}
}
Along with these query variables
{"where": {"email": {"eq" : "test#test.com"}, "password": {"eq":"te2st"}}}
You can do so by using the where clause and comparison operators.
https://hasura.io/docs/latest/graphql/core/databases/postgres/queries/query-filters.html#the-where-argument
query {
authors (where: {articles: {rating: {_gt: 4}}}) {
id
name
articles (where: {rating: {_gt: 4}}) {
id
title
rating
}
}
}
I wouldn't recommend using the string "where" in your filter clause. Don't try to emulate SQL. What are you trying to filter using the where clause. If it's an email address then the query in your schema should contain user as the field and email as a parameter to that field.
So the first example that you sent is the right way to do it.
Also, avoid declaring queries using verbs like getUsers or getUser. The schema should just declare the query using nouns
Query
{
Users(email:String):[User!]
}
type User
{
id
email
createdAt
otherStuff
}

Resources