GithubHelp home page GithubHelp logo

andruu / adonis-graphql Goto Github PK

View Code? Open in Web Editor NEW

This project forked from romainlanz/adonis-graphql

0.0 2.0 0.0 43 KB

GraphQL Provider for AdonisJs Framework

License: MIT License

JavaScript 97.63% HTML 2.37%

adonis-graphql's Introduction

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 = use('Route')
const GraphQLServer = use('Adonis/Addons/GraphQLServer')

const graphqlAdonisOptions = {
  debug: false
}

Route.post('/', function (context) {
  return GraphQLServer.handle(context, graphqlAdonisOptions)
})

Route.get('/graphiql', (context) => {
  return GraphQLServer.handleUI(context, { endpointURL: '/' })
})

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.graphql
type 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.js
module.exports = {
  Query: {
    hello: () => '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 = use('Adonis/Addons/GraphQLError')

module.exports = {
  Query: {
    hello: function () {
      throw new GraphQLError('Error Message', [...])
    }
  }
}

If you are using the Adonis Validation Provider your code must look like the example bellow.

const validation = await validateAll(data, rules)

if (validation.fails()) {
  throw new GraphQLError('Validation Failed', validation.messages())
}

adonis-graphql's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.