Apollo-server-adonis-directive-pack
  • Introduction
  • Usage
  • API
    • Query
      • @all
      • @find
    • Mutation
      • @create
      • @createMany
      • @update
      • @delete
    • Relationship
      • @hasOne
      • @hasMany
      • @belongsTo
Powered by GitBook
On this page
  • Exported Objects
  • Usage in schema

Was this helpful?

Usage

Exported Objects

The package provides an object with directive classes and an array with GraphQL schemes:

SchemaDirective - object with directive classes TypeDefsDirective - array with GraphQL schemes

Complete your schema configuration with these objects. This is an example of how I do it in my projects:

const { makeExecutableSchema } = require('graphql-tools')
const fs = require('fs')
const typeDefs = fs.readFileSync(__dirname + '/types.graphql', { encoding: 'utf8' })
const resolvers = require('./resolvers.js')
const { find, all } = require('./directives')

const { 
  TypeDefsDirective, 
  SchemaDirective 
} = require('apollo-server-adonis-directives-pack')

module.exports = makeExecutableSchema({ 
  typeDefs: [typeDefs, ...TypeDefsDirective],
  resolvers, 
  schemaDirectives: { ...SchemaDirective, find, all } 
})

The example above shows that the scheme is expanded using the package, and is not the main source

You can also destructure SchemaDirective and include directives separately.

Use of separate directives:

/* ... */
const { 
  TypeDefsDirective,
  SchemaDirective: { belongsTo } 
} = require('apollo-server-adonis-directives-pack')

makeExecutableSchema({ 
  /* ... */ 
  schemaDirectives: { belongsTo, /* ... */} 
})

Usage in schema

Use directives in the usual way in your circuits:

type User {
  id: ID!
  name: String
  posts: [Post] @hasMany
  token: Token @hasOne
}

type Post {
  id: ID!
  user_id: Int
  user: User @belongsTo
}

type Token {
  user_id: Int!
  token: String!
  user: User @belongsTo
}

For details on each directive, see the API description.

PreviousIntroductionNextQuery

Last updated 4 years ago

Was this helpful?