(Solved) Can't get Relationships to work with Apollo

Hi There,

I am trying to get started with GraphQL - I’ve got everything working except for retrieving the data via my Relationships (they work for writing, but not for reading – it creates the row with the correct event_type_id, but again the resulting event_type is null)

I really hope this is something stupid – I’ve spent all day getting this far…

Thanks for your time and attention

Al;

when I query an Event

query {
  fetchEvent(id: 5){
    id
    name
    capacity
    event_begin
    event_type {
      name
    }
  }
}

I only get back the Event, not the associated EventType

{
  "data": {
    "fetchEvent": {
      "id": 5,
      "name": "another similar conference",
      "capacity": -1,
      "event_begin": "2020-02-04T03:45:28.000Z",
      "event_type": null
    }
  }
}

Here is my GraphQL TypeDef

  type EventType {
    id: Int
    name: String
    description: String
    events: [Event]
  }

  type Event {
    id: Int
    name: String
    event_type_id: Int
    event_type : EventType
  }

and Resolver

    Query: {
      async fetchEvent(_, { id }) {
        const event = await Event.find(id)
        event.load('event_type')
        return event.toJSON()
      },
      async fetchEventType(_, { id }) {
        const eventType = await EventType.find(id)
        eventType.load('events')
        return eventType.toJSON()
      },
    },
    Mutation: {
      async createEvent(_, { name, event_type_id }) {
        const eventType = await EventType.find(event_type_id)
        let event = new Event()
        event.event_type_id = event_type_id
        event.name = name
        const saved = await event.save()
        event = await Event.find(event.id) // to get the new event
        event.load('event_type')
        return event.toJSON()
      },
    },

and I’ve turned on db_debug so I see this in my console

{
  method: 'first',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [ 5, 1 ],
  __knexQueryUid: '38c6acb7-8fe5-429e-95f6-be8bce73f6f7',
  sql: 'select * from `wpjt_app_events` where `id` = ? limit ?'
}
{
  method: 'first',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [ 2, 1 ],
  __knexQueryUid: '712a6abd-0deb-44af-9dc7-409395193c7e',
  sql: 'select * from `wpjt_app_event_types` where `id` = ? limit ?'
}

These are my models for Event & EventType

'use strict'
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')
const TablePrefixer = use('TablePrefixer/Provider');
// https://github.com/ammezie/adonis-graphql-server/blob/master/app/Models/User.js
class Event extends Model {
  static boot() {
    super.boot()
  }
  static get table() {
    return TablePrefixer.prefixTable('event')
  }
  event_type() {
    return this.belongsTo('App/Models/EventType', 'event_type_id', 'id')
  }
}
module.exports = Event
'use strict'
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')
const TablePrefixer = use('TablePrefixer/Provider');
// https://github.com/ammezie/adonis-graphql-server/blob/master/app/Models/User.js
class EventType extends Model {
  static boot() {
    super.boot()
  }
  static get table() {
    return TablePrefixer.prefixTable('event_type')
  }
  events() {
    return this.hasMany('App/Models/Event', 'id', 'event_type_id')
  }
}
module.exports = EventType

The event_type_id column is defined thusly:

this.table(TablePrefixer.prefixTable('event'), (table) => {
    table.integer('event_type_id').unsigned().references('id').inTable(TablePrefixer.prefixTable('event_type'))
})

TablePrefixer is something I wrote to deal with table prefixes : I've created a Provider to help with prefixing tables. It provides a workaround for the bug/problem with using the 'prefix' field in database configurations

1 Like

One must AWAIT the .load() !! ( Which is totally mentioned in the DOCS !! )

This fails to return the data

  async fetchEvent(_, { id }) {
    const event = await Event.find(id)
    event.load('event_type')
    return event.toJSON()
  },

while this succeeds

  async fetchEvent(_, { id }) {
    const event = await Event.find(id)
    await event.load('event_type')
    return event.toJSON()
  },
3 Likes