Giter VIP home page Giter VIP logo

Comments (6)

chrissm79 avatar chrissm79 commented on July 19, 2024 1

I'll add this to the docs, but you can only have 1 resolver directive on a field (there are resolver directives that "resolve" a field and middleware directives that "manipulate" a field).

As for filtering/ordering, you can add scopes to the hasMany and pagination directives like so:

type User {
    posts(status: PostStatus): [Posts!]! @hasMany(scopes: ["myStatusScope"])
}

type Query {
    posts(order: PostOrder): [Post!]! @pagination(scopes: ["myModelScope"]
}

Note: You'll get passed the args array to your scope

from lighthouse.

4levels avatar 4levels commented on July 19, 2024 1

Ok, I think I found the issue:
as soon as I change the scope function name to something else than scopeOrderBy (and update schema.graphql accordingly), things started working. Maybe there's already a built-on scope since I even ran into an endless loop when trying things out?

Seems like the laravel documentation is also not clear on this...

Doesn't work:

public function scopeOrderBy($query, $args) {
  ..

Works:

public function scopeOrderAndFilter($query, $args) {
  ..

from lighthouse.

kikoseijo avatar kikoseijo commented on July 19, 2024

Excellent, was missing the scope.

extend type Query {
  posts: [Post!]! @paginate(type: "connection", model: "Post", scopes: ["filterAndOrder"])
}

Thanks again!

from lighthouse.

4levels avatar 4levels commented on July 19, 2024

@kikoseijo can you explain a little more how you managed to get sorting with scopes working?

I've been fruitless in trying to allow sorting to be passed as an argument by graphql.
I added the orderBy argument and scope option to the paginator directive, like so

images(orderBy: String): [Image!]! @paginate(type: "relay", model: "Image", scopes: ["orderBy"])

However, as soon as I try adding an orderBy clause to the query (see code below) it doesn't return any results anymore (which seem logical from the Laravel scopes intention to filter results, not to sort)

in app/Models/Image.php

    /**
     * @param $query \Illuminate\Database\Eloquent\Builder
     * @param $args
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOrderBy($query, $args) {
        $orderBy = $args['orderBy'] ?: 'createdAt_DESC';
        @list ($orderField, $orderDirection) = explode('_', $orderBy);
        $orderDirection = $orderDirection ?: 'DESC';
        return $query->orderBy(snake_case($orderField), strtolower($orderDirection));
    }

Thanks again! Maybe this question should also be listed in #106 ?

from lighthouse.

kikoseijo avatar kikoseijo commented on July 19, 2024

Reserved word?

from lighthouse.

morpheus7CS avatar morpheus7CS commented on July 19, 2024

@kikoseijo Yes, as you can do Model::orderBy('some_attribute') on all Eloquent models I think.

from lighthouse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.