Skip to content

Example application to learn GraphQL using Laravel, Livewire & Lighthouse

Notifications You must be signed in to change notification settings

davidyell/Learning-GraphQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Learning GraphQL with Laravel and Livewire

This is an example app to learn more about creating GraphQL API endpoints using Laravel.

tests quality

What is it?

A GraphQL API for Magic: The Gathering cards!

Requirements

  • PHP 8.4
  • Composer
  • Docker

Install

  • Clone the repo
  • composer install
  • ./vendor/bin/sail up -d
  • ./vendor/bin/sail artisan migrate:fresh --seed
  • ./vendor/bin/sail npm run build

Visit http://localhost

Fake data

There is a card seeder that will create 100 random cards using Faker data.

./vendor/bin/sail artisan db:seed --seeder FakeCardSeeder

Real data

If you want to use actual data, you will need to download two files:

Save them into the /app/database folder and run the seeder artisan db:seed StandardSeeder, which will populate the database with all the cards from the Standard format.

More info on WhatsInStandard.com

GraphQL

The project includes GraphIQL. You can make use of the client by installing Composer dev dependencies and then visiting http://localhost/graphiql.

Authentication

The /graphql endpoint is secured with a Laravel Sanctum token auth. You will need to login and generate a token, and send this token with your requests as the Authorization header.

Authorization: Bearer <your-token>

Example queries

Get all the cards, but only a few fields, paginated by 20 per page on page 4, and include the set the card belongs to.

{
    cards(first:20, page: 4) {
        data {
            id
            name
            types
            set {
                block
                mcmName
                code
                parentCode
            }
        }
        paginatorInfo {
            currentPage
            lastPage
        }
    }
}

Get just a single card

{
    card(id: 3) {
        id
        name
        flavorName
        flavorText
    }
}

Create a new user

mutation {
  createUser(name:"David", email:"[email protected]", password:"GiantBananaBoat") {
    name
    email
    created_at
    updated_at
  }
}

Get a list of the first 10 sets, and their parent sets.

{
  sets(first: 10, page: 1) {
    data {
      code
      name
      parentCode
      parent {
        code
        name
      }
    }
  }
}

Get a single set and cards in that set.

{
  set(code: "BIG") {
    name
    code
    cards {
      name
      manaCost
      type
    }
  }
}

Search for sets which contain kami anywhere in their name

{
  setNameContains(name: "kami") {
    name
    block
    code
  }
}

Find all the red dragons

{
  cardSearch(name: "dragon", colors: ["R"]) {
    id
    name
    type
    colors
  }
}

References

🎓 Learn to play Magic the Gathering! https://magic.wizards.com/en/intro

🙅 Not officially affiliated or endorsed by Wizards of the Coast or Magic the Gathering

🙇‍♂️ Thanks to MTGJSON for their free data https://mtgjson.com/downloads/all-files/

🎁 Thanks to Andrew Gioia for his excellent mana & keyrune packages https://github.com/andrewgioia/

About

Example application to learn GraphQL using Laravel, Livewire & Lighthouse

Topics

Resources

Stars

Watchers

Forks