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.

how to Join with get_query

See original GitHub issue

Just trying to filter on a field from a foreign table.

This doesn’t seem to work

def resolve_articles(self, args, context, info):
        query = Article.get_query(context)
        authorName = args.get('authorName')
        return query.join(Author).filter(Author.name == authorName)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

6reactions
earlgreyzcommented, Nov 22, 2018

Hey sorry for delay, but it’s been a while since I used sqlalchemy so needed a quick refresh. You don’t need to subclass a connection field instead use a standard one with a custom resolver.

class Author(SQLAlchemyObjectType):
    class Meta:
        model = models.Author
        interfaces = [relay.Node]


class Article(SQLAlchemyObjectType):
    class Meta:
        model = models.Article
        interfaces = [relay.Node]


class Query(graphene.ObjectType):
    all_articles = SQLAlchemyConnectionField(Article, author_name=graphene.String())

    def resolve_all_articles(self, info, author_name):
        query = Article.get_query(info)
        return query.join(models.Author).filter(models.Author.name == author_name).all()

You can file complete files I’ve used to test here: https://gist.github.com/earlgreyz/bcbb9dc1b1e0618fe96125cd57b563a0

0reactions
earlgreyzcommented, Nov 23, 2018

@zhanibekshynarbek the problem is that when using add_columns you get a tuple with ArticleModel object and an author_name string. I came up with a rather dirty solution but you may be able to make something nice out of this:

class Article(SQLAlchemyObjectType):
    author_name = graphene.String()

    class Meta:
        model = models.Article
        interfaces = [relay.Node]


class Query(graphene.ObjectType):
    all_articles = SQLAlchemyConnectionField(Article)

    def resolve_all_articles(self, info):
        query = Article.get_query(info)
        query = query.join(models.Author).add_columns(
            models.Author.name.label('author_name'))

        def author_name_modifier(article, author_name):
            article.author_name = author_name
            return article

        return [author_name_modifier(a, n) for (a, n) in query.all()]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Joins | Dynamic Queries | Drupal Wiki guide on Drupal.org
Joins. To join against another table, use the join(), innerJoin(), leftJoin(), or addJoin() methods, like so:
Read more >
Doctrine query builder using inner join with conditions
I'm going to answer my own question. innerJoin should use the keyword "WITH" instead of "ON" (Doctrine's documentation [13.2.6.
Read more >
8 - entityQuery to get Nodes referenced by other nodes like an ...
Playing around with the raw SQL, what I basically need to do is an INNER JOIN or LEFT JOIN with the field field_detail_page_article_ref...
Read more >
Select using Query Builder - typeorm - GitBook
Each select can have its own alias, you can select from multiple tables each with its own alias, you can join multiple tables...
Read more >
Selecting data using JDatabase - Joomla! Documentation
$query = $db->getQuery(true); // Select all records from the user profile ... Using the JDatabaseQuery's join methods, we can select records ...
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