Comments (13)
It should work. Make sure you have a relation (function) named earlier_comments()
as well in the Model
from graphql-laravel.
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.
What exactly was the problem?
from graphql-laravel.
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.
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.
The function is a requirement and yes, you could put the query inside the function instead
from graphql-laravel.
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.
@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.
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.
Would it work with GraphQL types? I'm trying to do that but I can't seem to figure it out.
from graphql-laravel.
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.
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.
Alright. Thanks for the help man. Sorry for going overboard.
from graphql-laravel.
Related Issues (20)
- Tests are failing on CI HOT 3
- Cannot upgrade to laravel 10x because it cannot coexist with rebing dependency illuminate/contracts and illuminate/support
- Playground, are you thinking of implementing it?
- Unable to enable apq caching
- Is there documentation on returning objects with relation objects as a part of the type? HOT 4
- Pagination on Relation HOT 1
- Unexpected end of JSON input HOT 3
- queryContext in GraphQLController doesn't exist HOT 2
- L11: Unresolvable dependency resolving [Parameter #4 [ <required>; string $basePath ]] in class Illuminate\Foundation\Exceptions\Renderer\Renderer (500 Internal Server Error) HOT 1
- Improve error reporting when a type's name has illegal characters HOT 2
- Custom Query Fails to Load in Laravel 11 with graphql-laravel
- Add support for thecodingmachine/safe 2.0 HOT 2
- Nullable and Filled
- Resolver context is not updated once a user is logged in HOT 2
- repo description nit
- Pagination error HOT 2
- Integration tests for L9 are not working anymore
- useing $args in the resolve cloture of a type item
- Missing parent table in SelectFields for UnionType and InterfaceType HOT 3
- Using getRelations of SelectFields involves only selecting the requested fields for relationships HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-laravel.