Skip to content

Commit

Permalink
query releases
Browse files Browse the repository at this point in the history
  • Loading branch information
karmadolkar committed Jun 17, 2020
1 parent fe46499 commit 1be960b
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 6 deletions.
3 changes: 3 additions & 0 deletions bodhi/server/config.py
Expand Up @@ -361,6 +361,9 @@ class BodhiConfig(dict):
'initial_bug_msg': {
'value': '%s has been submitted as an update to %s. %s',
'validator': str},
'graphiql_enabled': {
'value': False,
'validator': _validate_bool},
'greenwave_api_url': {
'value': 'https://greenwave-web-greenwave.app.os.fedoraproject.org/api/v1.0',
'validator': _validate_rstripped_str},
Expand Down
34 changes: 34 additions & 0 deletions bodhi/server/graphql_schemas.py
@@ -0,0 +1,34 @@
# Copyright © 2020 Red Hat Inc., and others.
#
# This file is part of Bodhi.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Defines schemas related to GraphQL objects."""
from graphene import relay, Field, String
from graphene_sqlalchemy import SQLAlchemyObjectType

from bodhi.server.models import Release as ReleaseModel


class Release(SQLAlchemyObjectType):
"""Type object representing a distribution release from bodhi.server.models, such as Fedora 27."""

class Meta:
"""Allow to set different options to the class."""

model = ReleaseModel
interfaces = (relay.Node, )
state = Field(String)
package_manager = Field(String)
30 changes: 27 additions & 3 deletions bodhi/server/services/graphql.py
Expand Up @@ -16,19 +16,43 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Defines API endpoints related to GraphQL objects."""
import graphene
from cornice import Service
from webob_graphql import serve_graphql_request

from bodhi.server.config import config
from bodhi.server.graphql_schemas import Release


graphql = Service(name='graphql', path='/graphql', description='graphql service')


@graphql.get()
@graphql.post()
def graphql_get(request):
"""
Return "Hello World".
Perform a GET request.
Args:
request (pyramid.Request): The current request.
Returns:
str: A string "Hello World".
The GraphQL response to the request.
"""
return "Hello World"
context = {'session': request.session}
return serve_graphql_request(
request, schema, graphiql_enabled=config.get('graphiql_enabled'),
context_value=context)


class Query(graphene.ObjectType):
"""Allow querying objects."""

allReleases = graphene.List(Release)

def resolve_allReleases(self, info):
"""Answer Queries by fetching data from the Schema."""
query = Release.get_query(info) # SQLAlchemy query
return query.all()


schema = graphene.Schema(query=Query)
41 changes: 38 additions & 3 deletions bodhi/tests/server/services/test_graphql.py
Expand Up @@ -15,12 +15,47 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from graphene.test import Client

from bodhi.tests.server import base
from bodhi.server.services.graphql import schema
from bodhi.server.models import PackageManager, Release


class TestGraphQLService(base.BasePyTestCase):
"""This class contains tests for a /graphql endpoint"""
def test_get(self):
"""Ensure that "Hello World" is returned"""
res = self.app.get('/graphql')
assert res.body == b'"Hello World"'
"""Ensure that a GraphQL response is returned"""
res = self.app.get('/graphql?query={%0A%20 allReleases{%0A%20%20%20 name%0A%20 }%0A}')
assert res.body == b'{"data":{"allReleases":[{"name":"F17"}]}}'

def test_allReleases(self):
"""Testing allReleases"""
client = Client(schema)
release = Release(
name='F22', long_name='Fedora 22',
id_prefix='FEDORA', version='22',
dist_tag='f22', stable_tag='f22-updates',
testing_tag='f22-updates-testing',
candidate_tag='f22-updates-candidate',
pending_signing_tag='f22-updates-testing-signing',
pending_testing_tag='f22-updates-testing-pending',
pending_stable_tag='f22-updates-pending',
override_tag='f22-override',
branch='f22',
package_manager=PackageManager.dnf,
testing_repository='updates-testing')

self.db.add(release)
self.db.commit()

executed = client.execute("""{ allReleases{ name }}""")
assert executed == {
"data": {
"allReleases": [{
"name": "F17"
}, {
"name": "F22"
}]
}
}
9 changes: 9 additions & 0 deletions devel/ansible/roles/bodhi/tasks/main.yml
Expand Up @@ -109,6 +109,15 @@
name: jinja2-cli
executable: pip3

- name: pip install packages for GraphQL
pip:
name: "{{ item }}"
executable: pip3
loop:
- WebOb-GraphQL
- graphene
- graphene-sqlalchemy

- name: Fake a pungi install
file:
src: /usr/bin/true
Expand Down
2 changes: 2 additions & 0 deletions devel/development.ini.example
Expand Up @@ -71,6 +71,8 @@ test_gating.required = True
waiverdb_api_url = http://bodhi_user:pass@localhost:6544/api/v1.0
greenwave_api_url = http://localhost:6545/api/v1.0

graphiql_enabled = True

[server:main]
use = egg:waitress#main
host = 0.0.0.0
Expand Down
6 changes: 6 additions & 0 deletions production.ini
Expand Up @@ -316,6 +316,12 @@ use = egg:bodhi-server
# you are using the dummy acl_system.
# acl_dummy_committer =

##
## GraphiQL
##
# graphiql_url = http://localhost:6543/graphql
# graphiql_enabled = False

##
## Pagure
##
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Expand Up @@ -10,6 +10,8 @@ dogpile.cache
pyasn1-modules # Due to an unfortunate dash in its name, installs break if pyasn1 is installed first
fedora_messaging
feedgen>=0.7.0
graphene
graphene-sqlalchemy
jinja2
markdown
psycopg2
Expand All @@ -24,3 +26,4 @@ PyYAML
simplemediawiki
sqlalchemy
waitress
WebOb-GraphQL

0 comments on commit 1be960b

Please sign in to comment.