How to handle where clause in GraphQL Schema - graphql

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.

Related

Directus - Retrieve relations with 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

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.

Insert more than one record using GraphQL Mutation

I would like to insert more than one record using GraphQL Mutation but it is giving error. here is the code which I have used to perform this.
input BusinessImageInput {
business_id: Int
image_url: String
}
mutation MyMutation($images: [BusinessImageInput!]) {
insert_business_images(objects: [$images]) {
affected_rows
}
}
And here is variable which i want to pass as paramter.
{"images": [
{
"business_id": 15,
"image_url": "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTVzlb1cEw8E0LeLJzk9c0OQV-N387Nt2Kn5w&usqp=CAU"
},
{
"business_id": 15,
"image_url": "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTVzlb1cEw8E0LeLJzk9c0OQV-N387Nt2Kn5w&usqp=CAU"
}
]
}
Here is the error
{
"errors": [
{
"extensions": {
"path": "$.query",
"code": "bad-request"
},
"message": "not a valid GraphQL query"
}
]
}
Please help me out.
There is one glaring issue in your code. This line
insert_business_images(objects: [$images]) {
should be
insert_business_images(objects: $images) {
Notice the removed square brackets.
If that does not help, then we'll need more information, such as:
what error do you get?
which implementation of GraphQL are you using both client-side and server-side?
what does the GraphQL code (and possibly resolvers) look like on the server? You have only given us the client-side of the equation.
It's as simple as
mutation MyMutation($images: [BusinessImageInput!]) {
insert_business_images(images: $images) {
or
mutation MyMutation($objects: [BusinessImageInput!]) {
insert_business_images(objects: $objects) {
depends on server insert_business_images mutation definition, the name of argument (images or objects ?) - use explorer ... and [as you can see above] usually input arg and variable are same-named, they only differs with $ prefix.
https://graphql.org/learn/queries/#variables
Also you must follow server input types.

How can I define type in gql query in Apollo Client?

My Apollo Server have following definition.
input MinMax{
min:Float
max:Float
}
input ScreenerInput{
fy:Int!
quarter:Int!
ltp:MinMax
eps:MinMax
pe:MinMax
netWorth:MinMax
paidUp:MinMax
reserve:MinMax
netProfit:MinMax
}
How can I define MinMax type in Apollo client to send variables for the MinMax type. Till now I was doing scalar types only so my query were simple.
My query type is screeenedCompanies(criteria:ScreenerInput!):[ScreenedCompanies]
if I query directly like below it works.
{
screeenedCompanies(criteria:{
fy:2075
quarter:2
eps:{
min:30
max:40
}
}){
sector
symbol
}
}
What I want is to query like this so that variables can be changed
query getScreenedCompanies($criteria:<What type should be here ?>){
{
screeenedCompanies(criteria:$criteria){
sector
symbol
}
}
}
After giving ScreenerInput as type my query looks like below
query getScreenedCompanies($criteria:ScreenerInput!){
screeenedCompanies(criteria:$criteria){
sector
symbol
}
}
my variables in apolloplayground look like below
{
"criteria": {
"fy": 2075
"quarter": 4
"ltp": {
"min": 345
"max": 400
}
}
}
I am receiving the following error
"error": {
"errors": [
{
"message": "Variable \"$criteria\" of required type \"ScreenerInput!\" was not provided."
It works the same way, as any scalar type in GQL. Just define them with the same input name as on the server(like MinMax or ScreenerInput in your case). You don't need to describe its shape on the client, server will validate it for you :)
And don't forget to add exclamation mark !, if parameter is required.

Is GitHub API returning an invalid result for the schema query?

https://facebook.github.io/graphql/draft/#sec-Schema-Introspection
type __Schema {
types: [__Type!]!
queryType: __Type!
mutationType: __Type
subscriptionType: __Type
directives: [__Directive!]!
}
type __Type {
kind: __TypeKind!
name: String
description: String
...
Information downloaded from https://developer.github.com/v4/guides/intro-to-graphql/#discovering-the-graphql-api (curl -H "Authorization: bearer token" https://api.github.com/graphql)
(beginning of the file
{
"data": {
"__schema": {
"queryType": {
"name": "Query"
},
"mutationType": {
"name": "Mutation"
},
"subscriptionType": null,
"types": [
{
"kind": "SCALAR",
"name": "Boolean",
...
Question:
I interpreted this so this GitHub schema result is invalid because queryType doesn't specify a kind which is nonNullable (kind: __TypeKind!)
Is this result violating the schema rules or am I missing something?
This response passes validation because a missing field is not the same thing as a field that returns null. A field will be missing from the response only if it wasn't requested in the first place.
If you go to GitHub's GraphQL Explorer, you can generate an introspection query yourself, request the kind field as part of the selection set of the queryType field and it will return the field with a non-null value.
{
__schema {
queryType {
kind
}
}
}
Response:
{
"data": {
"__schema": {
"queryType": {
"kind": "OBJECT"
}
}
}
}
Fetching the schema by making a GET request to some endpoint is convenient, but it's not the standard way to introspect the schema. Instead, you should make the request using whatever selection set is needed against the endpoint itself. The drawback of doing it this less conventional way is made apparent by this question. In this case, whatever introspection query GitHub is making for you under the hood is missing one or more fields that could otherwise be requested. Because you're not the one making the introspection query, you don't know what to expect in terms of the shape of the response.

Resources