I'm using the following query to retrieve all cast members for "Star Wars - The Force Awakens" using its IMDb identifier (tt2488496).
The problem is that the results are not sorted accordingly, for example, I would like to see some main characters like John Boyega, Adam Driver, Daisy Ridley, etc. appear on the top most section of the result, but the results just seem random.
For example, IMDb sorts by top-billed cast, and I don't know if wikidata has that kind of info, but if we look into the wikidata page for the Star Wars - The Force Awakens title (https://www.wikidata.org/wiki/Q6074), in the cast member section, you will see that they are sorted properly.
Is there any way to achieve the same sorting that wikidata has on their title page?
SPARQL query (link):
SELECT ?titleLabel ?castLabel ?castImdb WHERE {
VALUES ?imdbIds { "tt2488496" }
?title wdt:P345 ?imdbIds .
?cast wdt:P345 ?castImdb .
?title wdt:P161 ?cast .
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
}
}
Related
So I'm doing some tests with GraphQL, and I'm failing in doing something that I believe is fairly simple.
When going to the GraphQL demo site (https://graphql.org/swapi-graphql) I'm presented with a default query which goes like this:
{
allFilms {
films {
title,
director,
releaseDate
}
}
}
This works as expected and returns a list of films.
Now - I would like to modify this query to return only the films where the director is George Lucas, and for the life of me - I can't figure out how to do that.
I've tried using the where and filter expressions, and also change the second line to films: (director: "George Lucas") but keep getting error messages.
What's the correct syntax for doing that?
Thanks!
If you check the docs of the provided GraphQL schema, you'll see that this is not possible. Following is the definition of the allFilms field:
allFilms(
after: String
first: Int
before: String
last: Int
): FilmsConnection
As per the doc, it has 4 input arguments, which are after, first, before, and last. There is no way to filter this out using the director's name.
GraphQL is not SQL. You cannot use expressions like WHERE or FILTER in GraphQL. The schema is already defined and the filters are pre-defined too. If the schema does not allow you to filter values using a certain field, you just can't do it.
You can to see the graphql schema here https://github.com/graphql/swapi-graphql/blob/master/schema.graphql
The allFilms query does not contain a filter for the field director. Also i can't find other query with this filter.
Most likely you need to write a filter on the result of the query.
in my app there are many entities which get exposed by GraphQL. All that entities get Resolvers and those have many methods (I think they are called "fields" in GraphQl). Since there is only one Query type allowed, I get an "endless" list of fields which belong to many different contexts, i.E.:
query {
newsRss (...)
newsCurrent (...)
userById(...)
weatherCurrent (...)
weatherForecast(...)
# ... many more
}
As you can see, there are still 3 different contexts here: news, users and weather. Now I can go on and prefix all fields ([contextName]FieldName), as I did in the example, but the list gets longer and longer.
Is there a way to "group" some of them together, if they relate to the same context? Like so, in case of the weather context:
query {
weather {
current(...)
forecast(...)
}
}
Thanks in advance!
If you want to group them together , you need to have a type which contain all fields under the same context . Take weather as an example , you need to have a type which contain currentWeather and forecastWeather field. Does this concept make sense to your application such that you can name it easily and users will not feel strange about it ? If yes , you can change the schema to achieve your purpose.
On the other hand, if all fields of the same context actually return the same type but they just filtering different things, you can consider to define arguments in the root query field to specify the condition that you want to filter , something like :
query {
weather(type:CURRENT){}
}
and
query {
weather(type:FORECAST){}
}
to query the current weather and forecast weather respectively.
So it is a question about how you design the schema.
I'm new to graphQL and Hasura. I'm trying(in Hasura) to let me users provide custom aggregation (ideally in the form of a normal graphQL query) and have then each item the results compared against the aggreation.
Here's a example. Assume I have this schema:
USERTABLE:
userID
Name
Age
City
Country
Gender
HairColor
INCOMETABLE:
userID
Income
I created a relationship in hasura and I can query the data but my users want to do custom scoring of users' income level. For example, one user may want to query the data broken down by country and gender.
For the first example the result maybe:
{Country : Canada
{ gender : female
{ userID: 1,
Name: Nancy Smith,..
#data below is on aggregated results
rank: 1
%fromAverage: 35%
}...
Where I'm struggling is the data showing the users info relative to the aggregated data.
for Rank, I get the order by sorting but I'm not sure how to display the relative ranking and for the %fromAverage, I'm not sure how to do it at all.
Is there a way to do this in Hasura? I suspected that actions might be able to do this but I'm not sure.
You can use track a Postgres view. Your view would have as many fields as you'd like calculated in SQL and tracked as a separate "table" on your graphql api.
I am giving examples below based on a simplification where you have just table called contacts with just a single field called: id which is an auto-integer. I am just adding the id of the current contact to the avg(id) (a useless endeavor to be sure; just to illustrate...). Obviously you can customize the logic to your liking.
A simple implementation of a view would look like this (make sure to hit 'track this' in hasura:
CREATE OR REPLACE VIEW contact_with_custom AS
SELECT id, (SELECT AVG(ID) FROM contacts) + id as custom FROM contacts;
See Extend with views
Another option is to use a computed field. This is just a postgres function that takes a row as an argument and returns some data and it just adds a new field to your existing 'table' in the Graphql API that is the return value of said function. (you don't 'track this' function; once created in the SQL section of Hasura, you add it as a 'computed field' under 'Modify' for the relevant table) Important to note that this option does not allow you to filter by this computed function, whereas in a view, all fields are filterable.
In the same schema mentioned above, a function for a computed field would look like this:
CREATE OR REPLACE FUNCTION custom(contact contacts)
RETURNS Numeric AS $$
SELECT (SELECT AVG(ID) from contacts ) + contact.id
$$ LANGUAGE sql STABLE;
Then you select this function for your computed field, naming it whatever you'd like...
See Computed fields
I am having problems with a live.dbpedia SPARQL request, for it returns some entries twice (once as an utf8 URI, once as a non-utf8 URI : Here are the results.
Is it something that needs to be fixed inside of dbpedia (where should it be reported)?
Is there a way to keep only one version of these duplicated urls? (I do not want to ignore a non-utf8 URI if there is no utf8 counterpart)
P.S.: The actual request
select distinct ?name where {
?name <http://purl.org/dc/terms/subject><http://dbpedia.org/resource/Category:Individual_graphs>.
} ORDER BY desc(?name) LIMIT 2
Even though there are multiple URIs that can identify the article, they all have the same article title, so you can extract the title (it's the value of the rdfs:label property), group by that, and then sample the URIs. Doing that, along with using the built-in DBpedia namespaces, I end up with this query:
select distinct (sample(?name_) as ?name) where {
?name_ dcterms:subject category:Individual_graphs ;
rdfs:label ?label
}
group by ?label
order by desc(?name)
SPARQL results
I am trying to integrate SPARQL queries into Prolog program (I am using SWI-Prolog)
To test it I am doing the following operations:
Into SWI-Prolog shell I execute this command: use_module(library(semweb/sparql_client)). that load the sparql client library
Then, in the SWI-Prolog shell, I execute the following SPARQL query:
?- sparql_query('select count(*) where { ?person a http://dbpedia.org/ontology/Person .?person http://it.dbpedia.org/property/nome ?name.filter regex(?name,"Leonardo"). }', Row, [ host('dbpedia.org'), path('/sparql/')]).
Row = row(literal(type('http://www.w3.org/2001/XMLSchema#integer', '0'))).
I don't know well SPARQL but I think that this is pretty simple and that work in this way:
This query ask about the instances number of objects that are considered personal names on a RDF ontology called dbpedia, passing the input parameter "Leonardo".
As you can see the problem seems that don't find any instance of this type (I have tried also with others personal name)
Why? What are am I missing?
Your query does not return what you want it to return. You can use SPARQL interactively with the DBpedia SPARQL endpoint. I suggest that you use that to debug your query and make sure it returns what you would like first, and then copy it into your Prolog program. You can build it up in pieces to make sure it's giving suitable results. For instance, start with something like the following:
select * where {
?person a <http://dbpedia.org/ontology/Person> .
}
LIMIT 10
SPARQL results
Then expand it piece by piece:
select * where {
?person a <http://dbpedia.org/ontology/Person> .
?person <http://it.dbpedia.org/property/nome> ?name .
}
LIMIT 10
SPARQL results
This query doesn't return anything, so it would be best to debug it before continuing on the the FILTER or COUNT.
After playing with the endpoint for a while, it seems that using REGEX is going to be kind of expensive, as are other string operations. If you can query for the strings you want directly, you might be better off. Unfortunately, you'll need to start working with language tags, too. For instance, here is a query that counts people with the given name or surname "Leonardo".
select COUNT(?person) where {
?person a dbpedia-owl:Person .
{ ?person foaf:givenName "Leonardo" }
UNION
{ ?person foaf:surname "Leonardo" }
}
SPARQL results
It returns 0. However, if we add (English) language tags to those strings, we get different results (133):
select COUNT(?person) where {
?person a dbpedia-owl:Person .
{ ?person foaf:givenName "Leonardo"#en }
UNION
{ ?person foaf:surname "Leonardo"#en }
}
SPARQL results