Directus - Retrieve relations with GraphQL - graphql

I try to retrieve the relations of the database of my directus app.
I use GraphQL with following endpoint https://myapp.directus.app/graphql.
Using a query for the relations according to the docs
query {
relations {
collection
field
}
}
leads to an validation error:
{
"errors": [
{
"message": "GraphQL validation error.",
"extensions": {
"code": "GRAPHQL_VALIDATION_EXCEPTION",
"graphqlErrors": [
{
"message": "Cannot query field "relations" on type "Query". Did you mean "locations"?",
"locations": [
{
"line": 2,
"column": 2
}
]
}
]
}
}
]
}
I use Postman so far and started with a query for the database entries
query {
boards {
id
columns {
id
name
}
}
}
which works as expected.
What I find interesting is, that Postman shows an mouseover text on the keyword "relations" of the 2nd query -->
"Cannot query field relations on type Query. Did you mean locations?"
But unfortunately I have no idea what to do with that message.
Any ideas what is wrong here?

Meanwhile I found the problem. The endpoint for queries on relations is https://myapp.directus.app/graphql/system
With that the query
query {
relations {
collection
field
}
}
is working

Related

How to handle where clause in GraphQL Schema

I am new to GraphQL and creating a API Server using Flask and GraphQL,
Facing some issues while handling the "where" clause in GraphQL Request.
The basic Request and Response is working fine . please find a short snippet of the Schema I have designed
type data {
edges:[data_edges]
}
type QueryCustom {
data: data
}
type Query {
query: QueryCustom
}
Below mentioned basic request (Without the where clause) is working fine with this schema
query {
query {
data {
edges { .... }
But Getting an error when I am executing the Request with the where clause
query dataClosingSoon($month: Long) {
query{
data(where: { LastModifiedDate: { CALENDAR_MONTH: { value: { eq: $month } } } } ) {
edges { ....... }
Following is the response I get:
{
"errors": [
{
"locations": [
{
"column": 40,
"line": 1
}
],
"message": "Unknown type 'Long'."
},
{
"locations": [
{
"column": 9,
"line": 5
}
],
"message": "Unknown argument 'where' on field 'QueryCustom.data'."
}
]
}
I need to understand how to handle the where condition.
GraphQL is not SQL. You cannot use SQL clauses such as WHERE, LIKE, etc. in a GraphQL query.
You need to look at the schema to check how can you filter your query. These filters are pre-defined in the schema. You cannot create custom filters (at least in a basic sense) for a GraphQL query.
Edit:
If you want to use the query you are trying to send, your schema should look like something this:
type data {
edges:[data_edges]
}
type Query {
data(where: Filter!): data
}
input type Filter {
lastModifiedDate: // the type of this field
// Rest of the input fields
}
Note that your first query and the second query are totally different. Your second query is clearly wrong due to two reasons:
The Query type does not have a field called data. It only has one field called query. (I wouldn't add a field named query under the Query type though).
Your data field does not have any inputs. But your document (the GraphQL request) clearly does.

Filter Query by any field, not just defined available filters

PS: my GraphQL skills are pretty basic so sorry for any incorrect use of words and terms
I want to achieve filtering on the code field highlighted below
(transactions --> edges --> node --> header --> transactionSource --> code = "something"
{
transactions(last: 10) {
edges {
node {
amount
periodId
header {
owner {
owner {
code
description
dbId
ownerDbId
ownerCode
}
}
transactionSource {
code
}
}
}
}
pageInfo {
hasNextPage
}
}
}
The client i'm working with have defined a list of filtering options, which I can successfully filter on, but when I try to filter on the code field I get the following result:
{
"errors": [
{
"message": "Argument 'filter' has invalid value. In field 'code': Unknown field.",
"locations": [
{
"line": 2,
"column": 26
}
],
"extensions": {
"code": "ARGUMENTS_OF_CORRECT_TYPE",
"codes": [
"ARGUMENTS_OF_CORRECT_TYPE"
],
"number": "5.6.1"
}
}
]
}
I presume this is because of me not knowing exactly how to set up the filter correctly.
Is there a way to filter on any field or do I need to talk to the guys maintaining the client and ask them nicely to make the code field available for filtering?
Thanks

How can i provide a a value for an argument in GraphQL?

I'm very new to GraphQL, and i'm trying to perform some example queries to this graph. In particular i'm trying the User schema.
According to that documentation, the schema is the following:
id: ID!
liquidityPositions: [LiquidityPosition!]
usdSwapped: BigDecimal!
And here is query i tried:
{
user (where: {id: "0x7c9C48b7cBEbBDA3268435F20c81f15A538C566C"}) {
id
liquidityPositions
usdSwapped
}
}
This query fails, i keep getting the following response:
{
"errors": [
{
"locations": [
{
"line": 0,
"column": 0
}
],
"message": "No value provided for required argument: `id`"
}
]
}
How can i provide the id field and where am i supposed to provide it? Thanks in advance!
You've got a couple of problems with that query. First, to get a user by id, remove the "where" and curly braces from your query. Secondly, the liquityPositions field needs a selection of subfields. Like so:
{
user (id: "0x7c9C48b7cBEbBDA3268435F20c81f15A538C566C") {
id
liquidityPositions {
id
}
usdSwapped
}
}
That website you linked to will show you errors with your query so you can interactively learn more about what is supported.
I would also suggest running through the introduction to GraphQL here: https://graphql.org/learn/ to get a handle on how things are done.

How to fix "Syntax Error: Expected Name, found String \"query\"" in GraphQL

I'm trying to test the GraphQL server I built, by sending GraphQL queries to the server using Postman.
It works when I'm using raw radio button, but when I'm trying to use GraphQL radio button, it returns "message": "Syntax Error: Expected Name, found String \"query\"".
I have tried to change the syntax: mainly add or delete curly braces but nothing happened.
The query I sent in raw mode (working):
{
person(id:"123456789") {
personal_info {
address
}
}
}
The query I sent in GraphQL mode:
QUERY:
query getPerson ($id: String){
person(id: $id){
personal_info {
address
}
}
}
GRAPHQL VARIABLES:
{
"id": "123456789"
}
I expect to get the data I asked for, but I get the error message:
{
"errors": [
{
"message": "Syntax Error: Expected Name, found String \"query\"",
"locations": [
{
"line": 1,
"column": 2
}
]
}
]
}
I had the same problem. During researching I have found the next answer on stackoverflow, thanks #gbenga_ps.
Resolved by adding the correct header to Postman request:
Body of request should be something like next:
{
courses {
title
}
}
If incorrect content-type set, error like next happened:
{
"errors": [
{
"message": "Syntax Error: Expected Name, found String \"query\"",
"locations": [
{
"line": 1,
"column": 2
}
]
}
]
}

How to use distinct in GraphQL query?

Here is query where I try to use distinct in graphQl query:
query{
contacts(take: 10, distinct: true) {
firstName
lastName
title
}
}
But I am getting error:
{
"errors": [
{
"message": "Unknown argument \"distinct\" on field \"contacts\" of type \"QuerySchema\".",
"locations": [
{
"line": 2,
"column": 21
}
]
}
]
}
GraphQL has no built-in sorting/filtering. It is up to the server to implement features like that, so if you're relying on a third party API and it doesn't support it then you will have to filter the response yourself.
You need to specify the column that GraphQL will use to check wether a value is distinct. In your case you can do something like:
query MyQuery {
contacts(distinct_on: firstName) {
firstName
lastName
title
}
}
Here is a example of distinct query.
query {
contacts {
distinct(field: title)
}
}
Result will be.
{
"data": {
"contacts": {
"distinct": [
"This is my test post",
"This is my test post1",
"This is my test post2"
]
}
}
}
This query binds all titles and deduplicates.

Resources