Comments (4)
Thanks for the example project, will help out people figure out faster how to structure the project.
As for what I have discovered my side, the AutoLogin Middleware its not a good idea to test things, will always to be present no matter what middleware you use, and with 'web' middleware a real @auth does not work.
I'm working on a poject, its a website with an app, (Using this package for feeding the app) the website uses the normal routes and has no graphQL, the app without restriction and passport for authentication trough the mobile app-
I end up doing a second route, something like this:
$controller = config('lighthouse.controller');
Route::group(['middleware' => ['auth:api']], function () use ($controller) {
Route::post('graphql', ['as' => 'graphql.secured', 'uses' => '\\'.$controller]);
});
This is how it look like:
Hardest part was paginate the results with no connection and the belongsToMany relationship, you will laugh at me when I tell you that took me, like 4h, to figure out how to do the belongsToMany, was trying to use the @hasmany directive because the collection results looks the same, but could not work it out, till I removed the @hasmany directive and it just went and worked.
My conclusions: This is best Laravel + webonyx/graphql-php Wrapper on the Net.
With the example project, things should be faster, but I recommend to have this 2 common issues that people always need. Pagination without relationships, Belongs to many example. (More documentation, less hassle from new users)
The middlewares are always also confusing, and could be a plus to have something regarding this auth, middleware thing.
this is what I end up having, built, Its a Card Dealer solution, things are related to vehicles.
# type Car @model
media: [Media] @hasMany # this is a morphMany relationship.
extras: [CarOption!]! # this is a belongsToMany
make: CarMake @belongsTo
model: CarModel @belongsTo
body: CarBody @belongsTo
Paginated results without connection:
type CarConnection {
edges: [CarEdge]
pageInfo: PaginatorInfo
}
type CarEdge {
cursor: ID!
node: Car
}
type Query {
cars(
filter: String
after: ID
first: Int
count: Int
take: Int
limit: Int
page: Int
before: String
last: Int
): CarConnection!
}
above are toooo many fields, was testing things out.....
and the resolver for the query,
namespace App\GraphQL\Queries;
use App\Models\Car;
use App\ResolverLogTrait;
use Nuwave\Lighthouse\Support\Schema\GraphQLQuery;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Nuwave\Lighthouse\Schema\Types\PaginatorField;
use Nuwave\Lighthouse\Support\Traits\HandlesGlobalId;
class Cars extends GraphQLQuery
{
use HandlesGlobalId, ResolverLogTrait;
public function resolve()
{
$data = Car::orderBy('id', 'DESC')
->active('published')
->notActive('sold')
->with(['model', 'extras', 'make', 'media'])
->paginatorConnection($this->args);
$pageInfo = (new PaginatorField)->paginatorInfoResolver($data,$this->args,$this->context,$this->info);
$page = $data->currentPage();
$edges = $data->values()->map(function ($item, $x) use ($page) {
$cursor = ($x + 1) * $page;
$encodedCursor = $this->encodeGlobalId('Car', $cursor);
return ['cursor' => $encodedCursor, 'node' => $item];
});
return [
'pageInfo' => $pageInfo,
'edges' => $edges,
];
}
}
from lighthouse.
Just remember something else, I think its important as was rare,
scalar DateTime @scalar(class: "DateTime")
Had to extend yours because its looking for them in my configured folder, solution was:
namespace App\GraphQL\Scalars;
use Nuwave\Lighthouse\Schema\Types\Scalars\DateTime as LightDateTime;
class DateTime extends LightDateTime
{
}
This was a bit rare, in the sense of not being able to call directly yours from the [@]scalar directive as was allays starting inside my folder.
just a punctuation....
BTW: I'm done with the issue, close if you want...or leave open for others to provide feedback...
from lighthouse.
-well, I clicked close!
from lighthouse.
@kikoseijo Hey, I was able to get the belongsToMany
relationship to work for me using the @hasMany
directive. Here's an example project and you can use this query (once it's migrated and seeded):
{
cars {
make
year
extras(first: 5) {
pageInfo {
hasNextPage
}
edges {
cursor
node {
name
cost
}
}
}
}
}
I don't currently have a way to create a connection
or paginator
instance from the root query... didn't really think about that but it makes sense. I'll see if that's a directive I can create.
Going back to the belongsToMany
relationship, here is my schema:
type Car {
make: String!
year: Int!
# Set type pagination type to `cursor` and since the field doesn't match the name of the
# relationship, you can customize it with the `relation` argument
extras: [CarOption!]! @hasMany(type: "relay" relation: "options")
}
type CarOption {
name: String!
cost: Float!
}
type Query {
cars: [Car!]! @field(class: "App\\Http\\GraphQL\\Queries\\CarQuery" method: "resolve")
}
from lighthouse.
Related Issues (20)
- Clear Cache (private) HOT 1
- Segmentation fault when handling large payloads HOT 8
- Requesting local scopes can pass parameters HOT 1
- New directive that mirrors functionality of `whereHas` for relations (not `@whereHasConditions`) HOT 1
- `make setup` failed on macOS
- Access context in `FieldMiddleware` HOT 3
- CanArgs is defined twice in schema-directives.graphql
- `extend scalar X` directives are lost
- @canFind is missing "model" argument in graphql definition HOT 1
- Allow to customize the unique key for `PaginatedModelsLoader`
- artisan lighthouse:union stub appears to be incorrect HOT 1
- Problem with subscription middleware HOT 2
- An error occurs in the SubscriptionRegistry when sending a subscription event via Subscription::broadcast with Laravel Octane (Swoole) HOT 5
- Laravel v11 support dependancy missing
- Enum Support for Morph Types HOT 1
- Mutation transaction seem not work HOT 4
- Failed to find class App\GraphQl\Queries\TodoQuery in namespaces [] for directive @paginate
- @whereNull and @whereNotNull No directive found!
- GraphQL subscription with Laravel Reverb HOT 3
- input.id in @can(find: ) HOT 1
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 lighthouse.