Graphql to FQL v10 migration, how populate one-to-many relation field

Hello,

We are working on migrating from using the GraphQL api to FQL v10, and there are a few things I’m trying to figure out.

This is one of our GraphQL queries which works as expected:

gql`
  query findItemByID($id: ID!) {
    findItemByID(id: $id) {
      _id
      number
      name
      inventoryItems {
        data {
          _id
          quantity
          storageSpace {
            _id
            name
            displayLabel {
              _id
              barcode
            }
          }
        }
        before
        after
      }
    }
  }
`

There are a number of relationships in this structure:
one-to-many from one item to many inventoryItems
one-to-many from one storageSpace to many inventoryItems
one-to-one from one storageSpace to one displayLabel

I’ve tried running a few queries in the dashboard:

This query works fine, the item, storageSpace, and displayLabel fields get populated as expected.

inventoryItems.byId("331882561846379088"){
    id, 
    quantity, 
    item, 
    storageSpace {
        id, 
        displayLabel
    }
}

However, the following query returns null on the inventoryItems field.

items.byId("331803918482801233"){
    id,
    number,
    name,
    inventoryItems {
        id, 
        quantity
    },
}

So, how do I populate the field inventoryItems with the list of related objects as I was able to do in the GraphQL query?

Hi @Nobbe and welcome! :wave:

The GraphQL API compiles your GraphQL requests into a single FQL v4 request. First, it is good to understand how Fauna stores the relationships used with the GraphQL API. Our documentation goes into more details here: Learn how one-to-many, many-to-one, and many-to-many relationships are modeled in Fauna's GraphQL API. - Fauna Documentation

For a one-to-many relationship, for example, we see that the relationship is stored in the “many” side, and an index is used to list the many documents given the one. In your case, that means the relationship will be stored in the inventoryItem documents.

Now, since the existing indexes created for you when you uploaded your schema are all defined in FQL v4, you cannot use them directly with v10. I would recommend creating a new v10 index to use.

// FSL
collection inventoryItem {
  history_days 0

  index by_item {
    terms [.item]
  }
}

Now we can use the index in your v10 query. We will need to reuse the item to pass it into the index, so we can store it in a variable to use it later.

let item = items.byId("331803918482801233")! // use `!` to require it to be non-null
item {
  id,
  number,
  name,
  inventoryItems: items.by_item(item) {
    id, 
    quantity, 
    item, 
    storageSpace {
      id, 
      displayLabel
    }
  }
}

Sorry for the late response.

This makes sense. I managed to get it working, thank you.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.