Getting Started
Installation
Install and configure the package using the following command :
node ace add @foadonis/graphqlChoosing your Driver
Yoga Server
GraphQL Yoga is a modern, lightweight GraphQL server with excellent subscription support. It's fully open-source.
Choose Yoga if:
- You want a lightweight, modern server
- You need built-in subscription support
- You prefer a flexible plugin system
- You prefer fully open-source solutions
Apollo Server
Apollo Server is the industry-standard GraphQL server with extensive tooling and enterprise features. Some advanced features require Apollo's commercial offerings.
Choose Apollo if:
- You need enterprise features and Apollo Studio integration
- You're already using Apollo tools in your stack
- You want the most mature ecosystem
Getting Started
To explore the different capabilities of GraphQL we will create a sample GraphQL API for cooking recipes.
Let's start with the Recipe type, which is the foundation of our API.
Create our Recipe type
The Recipe
We will start with a basic Recipe model.
import { , } from '@adonisjs/lucid/orm'
import { } from 'luxon'
export default class extends {
@({ : true })
declare : string
@()
declare : string
@()
declare : string | null
@()
declare : string[]
}The ObjectType
We now need to create an object type from our model. It will then be used in our GraphQL schema.
import { , } from '@adonisjs/lucid/orm'
import { , , } from '@foadonis/graphql'
import { } from 'luxon'
@()
export default class extends {
@({ : true })
@(() => )
declare : string
@()
@()
declare : string
@()
@({ : true })
declare : string | null
@()
@(() => [])
declare : string[]
}For simplicity here we use directly our model in our schema, but you can create a type from any class.
Create the CRUD operations
After that we want to create typical crud operations. Also called queries and mutations. For this we have to create a Resolver (similar to a controller).
The Resolver
import from '#models/recipe'
import { , , , , , } from '@foadonis/graphql'
@()
export default class {
@(() => )
(@('id') : number) {
return .findOrFail()
}
@(() => [])
(@() { , }: ) {
return .query().paginate(, )
}
@(() => )
(@('newRecipeData') : ) {
return .create()
}
@(() => )
async (@('id') : number) {
const = await .find()
if (!) {
return false
}
await .delete()
return true
}
}The Inputs and Arguments
We are missing two important pieces, the inputs for the recipes query and the arguments for the addRecipe mutation.
Let's create them:
import { , , , } from '@foadonis/graphql'
@()
class {
@(() => )
: number = 0
@(() => )
: number = 0
}
@()
class {
@()
declare : string
@({ : true })
declare ?: string
@(() => [])
declare : string[]
}Access the Playground
Everything is now ready!
You can access the GraphQL playground at http://localhost:3000/graphql and start playing with your queries and mutations.
