1.0.1 • Published 6 years ago

@gyu.nu/adonis-graphql v1.0.1

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
6 years ago

Gyunu Adonis GraphQL

This module's aim is to simplify the process of generating a GraphQL server on an AdonisJS installation.

It has peer dependencies for:
graph-ql
graphql-tools apollo-server-adonis

Install these with:

yarn add graph-ql graphql-tools apollo-server-adonis
npm i graph-ql graphql-tools apollo-server-adonis

Getting Stated

Installation

To get started, first install the package:
yarn add @gyu.nu/adonis-graphql
npm i @gyu.nu/adonis-graphql --save

Registration

Register the provider in start/app providers array, like all other providers with the following line:

'@gyu.nu/adonis-graphql/providers/GraphQLProvider',

Usage

Configuration

This step is important!

create a file in config called graphql.js and add the following to it:

'use strict'

const Env = use('Env')
const Helpers = use('Helpers')

module.exports = {
  path: 'app/Graphql',
  ext: '.gql',
  resolverExt: '.js',
  endpoints: {
    graphql: Env.get('GRAPHQL_ENDPOINT', '/graphql'),
    graphiql: Env.get('GRAPHQL_ENDPOINT', '/graphiql')
  }
}

Config Properties

path: property is the path where you keep all the .gql files (Types, Queries, Mutations, Input, Enums, Interfaces etc) which will get autoloaded by the provider

ext: The file extension used by your graphql specifications, if not using the .gql filetype

resolverExt: The extension of your resolver files, usually .js

endpoints: The endpoints to use for the graphql api, you can use env vars here like above. You can set them to be anything as long as they dont clash with other routes.


Registering Routes

Open up start.routes.js and require apollo-server-adonis like this:

const { graphqlAdonis, graphiqlAdonis } = require('apollo-server-adonis')

make sure const Route = use(Route) is above all the following.
use adonis-graphql by using it like so:

const graphql = use('GraphQL')

Then, define the handlers for the graphql and graphiql endpoints call the following methods:

graphql.handleGraphql(Route, graphqlAdonis)
graphql.handleGraphiql(Route, graphiqlAdonis)

and the routing for the graphql and graphiql endpoints, .gql files and resolvers will be automatically handled.

Thats it!

Run adonis and navigate to the graphiql endpoint set in config/graphql.js and you should see graphiql.

The handler functions expect the Route provider from adonis to set the routes, we pass it in so in the future we can use other routing if necessary.

The second argument is the graphql handler, in this case the graphqlAdonis package, but we can use any other by passing it in as long as it has the same interface.

This way, in the future this package can handle different frameworks or interfaces.


Extension

To pass in more default types and resolvers, the graphql instance returned by the provider exposes a use function.

Pass in an object with the keys: queries and/or resolvers, where those properties are an array of gql strings and normal js resolvers.

The provider will then load these along with the defaults. You can then move your queries and resolvers into other packages and just use them here.

example, using gyu.nu/adonis-graphql-typeext:

graphql.use(require('@gyu.nu/adonis-graphql-typeext'))

Use the use method before you call the routing, otherwise the schema will be generated without it.