Giter VIP home page Giter VIP logo

Comments (13)

rebing avatar rebing commented on August 15, 2024 1

It should work. Make sure you have a relation (function) named earlier_comments() as well in the Model

from graphql-laravel.

rebing avatar rebing commented on August 15, 2024 1

Sorry for the late reply. The way you're selecting those tables creates one select, so it essentially needs just 1 Type. You should use ->with(['users']) instead, for example

from graphql-laravel.

rebing avatar rebing commented on August 15, 2024

What exactly was the problem?

from graphql-laravel.

niiapa avatar niiapa commented on August 15, 2024

I tried to have a type relationship query where I could query which the comments of a user before "today" and limit it to 3, so I was using many to many polymorphic relationships in Laravel. Tested the relationship before trying to limit it and the relationship was fine.

When I tried to add the type relationship query to limit the query, it doesn't work:

'earlier_comments' => [
     'type'          => Type::listOf(GraphQL::type('comment')),
    'description'   => 'A list of yesterday or earlier comments written by the user',
    'query'         => function(array $args, $query) {
        return $query->where('comments.created_at', '<', Carbon::today())->take(3);
    },
]

I have Carbon/Carbon being used don't worry. I tried it without the "take" and with different queries like comment.id being 1 or something, nothing seems to work.

from graphql-laravel.

niiapa avatar niiapa commented on August 15, 2024

I forgot to add that I set 'selectable' to false to test it out too. Didn't work either. Is the function a requirement in the relation, and if so, can't I just put the entire query there then?

from graphql-laravel.

rebing avatar rebing commented on August 15, 2024

The function is a requirement and yes, you could put the query inside the function instead

from graphql-laravel.

niiapa avatar niiapa commented on August 15, 2024

Thanks so much for your help. It works like you said. Thanks again for the package and the help dude. You rock!

from graphql-laravel.

niiapa avatar niiapa commented on August 15, 2024

@rebing Sorry to reopen but I've got a question. I can't seem to combo these Type Relationship Queries with Eager Loading. Is there a way to make it work? I used the example guide structure but my Debugbar still shows it runs two separate queries.

Edit: Unless of course that's how it works. Without the eager loading it runs a query per earlier sub query for earlier_comments but with it, it runs only once.

Double Edit: Is it possible to use raw queries to combine all queries into one?

from graphql-laravel.

rebing avatar rebing commented on August 15, 2024

Eager loading creates 1 query per relation. For example, if you have

query UserQuery {
  user {
    ...
    threads {
      ...
      comments {
        ...
      }
    }
  }
}

it will do 1 query to get the user, 1 for threads and then 1 for comments.

You can use DB::enableQueryLog() and dd(DB::getQueryLog()) to see exactly which queries are executed and the accompanied bindings.

You could use raw queries, but it wouldn't work well with Eloquent Models

from graphql-laravel.

niiapa avatar niiapa commented on August 15, 2024

Would it work with GraphQL types? I'm trying to do that but I can't seem to figure it out.

from graphql-laravel.

rebing avatar rebing commented on August 15, 2024

Whatever you SELECT with a raw query must have 'selectable' => false for the field. For example:

'raw_id' => [
  'type' => Type::nonNull(Type::int()),
  'selectable' => false,
]

and the query

User::where(...)->selectRaw('user.id AS raw_id')->get();

Did you mean something like this?

from graphql-laravel.

niiapa avatar niiapa commented on August 15, 2024

I'm doing something like that but with joins to other tables. So a relation for User like Post, I'm trying to write a raw query to get everything back in one query. So I'm doing like
DB::table('posts)->select('posts.*', 'users.*')->where('posts.id', $args['id'])->join('users', 'users.id', '=', 'posts.user_id')->get();
all inside the resolve function of the Query file.

from graphql-laravel.

niiapa avatar niiapa commented on August 15, 2024

Alright. Thanks for the help man. Sorry for going overboard.

from graphql-laravel.

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.