Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/add_queries'
Browse files Browse the repository at this point in the history
  • Loading branch information
alastair committed Jun 11, 2020
2 parents 80665e9 + c4e4814 commit 21915b4
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 163 deletions.
5 changes: 3 additions & 2 deletions tests/data/person/query_person.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
query {
Person
{
Person(

) {
identifier
name
publisher
Expand Down
5 changes: 3 additions & 2 deletions tests/data/person/query_person_parameter.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
query {
Person(identifier: "ff59650b-1d47-4ea5-b356-31fddeb48315")
{
Person(
identifier: "ff59650b-1d47-4ea5-b356-31fddeb48315"
) {
identifier
name
publisher
Expand Down
13 changes: 0 additions & 13 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import unittest

from trompace.mutations import document
from trompace.queries import document as document_query
from tests import util


Expand All @@ -13,18 +12,6 @@ def setUp(self) -> None:
super()
self.data_dir = os.path.join(os.path.dirname(__file__), "data", "document")

def test_query(self):
expected = util.read_file(self.data_dir, "EXPECTED_DOCUMENT_QUERY.txt")

created_document = document_query.query_document("ff59650b-1d47-4ea5-b356-31fddeb48315")
self.assertEqual(created_document, expected)

def test_query_all(self):
expected = util.read_file(self.data_dir, "EXPECTED_DOCUMENT_QUERY_ALL.txt")

created_document = document_query.query_document()
self.assertEqual(created_document, expected)

def test_create(self):
expected = util.read_file(self.data_dir, "EXPECTED_DOCUMENT.txt")

Expand Down
3 changes: 3 additions & 0 deletions trompace/queries/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
QUERY = '''query {{
{query}
}}'''
82 changes: 0 additions & 82 deletions trompace/queries/document.py

This file was deleted.

120 changes: 66 additions & 54 deletions trompace/queries/person.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,76 @@
# Generate GraphQL queries for queries pertaining to persons objects.
from typing import List
# Generate GraphQL queries for queries pertaining to person objects.

from trompace import QUERY, filter_none_args, make_parameters
from trompace.exceptions import UnsupportedLanguageException, NotAMimeTypeException
from trompace.queries.templates import format_query
from trompace import StringConstant, _Neo4jDate, filter_none_args
from trompace.constants import SUPPORTED_LANGUAGES

QUERY_PERSON = '''Person{parameters}
{{
{fields}
}}'''

QUERY_PERSON_SUBSTRING = '''personBySubstring{parameters}
{{
{fields}
}}'''

default_fields = ["identifier", "name", "publisher", "contributor", "creator", "source", "description", "language"]


# TODO: We only define some basic query fields for now. Potential to add more if we see that they're needed
def query_person(query: str = None, identifier: str = None, contributor: str = None, creator: str = None,
source: str = None,
first: int = None, offset: int = None, fields: List[str] = None):
"""Returns a query for obtaining Person objects

def query_person(identifier: str=None,title: str=None, contributor: str=None, creator: str=None, source: str=None,
language: str = None, format_: str = None, name: str = None,
family_name: str = None, given_name: str = None, gender: str = None,
birth_date: str = None, death_date: str = None,
description: str = None, image: str = None, publisher: str = None,
honorific_prefix: str = None, honorific_suffix: str = None, job_title: str = None):
"""Returns a mutation for creating a Person
Arguments:
query: perform a substring query on the name field
identifier: get a Person with this identifier
contributor: get Person objects from this contributor
creator: get Person objects from this creator
source: get Person objects from this source
first: get the first this many items
offset: offset search results by this value
fields: return these fields in the Person objects. Defaults to `trompace.queries.person.default_fields`
identifier: The identifier of the person in the CE to be updated
title: The title of the resource indicated by `source`
contributor: The main URL of the site where the information about this Person was taken from
creator: The person, organization or service who is creating this Person (e.g. URL of the software)
source: The URL of the web resource where information about this Person is taken from
language: The language the metadata is written in. Currently supported languages are en,es,ca,nl,de,fr
format_: The mimetype of the resource indicated by `source`
name: The name of the person
family_name (optional): The family name of the person
given_name (optional): The given name of the person
gender (optional): The person's gender
birth_date (optional): The birth date of the person, formatted as yyyy, yyyy-mm or yyyy-mm-dd
death_date (optional): The date of death of the person , formatted as yyyy, yyyy-mm or yyyy-mm-dd
description (optional): A biographical description of the person
image (optional): URL to an image associated with the person
publisher (optional): An entity responsible for making the resource available
honorific_prefix (optional): An honorific prefix.
honorific_suffix (optional): An honorific suffix.
job_title (optional): The person's job title.
Returns:
A Person Query string
The string for the mutation for creating the person.
Raises:
UnsupportedLanguageException if `language` is not one of the supported languages.
NotAMimeTypeException if `format_` is not a valid mimetype.
"""

if query:
args = {"query": query,
"first": first,
"offset": offset}
querystr = QUERY_PERSON_SUBSTRING
else:
args = {"identifier": identifier,
"contributor": contributor,
"creator": creator,
"source": source,
"first": first,
"offset": offset}
querystr = QUERY_PERSON
if language and language.lower() not in SUPPORTED_LANGUAGES:
raise UnsupportedLanguageException(language)

args = filter_none_args(args)
if format_ and "/" not in format_:
raise NotAMimeTypeException(format_)

if fields:
fields = "\n".join(fields)
else:
fields = "\n".join(default_fields)
args = {
"identifier": identifier,
"title": title,
"contributor": contributor,
"creator": creator,
"source": source,
"format": format_,
"name": name,
"familyName": family_name,
"givenName": given_name,
"gender": gender,
"description": description,
"image": image,
"publisher": publisher,
"honorificPrefix": honorific_prefix,
"honorificSuffix": honorific_suffix,
"jobTitle": job_title
}
if language is not None:
args["language"] = StringConstant(language.lower())
if birth_date is not None:
args["birthDate"] = _Neo4jDate(birth_date)
if death_date is not None:
args["deathDate"] = _Neo4jDate(death_date)

args = filter_none_args(args)

parameters = ""
if args:
parameters = "({})".format(make_parameters(**args))
formatted_query = querystr.format(parameters=parameters, fields=fields)
return QUERY.format(query=formatted_query)
return format_query("Person", args)
36 changes: 26 additions & 10 deletions trompace/queries/templates.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
# Templates for generate GraphQL queries for mutations.
# Templates for generating GraphQL queries.

# To be added EntryPoint, ControlAction, PropertyValueSpecification and Property
from .. import make_parameters, QUERY
from typing import Dict, Any

from trompace.queries import QUERY
from trompace import make_parameters

QUERY_TEMPLATE = '''{queryname}(
{parameters}
) {{
identifier
name
publisher
contributor
creator
source
description
language
}}'''

def query_create(args, query_string: str):
"""Returns a mutation for creating an object.
Arguments:
args: a dictionary of arguments for the template. The fucntion calling this function is responsible for validating the arguments.


def format_query(queryname: str, args: Dict[str, Any]):
"""Create a query to send to the Contributor Environment.
Arguments:
mqueryname: the name of the query to generate
args: a dictionary of field: value pairs to add to the mutation
Returns:
The string for the mutation for creating the object.
Raises:
Assertion error if the input language is not one of the supported languages.
A formatted query
"""

create_query = query_string.format(parameters=make_parameters(**args))
return QUERY.format(query=create_query)
formatted_query = QUERY_TEMPLATE.format(queryname=queryname, parameters=make_parameters(**args))
return QUERY.format(query=formatted_query)

0 comments on commit 21915b4

Please sign in to comment.