Skip to content

Latest commit

 

History

History
116 lines (93 loc) · 5.6 KB

README.md

File metadata and controls

116 lines (93 loc) · 5.6 KB

Laragraph example application

A small "team blog" API — organizations, their members and the posts they publish — that exercises every Laragraph feature. Every feature is covered by a test in tests/Feature/GraphQL or tests/Octane, so the code here is guaranteed to work with the Laragraph version in this repository.

The step-by-step explanation of each feature lives in the developer guide; this README gets the app running and tells you where to look.

Setup

Requires PHP 8.3+ and Composer. The app installs Laragraph from the parent directory (a Composer path repository), so it always runs the code in this checkout.

cd example
composer install
cp .env.example .env
php artisan key:generate
php artisan jwt:secret
touch database/database.sqlite
php artisan migrate --seed
php artisan serve

Open http://localhost:8000/graphql/graphiql (served because APP_DEBUG=true).

Seeded accounts (password password):

Email Role
admin@example.com Admin of "Acme Corporation"
member@example.com Member of "Acme Corporation"

Try it

Log in and keep the token:

mutation {
  login(email: "member@example.com", password: "password") { token user { name role } }
}

In GraphiQL, add the header {"Authorization": "Bearer <token>"}, then:

{
  me { name role organization { name memberCount } }
  posts(first: 5) {
    edges { cursor node { title status publishedAt author { name } } }
    pageInfo { hasNextPage endCursor total }
  }
  search(term: "Acme") {
    __typename
    ... on Post { title }
    ... on Organization { name }
  }
}
mutation {
  createPost(input: { title: "Hello", body: "My first post from GraphiQL.", publish: true }) {
    id status publishedAt
  }
}

With curl:

curl -s localhost:8000/graphql -H 'Content-Type: application/json' \
  -d '{"query":"{ organizations(first: 3) { edges { node { name } } } }"}'

Admins can also query the separate admin schema:

curl -s localhost:8000/graphql/admin -H 'Content-Type: application/json' \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -d '{"query":"{ stats { members publishedPosts draftPosts } }"}'

Where each feature lives

Feature Code Test
Object types, per-field resolvers Types/UserType.php, PostType.php TypesTest
Interface / union / input types NodeType, SearchResultType, PostInputType TypesTest
Native PHP enums app/Enums, registered in config/laragraph.php TypesTest
Scalars: DateTime, JSON, Upload publishedAt, Organization.settings, UploadAvatarMutation TypesTest, FileUploadTest
Field arguments & deprecation Post.excerpt(length:), Post.summary, CurrentUserQuery TypesTest, AuthenticationTest
Auto-discovery everything in app/GraphQL/{Types,Queries,Mutations,Subscriptions} all
Validation rules RegisterMutation, CreatePostMutation, SearchQuery ValidationTest
authorize() / policies / policy() shortcut / guards UserQuery, UsersQuery, LogoutMutation, app/Policies AuthorizationTest
Field middleware (rate limiting) LoginMutation AuthenticationTest
N+1-safe relations & a custom DataLoader batchRelation() in the types, MemberCountLoader BatchingNPlusOneTest
Relay cursor pagination PostsQuery, UsersQuery PaginationTest
Subscriptions (queued fan-out, private channels, unsubscribe) PostPublishedSubscription, PublishPostMutation SubscriptionsTest
Multiple schemas + per-schema middleware app/GraphQL/Admin, schemas.admin in the config SecurityTest
Security limits, custom validation rule security config, MaxRootFieldsRule SecurityTest
GraphQL over HTTP, batching, APQ config: batching, persisted_queries HttpProtocolTest
Response cache + invalidation cache.response, FlushResponseCacheAfterMutations CachingAndObservabilityTest
Events, extensions, tracing LogSlowGraphQLOperations, ApiVersionExtension, AppServiceProvider CachingAndObservabilityTest
Deployment commands laragraph:validate, laragraph:cache, laragraph:schema:export ToolingTest
Laravel Octane (schema compiled once per worker, per-request isolation) automatic (laragraph.octane.warm) tests/Octane/OctaneTest

Tests

php artisan test

The tests talk to the API over HTTP with real JWTs, exactly as a client would.