Giter VIP home page Giter VIP logo

Comments (4)

kikoseijo avatar kikoseijo commented on July 19, 2024

@chrissm79,

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:

screen shot 2018-03-23 at 09 08 42

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.

kikoseijo avatar kikoseijo commented on July 19, 2024

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.

kikoseijo avatar kikoseijo commented on July 19, 2024

-well, I clicked close!

from lighthouse.

chrissm79 avatar chrissm79 commented on July 19, 2024

@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)

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.