Integration with AdonisJS
AdonisJS is a TypeScript-first web framework for Node.js. The @foadonis/graphql package ships a GraphQL Yoga driver, so you can build code-first GraphQL APIs on top of the AdonisJS ecosystem.
AdonisJS is a TypeScript-first web framework for Node.js, shipping with a batteries-included ecosystem: an ORM (Lucid), authentication, authorization, validation and an IoC container.
@foadonis/graphql is a community package that brings
GraphQL to AdonisJS. It is code-first (built on top of TypeGraphQL, so
your schema is derived from decorated classes) and supports GraphQL Yoga as a server driver.
Installation
The command prompts you for a driver, where you should pick Yoga (graphql-yoga). It then
installs the required dependencies (graphql, graphql-yoga, graphql-scalars,
@graphql-yoga/subscription and @graphql-yoga/plugin-disable-introspection), registers the
provider, and generates a config/graphql.ts file along with a demo resolver.
Configuration
The Yoga driver is configured with drivers.yoga() in config/graphql.ts. It accepts every option
of createYoga, including plugins. The only exception is
schema, which is built from your resolvers.
Building the schema
Types and resolvers are plain classes decorated with the decorators re-exported from
@foadonis/graphql. Any class can become an @ObjectType, including a Lucid model.
Resolvers are autoloaded from app/graphql/resolvers (with hot module reloading in development), so
there is nothing to register manually.
Start your app with node ace serve and GraphiQL is served at
http://localhost:3000/graphql.
Context
The Yoga context is the AdonisJS
HttpContext, which gives resolvers
access to the request, the authenticated user and Bouncer.
Subscriptions
Subscriptions are powered by @graphql-yoga/subscription. Configure
a PubSub driver and a transport in config/graphql.ts:
Then declare a @Subscription resolver and publish events through the pubSub service:
File uploads
GraphQL Yoga implements the GraphQL multipart request specification
out of the box, but the AdonisJS bodyparser must not consume the multipart stream first. Add your
GraphQL endpoint to the processManually list:
Resolvers then receive a WHATWG File
instance.
Further reading
The @foadonis/graphql documentation covers everything
else, including
authorization with Bouncer and @adonisjs/auth,
middlewares,
validation and
custom decorators.