Adonis GraphQL 🚀
Adonis GraphQL is a wrapper around apollo-server-adonis to provide a better experience writing Schema and Resolvers.
Setup
The package must be installed by using adonis
command.
> adonis install adonis-graphql
You can use directly npm
or yarn
but the instructions (instructions.js
and instructions.md
) will not be displayed and ran.
⚠️ This package requires
@adonisjs/bodyparser
to be installed.
Bind GraphQL Endpoint
You can bind the GraphQL endpoint directly from your routes.js
file.
In this example, we are using the /
route to handle all graphql query.
const Route = const GraphQLServer = Route Route // or add options (example)Route // or change options in file `config/graphql.js`
Create Schema
All schema are defined by default in the app/Schema
directory. You can change this by editing the configuration config/graphql.js
.
To define a schema, you must use the graphql
extension and syntax.
# app/Schema/Hello.graphqltype Query { hello: String}
Create Resolver
All schema are defined by default in the app/Resolvers
directory. You can change this by editing the configuration config/graphql.js
.
To define a resolver, you must return a JS Object.
// app/Resolvers/Hello.jsmoduleexports = Query: 'World'
Throw an error
GraphQL handles errors by a different way.
To provide a GraphQL Compliant error we recommend you to use the GraphQLError
class.
// app/Resolvers/Hello.js const GraphQLError = moduleexports = Query: { throw 'Error Message' ... }
If you are using the Adonis Validation Provider your code must look like the example bellow.
const validation = await if validation throw 'Validation Failed' validation