question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question about querying a single resource with flask sqlalchemy example.

See original GitHub issue

I have followed the example at http://docs.graphene-python.org/projects/sqlalchemy/en/latest/tutorial/#testing-our-graphql-schema and have a question.

I can issue the following query to list all employees and their departments.

{
  allEmployees {
    edges {
      node {
        name
        department {
          name
        }
      }
    }
  }
}

I would like to be able to query a single employee name and their department name by employee id. I can query using the Node interface, but that doesn’t allow me to access Employee name field. Am I supposed to “cast” this to the specific Employee type to do that? What I would like is something like:

{
  employee(id: "someid") {
    name
    department {
      name
    }
}

Is this reasonable or am I “doing it wrong”? What is best practice for accessing a single employee using the Relay connections/nodes/edges paradigm?

Many thanks in advance!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
tlbjrcommented, Jul 6, 2019

You could do something like this.

find_employee = graphene.Field(lambda: Employee, name=graphene.String())

def resolve_find_employee(self, args, context, info):
    query = Employee.get_query(context)
    name = args.get('name')
    return query.filter(EmployeeModel.name == name).first()

Thanks for the pointers @navinesh. This worked great:

class Query(graphene.ObjectType):
    ...
    find_employee = graphene.Field(lambda: Employee, name=graphene.String())

    def resolve_find_employee(self, info, **kwargs):
        query = Employee.get_query(info)
        name = kwargs.get("name")
        return query.filter(EmployeeModel.name == name).first()
query FindEmployeeByName($name: String = "Tracy") {
    findEmployee(name: $name) {
        id
        name
        department {
            name
        }
    }
}
2reactions
navineshcommented, Nov 14, 2017

You could do something like this.

find_employee = graphene.Field(lambda: Employee, name=graphene.String())

def resolve_find_employee(self, args, context, info):
    query = Employee.get_query(context)
    name = args.get('name')
    return query.filter(EmployeeModel.name == name).first()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Flask-SQLAlchemy to Interact with Databases in a ...
It provides ways to interact with several database engines such as SQLite, MySQL, and PostgreSQL. It gives you access to the database's SQL ......
Read more >
How to query database by id using SqlAlchemy?
Query has a get function that supports querying by the primary key of the table, which I assume that id is. For example,...
Read more >
Top 31 Popular Python Flask Interview Questions With Answers
Answer: We can get the argument's value using the request object in Flask. An example is shown below. from flask import Flask from...
Read more >
Python REST APIs With Flask, Connexion, and SQLAlchemy
Note: If you haven't worked through part one of this tutorial series, then make sure to download the source code by clicking the...
Read more >
Object Relational Tutorial (1.x API)
The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes with database tables, and instances of those classes ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found