matfyz-sk / courses-backend

0 stars 1 forks source link

[UGQL] Inconsistent results with a query under certain conditions #39

Open ZubatyNos opened 10 months ago

ZubatyNos commented 10 months ago

This query should return a Folder with the folderContent subquery matching such entities whose value isDeleted is set to false. At least I presume.

query {
  courses_Folder(_id: ["http://www.courses.matfyz.sk/data/folder/sn8yA"]) {
      _id
      _type
      courses_name
      courses_folderContent {
          courses_isDeleted(equals: [false])
      }
  }
}

If there are no such subquery entities for folderContent, we get a correct result:

{
  "extensions": {},
  "data": {
    "Folder": [
      {
        "folderContent": [],
        "_type": "http://www.courses.matfyz.sk/ontology#Folder",
        "name": "Home",
        "_id": "http://www.courses.matfyz.sk/data/folder/sn8yA"
      }
    ],
    "@context": {
      "isDeleted": "http://www.courses.matfyz.sk/ontology#isDeleted",
      "folderContent": "http://www.courses.matfyz.sk/ontology#folderContent",
      "_type": "http://hypergraphql.org/query/_type",
      "name": "http://www.courses.matfyz.sk/ontology#name",
      "_id": "http://hypergraphql.org/query/_id",
      "Folder": "http://www.courses.matfyz.sk/ontology#Folder"
    }
  },
  "errors": []
}

But sometimes, we don't even get the main entity:

{
  "extensions": {},
  "data": {
    "@context": {
      "isDeleted": "http://www.courses.matfyz.sk/ontology#isDeleted",
      "folderContent": "http://www.courses.matfyz.sk/ontology#folderContent",
      "_type": "http://hypergraphql.org/query/_type",
      "name": "http://www.courses.matfyz.sk/ontology#name",
      "_id": "http://hypergraphql.org/query/_id",
      "Folder": "http://www.courses.matfyz.sk/ontology#Folder"
    }
  },
  "errors": []
}

This only happens if there truly no matching entities, otherwise the query works fine

The backend seems to generate SPARQL where the clauses can be ordered differently.

prefix courses: <http://www.courses.matfyz.sk/ontology#>
SELECT * WHERE {
  GRAPH <http://www.courses.matfyz.sk/> {
    VALUES ?x_1 {
      <http://www.courses.matfyz.sk/data/folder/sn8yA>
    }
    { SELECT ?x_1 WHERE { ?x_1 rdf:type courses:Folder. } }
    OPTIONAL {
      ?x_1 courses:folderContent ?x_1_1.
      ?x_1_1 rdf:type ?x_1_1_t.
      OPTIONAL { ?x_1_1 courses:isDeleted ?x_1_1_1. }
    }
    OPTIONAL { ?x_1 courses:name ?x_1_2. }
  }
  FILTER(?x_1_1_1 IN("false"^^xsd:boolean))
}
prefix courses: <http://www.courses.matfyz.sk/ontology#>
SELECT * WHERE {
  GRAPH <http://www.courses.matfyz.sk/> {
    VALUES ?x_1 {
      <http://www.courses.matfyz.sk/data/folder/sn8yA>
    }
    { SELECT ?x_1 WHERE { ?x_1 rdf:type courses:Folder. } }
    OPTIONAL { ?x_1 courses:name ?x_1_1. }
    OPTIONAL {
      ?x_1 courses:folderContent ?x_1_2.
      ?x_1_2 rdf:type ?x_1_2_t.
      OPTIONAL { ?x_1_2 courses:isDeleted ?x_1_2_1. }
    }
  }
  FILTER(?x_1_2_1 IN("false"^^xsd:boolean))
}

The clauses for fields are ordered differently which seems to be the case for other queries also, but here the entity resolution can be different for some reason.

OPTIONAL { ?x_1 courses:name ?x_1_1. }

OPTIONAL { ?x_1 courses:name ?x_1_2. }
crnkjck commented 9 months ago

@ZubatyNos, this needs clarification.

What do you mean by:

This only happens if there truly no matching entities, otherwise the query works fine

Which entities are you referring to? The folder, or its content?

What does “truly” mean here? There is no folder at all? No content in the folder at all, deleted or not deleted? No folders whatsoever?

Also, is there a pattern to when the two different SPARQL queried are generated? Which one produces which result?

ZubatyNos commented 9 months ago

@crnkjck sorry for the inaccuracies. So I'm referring to the content of the folder and also hwo the subquery filter courses_isDeleted(equals: [false]) comes into play. So if the main folder has no folderContent entities that are also set to isDeleted: false then I should get an empty array from this subquery. Which I can get, but some of the times I don't even get the main Folder entity.

I can't tell what the pattern for different SPARQL is. The same graphql being queried repeatedly produces this behavior. Seems random to me.