Graphql - How to perform where clause
Asked Answered
B

2

30

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": "[email protected]"}

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" : "[email protected]"}, "password": {"eq":"te2st"}}}
Bromoform answered 11/2, 2018 at 12:13 Comment(7)
To do this you have to modify your query to have some parameters. Then in a resolver you send the arguments you got with the request.Ransome
Just updated question with errors I am gettingBromoform
Could you provide your schema code for this query?Ransome
I am using scaphold.io to create the schema - I can add you to my account to see it?Bromoform
I have never used scaphold, but I'm sure you can view you schema code so it's possible to just post it here. Anyway, my first guess would be that you need to update getV1UserQuery query in your schemaRansome
@Ransome - just posted the schema thereBromoform
@Ransome for the sake of noobs such as myself, can you elaborate on resolvers? I'm jargon-deficient to this world.Hatley
C
15

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
     }
   }
 }
Chuck answered 6/5, 2021 at 7:30 Comment(0)
S
2

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
}
Sorrento answered 23/5, 2019 at 10:41 Comment(1)
" Don't try to emulate SQL" - SQL uses English, in a style with familiar grammar and sentence construction (for those who can read/write English), so you might as well say "do this in a way that is much harder for your team to grok and work with instead of the easier way". Concision and terseness are valuable so long as they dont remove meaning or make the idea harder for the listener to undertstand. I've had about a weeks worth of introductory fun with GraphQL and at every turn there is something non-intuitive, not documented, intentionally left ambiguous or simply omitted from the spec.Continuo

© 2022 - 2024 — McMap. All rights reserved.