Giter VIP home page Giter VIP logo

Comments (9)

rebing avatar rebing commented on June 30, 2024

What do you mean? Just return whatever you want in the Query/Mutation resolve() function. It can come from Eloquent or DB or it doesn't even have to interact with the database.

from graphql-laravel.

nyalex avatar nyalex commented on June 30, 2024

You're saying to do something like this?

class ClickQuery extends Query {

...

public function resolve($root, $args)
{
		return DB::table('click')
                 ->select(DB::raw('user_id, count(distinct click.id) as click_count'))
                 ->join('user', 'user.id', '=', 'click.user_id')
                 ->groupBy('user_id')
                 ->get();
}

If I do that, how do I relate it to other querys? Please look at this query.

{
  "query": "{user{id, email, first_name, clicks{click_count}}}"
}

How can it connect it to the UserQuery in this? There is no relation since it is outside of Eloquent.

from graphql-laravel.

nyalex avatar nyalex commented on June 30, 2024

I guess what I am asking is how do I setup relations in the Type file if I am not using Eloquent?

from graphql-laravel.

rebing avatar rebing commented on June 30, 2024

If you're not using Eloquent, then just omit the model property from the Type's $attributes. But this way you obviously cannot create relations to other tables

from graphql-laravel.

nyalex avatar nyalex commented on June 30, 2024

So there is no way to create the parent/child query in the POST without using Eloquent?

Example below. Is it possible to return something like this without Eloquent?

{
  "query": "{ users {id, email, first_name, profile {id, twitter_url, facebook_url, modified} } }"
}

from graphql-laravel.

rebing avatar rebing commented on June 30, 2024

It's possible, by just returning an array in the resolve method, for example:

return [
    'id' => 1,
    'email' => '[email protected]',
    'profile' => [
        'id' => 1,
        ...
    ],
    ...
];

But you also have to set 'selectable' => false for each field

from graphql-laravel.

nyalex avatar nyalex commented on June 30, 2024

Thanks. So just to confirm, if we were going to query the DB (without Eloquent) we would need to build that multi-dimensional array? Something like this (pseudo-ish code)?

$db_rows = DB::table('user')
     ->select(DB::raw('id, twitter_url'))
     ->join('profile', 'profile.id', '=', 'user.profile_id')
     ->get();

$db_rows = $db_rows->toArray();

$response = array();
$n = 0;

foreach ($db_rows as $row) {

     $response[$n]['id'] = $row['id']; // Parent element

     $response[$n]['profile']['twitter_url'] = $row['twitter_id']; // Child element

     $n++;

}

return $response;

I just want to confirm that this would be the best practice in this kind of scenario.

from graphql-laravel.

rebing avatar rebing commented on June 30, 2024

Yes, this would work (no need for even $db_rows->toArray() as you can iterate over a Collection with foreach)

from graphql-laravel.

nyalex avatar nyalex commented on June 30, 2024

Awesome - thanks so much for your help.

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.