trompamusic / ce-api

The Contributor Environment API for the TROMPA project
https://trompamusic.eu
Apache License 2.0
6 stars 1 forks source link

json-ld api doesn't return fields that are defined as interfaces #178

Closed alastair closed 3 years ago

alastair commented 3 years ago

when using the document api, only fields that are scalars or links to concrete types are returned. For example, a query

query {
  MusicComposition {
    identifier
    composer {
      ... on Person {
        name
      }
    }
    workExample {
      ... on MediaObject {
        identifier
      }
    }
  }
}

which returns

{
  "data": {
    "MusicComposition": [
      {
        "identifier": "479ee0b6-9c9d-4e0c-b8bd-9350b2825dc9",
        "composer": [
          {
            "name": "Jacob Handl"
          }
        ],
        "workExample": [
          {
            "identifier": "4fe5a535-385f-4328-9f6c-60bc557b4f16"
          },
          {
            "identifier": "02db0711-a356-4436-bee6-ef0bd7a8760e"
          },
          {
            "identifier": "0df44126-89cd-4b33-8605-b4ae03eb84e6"
          }
        ]
      }
    ]
  }
}

however, the json-ld response only includes:

{
  "dc:source": "https://cpdl.org/wiki/index.php/Ab_Oriente_venerunt_Magi_(Jacob_Handl)",
  "associatedArticle": [],
  "dc:type": null,
  "rdf:type": null,
  "dc:relation": null,
  "disambiguatingDescription": null,
  "character": [],
  "dc:modified": "2021-04-10T20:46:28.597000000Z",
  "text": null,
  "height": null,
  "regionsAllowed": [],
  "dc:identifier": "479ee0b6-9c9d-4e0c-b8bd-9350b2825dc9",
  "identifier": "479ee0b6-9c9d-4e0c-b8bd-9350b2825dc9",
  "image": null,
  "schema_publisher": [],
  "workExampleMediaObject": [     {
      "@id": "http://localhost:4000/4fe5a535-385f-4328-9f6c-60bc557b4f16"
    }]
}

It includes things like source which is just a String, and it also includes workExampleMediaObject which is our special field with the same relation name as workExample but with a type of MediaObject rather than ThingInterface.

The fields for workExample and composer don't appear in the returned json.

The issue seems to be that SchemaHelper.findInterfaceImplementingTypes https://github.com/trompamusic/ce-api/blob/c95fc49dc836569a3d6c8f3d4f55d7ee59150a01/src/helpers/QueryHelper.js#L96-L97 is returning null when trying to find schema.getImplementations of an interface name.

alastair commented 3 years ago

It looks like the interface a schema in graphql-tools changed since we upgraded it. This patch appears to work:

diff --git a/src/helpers/SchemaHelper.js b/src/helpers/SchemaHelper.js
index 2cfb675..8ae1638 100644
--- a/src/helpers/SchemaHelper.js
+++ b/src/helpers/SchemaHelper.js
@@ -31,11 +31,11 @@ class SchemaHelper {
    */
   findInterfaceImplementingTypes (interfaceName) {
     const implementations = this.findInterface(interfaceName)
-    if (!Array.isArray(implementations) || !implementations.length) {
+    if (!implementations.objects || !Array.isArray(implementations.objects) || !implementations.objects.length) {
       return null
     }

-    return implementations.map(implementation => { return implementation.toString() })
+    return implementations.objects.map(implementation => { return implementation.toString() })
   }

   /**
@@ -43,7 +43,7 @@ class SchemaHelper {
    * @returns {*|Array<GraphQLObjectType>}
    */
   findInterface (interfaceName) {
-    return this.schema.getImplementations(interfaceName)
+    return this.schema.getImplementations({name:interfaceName})
   }

   /**