question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GraphQL addReference on Object Array

See original GitHub issue

Summary

A way to have addReference traverse down into non root Types. collection.addReference(fieldName, typeName) and the source-filesystem options only apply to a root Type.

Basic example

My Input File:

field1: 'data'
field2: ['data1', 'data2']
field3:
- sub_field1: 'data'
  sub_field2: ['data1', 'data2']

This already works:

refs: {
  field1: 'typeName',
  field2: 'typeName'
}

This would be nice:

refs: {
  field3: {
    sub_field1: 'typeName'
    sub_field2: 'typeName'
}

Motivation

References are already very powerful, but they are limited by only applying at the Root level.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
belvederefcommented, Mar 26, 2020

I was experiencing the same problem @davidleininger was, and I solved it by declaring my own schema. Example:

 api.loadSource(({ addSchemaTypes }) => {
    addSchemaTypes(`
      type Comment implements Node {
        article: Article @reference(by: "id")
      }
    `);
  });

It was not working before specifying @reference(by: "id").

Just to be explicit, your Comment type needs to have a field article with a value that matches, in this case, the id field on the type Article.

From Comment I am now able to find the matching Article just using the field article. Instead, to find comments from an article, I used:

allArticle {
    edges {
      node {
        belongsTo {
          edges {
            node {
            	...on Comment {
                id
                content
              }
            }
          }
        }
      }
    }
  }

A bit convoluted but I could not find another way.

1reaction
davidleiningercommented, Feb 27, 2020

@hjvedvik I’m having an issue where the references aren’t getting made. I’ve added the following code to link one collection to another. I haven’t had an issue doing this in the past, but at the moment, GraphQL is just returning null for every reference.

Looking at the schema and the docs, in the playground, everything seems to be wired up correctly, but for whatever reason, everything comes back null.

api.loadSource(({ getCollection }) => {
    const restaurants = getCollection('Restaurants')
    restaurants.addReference('foodType', 'Types')
 })

In this case, it will return:

{
"id": "recwcDQAZGficxf0W",
"name": "O'Fallon Brewery",
"foodType": null
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to push an object into an array with GraphQL?
I would like to change the way my resolver is creating the payment cards in my DB. So now I create the wallet...
Read more >
Apollo Federation Transform – GraphQL Mesh
GraphQL Mesh Documentation. ... Add Reference Resolver as a Code File ... extends (type: Boolean ); fields (type: Array of Object , required):....
Read more >
Develop a GraphQL-Powered API With Symfony - Twilio
In this article, you will learn how to develop a GraphQL-powered API for an online book store with Symfony.
Read more >
Data Store API - Gridsome
#Data Store API. The Data Store API lets you insert your own data into the GraphQL data layer. You will then be able...
Read more >
GraphQL List - How to use arrays in GraphQL schema ...
It is often common practice in REST APIs to return a JSON response with an array of objects. In GraphQL we would like...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found