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
  • For a separate add use
  • Definition
  • Example

Was this helpful?

  1. API
  2. Relationship

@belongsTo

Belongs to (one or more)

For a separate add use

/* ... */
const {
  BelongsToDirective,
  BelongsToTypeDefs
} = require('apollo-server-adonis-directives-pack/src/directives/relationship/BelongsTo')

makeExecutableSchema({ 
  /* ... */ 
  typeDefs: [ BelongsToTypeDefs, /* ... */ ],
  schemaDirectives: { BelongsToDirective, /* ... */} 
})

Definition

BelongsToTypeDefs
directive @belongsTo(
  # By default, the model will be calculated from the name
  # of the returned "type" with the prefix 'App/Model/'
  model: String,
  # By default ownerColumn = 'id'
  ownerColumn: String,
  # By default, the column will be calculated from the name 
  # of the returned "type" with the postfix '_id'
  localColumn:String) on FIELD_DEFINITION

Example

type User {
    id: ID!
    name: String
    country_id: Int!
}

type Post {
    id: ID!
    title: String!
    text: String
    user_id: Int!
    user: User @belongsTo #localColumn: user_id
}

type Country {
    id: ID!
    name: String
    users: [User] @belondsTo(localColumn: "id", ownerColumn: "country_id")
}
Previous@hasMany

Last updated 4 years ago

Was this helpful?