9117124, 9:05 PM ‘Support for Graph. in generateDS - Dave's page
Dave's page
Support for GraphQL in generateDS
Contents,
1_ Introduction
2 Generate a module containing Graph support
3 Run the Strawberry server
‘Make requests and queries
‘S_ Examples of usage
5.1 NeuroML and Ii
5.2 Open stronomyl
53. Nexm
5.4 Demos/Peop! y
5.5. Emdb -- the Electron Microscopy Data Bank
1_Introduction
You can generate modules that contain code to support GraphQL queries. The queries can be applied to the data contained in
XML instance documents described by the XML schema from which you generated your module. For information about
GraphQL see ~ https://eraphal.ore’.
Important: This capability requires generateDs version 2.43.2 or later.
‘The generated code uses the Strawberry GraphQL Python module, See https://pypi.org/praject/strawberry-eraphql! and
htps://strawberry.rocks,
In order to use the generateos support for GraphQL, you must install Strawberry. See the Strawberry getting started guide at
httpsu/strawberry.rocks/does
This facility supports GraphQL queries. There is no support for mutations.
For information on generateds sce hitp:/iwww.davekuhlman.org/generateDS.html
For information on GraphQL see https://graphgLorg/
‘There is an example of the use of this capability in the Denos/People directory within the generateos source distribution,
which you can clone with the following:
$ hg clone http://hg.code. sf.net/p/generateds/code generateds-code
Please post suggestions and questions at hups.J/sourceforge.net/p/venerateds/discussion/general
2_Generate a module containing GraphQL support
In order to generate a module that contains this support code, use the "--graphql:
generateds. py. For example:
command line option when you run
$ generateDS.py -0 my_module.py --graphql="tagnane: typename" my_xnl_schena.xsd
Where:
hitps:www-davekuhiman orgigenerateds-grapnl him! 69117124, 9:05 PM ‘Suppor for Graph. in generateDS - Dave's page
+ tagnane is the tag of the top-most element in your XML instance documents.
+ typenane is the name of the XML conplextype for the top-most element in your XML instance documents.
You have now generated a GraphQL API against which you can apply GraphQL queries. These queries follow the nested
structure of your XML instance document. At each level, you have access to the child nodes and the attributes of the nodes
at that level
3_Run the Strawberry server
‘After generating your module, you can run the Strawberry server that the module implements with something like the
following
$ strawberry server my module
Important ~ Before running the Strawberry server on your module, you must set the environment variable GRAPHQL_ARGS to
specify the path/name of an XML instance document that contains the data to which you want to apply GraphQL queries.
This XML document must be an instance of the document type of the schema used to generate your module and should
validate against that schema.
{'m on Linux, and so I can do that with, for example, either of the following, assuming that "my_module.py" is the name of
the module generated by generateDS.py
$ export GRAPHQL_ARGS=ny_input_data.xnl
$ stranberry server my module
Or, on a single line:
S GRAPHQL_ARGS=*
1y_input_dat
-xml strawberry server my_nodule
For help with starting the Strawberry server, run the following:
$ stranberry --help
$ stramberry server --help
4 Make requests and queries
‘After starting Strawberry, you can interact with your Strawberry server in the following ways (among others):
+ Visit nttp://0.0.8.0:8000/graphqi in your Web browser and use the Strawberry interactive REPL. (read-eval-print
loop). Note that interactive Strawberry supports tab completion and provides visual hints to guide you through your
generated GraphQL API. Alternatively, near the upper left of the sereen (in your Web browser) is a button that will
display a GraphQl. Explorer, which, among other things, will describe your GraphQL API.
+ Use curt to make requests (see httpsi//curl,se/) and receive JSON data, For example, here is a bash shell script that I
can run from the Linux command line:
#1 /use/bin/bash -x
curl ‘http://@.0.0.0:8080/graphal' \
“X POST
-H “content-type: application/ json’ \
data '{
"query": "{ container { author { nane book { author title date genre rating 3))
+ Use a Python script and the Python requests module (see btps/pyiore/projectirequests/ and
‘nttps:/irequests.readthedocs.io/en/latest/), For example:
#I/usr/bin/eny python
synopsis:
Denonstration of Python requests to a Strawberry GraphQl. server.
hitps:www-davekuhiman orgigenerateds-grapnl him! 269117124, 9:05 PM ‘Support for Graph. in generateDS - Dave's page
usage:
python
[email protected]
Amport requests
# Import json
def test()
headers = (
“content-type": ‘application/jsen’,
>
url = "hetp://0.6.0.0:8000/graphat”
query = ('( "query": "{ container { author
“{ name book (author title date rating genre}}))" )")
response = requests.post(url, query, headers-headers)
Jsonod} = response. json()
data = json003["aata"}
container ~ data[ "contain
author = container "autho
‘for ten in author
prins(*nane: {}' format item{"nane"]))
for iter in iten{"book"]
print
© author: () date: () tithe
}" Format (
ant
author"),
item ("date"),
tent ["titl
Ye
1
tend ["genr
tent ["rating"],
»
def main()
test()
if _name_
Fain(y
5_ Examples of usage
The examples below are intended to show how to prepare and run generatos GraphQL servers. They may or may not have
uses in the real world.
5.
jeuroML,
NeuroML and
References:
+ ttps://pypi.org/projectlibNeuroML
+ hupsi/ibmeurom readthedocs jo/en/latest
+ https//docs neuro! org/Landing. him!
Instructions:
1, Download the repository — https://github.com/NeuroML/NewroML?. git
2. Build the server module:
$ -/generateds. py \
fF
“0 tmpet.py \
=-graphql="neuronl :NeuroMLDocunent" \
-+/Sit/NeurotL2/Schenas/NeuroNL2/NeuroMl_v2.2.xsd
3, Run the server -- We need to set an environment variable GRAPHQL_ARGS. On Linux, I can do the following. On other
platforms, you may need to make an adjustment:
hitps:www-davekuhiman orgigenerateds-grapnl him!
a69117124, 9:05 PM ‘Suppor for Graphal. in generateDS - Dave's pa
5 GRAPHQL_ARGS=../6it/NeuroML2/examples/NML2_FullCell.nml strawberry server tnpel
5.2. OpenAstronomyLog
References:
+ https://github.com/openastronomylog/openastronomylog
Instructions:
1. Download the repository ~ htips/github com/epenastronomylog/openastronomylog
2. Build the server module:
$ ./generateDs.py -F -0 tepel.py ~g observations:observations openastrononylog-naster/sr¢/oal21.xsd
3. Run the server:
$ GRAPHQL_ARG:
penastrononylog-naster/doc/OAL2@_simple_example.xml strawberry server tapel
5.3. Nexm
Instructions:
1, Download the Nexel repository ~ https://github, com/nexml/nexmlL git
$ mkdir MyTestoir
$ cd MyTestDir
$ git Clone https: //github.con/nexml/nexnl git
2. Build the server module. For example:
$ ./generatens.py -f - lex :Nexs
[email protected] ~-raphql="
/nexml/xsd/nexm) .xsé
3. Run the server -- Here are several data sets that seem to produce reasonable results:
/nexml/exanples/characters.xml strawberry server tup@t
/nexnl /exanples/tolskeletaldunp-nexnl.xnl strawberry server tnpol
If you run the server in the basic way (as above), you can explore your data in your Web browser at
htip://0.0.0.0:8000/eraphql
And, here is a shell script that makes a request
#1/usr/bin/bash x
curl ‘http: //0.0.0.0:8008/graphql" \,
“x posT \
-H ‘content-type: application/Json' \
data '{ "query": "{ Nexrl { characters
| python -m json.tool
format ( states { id state { id } } char ( id states }} } } }" }"\
Here is a roughly equivalent Python script:
#I/usr/bin/env python
import requests
import pprint
Queryes =
{ "query":
{
Nexal {
characters {
hips. davekuhiman orgigenerateds-grapnl him! 4169117124, 9:05 PM ‘Suppor for Graph. in generateDS - Dave's pa
format {
states {
def test()
headers = {
“content-type': ‘application/json’,
»
unl = “http://2.0.0.0:8000/graphql’
query = Queryes
query = query.replace("\n", * ')
response = requests.post(url, query, headers=neaders)
jsonobj = response. json()
data = jsonob j{"data’
print("=* * 40)
pprint .pprint (data)
print(’-" * 48)
def main()
test()
if _nane__ == "_nain_’
main()
5.4_Demos/People in the generateDS repository
‘There is a reasonably usable example in the generateDs source code distribution
References:
+ Documentation on generateds, itself ~ https/www.davekuhiman org/generateDS. html
+ Documentation of GraphQL support by generatebs -- http://www, daveluhlman org/generateDS.iml#praphl-support
+ The generateds source repository — btins://sourceforge.netprojects/generateds/
Instructions:
There are scripts that can be used to build and run the server. See benos/People/run-gen-graphql.sh and
/Dewoes/People/run-test-graphal. sh.
1, Download the generateds repository -- https://sourceforge.nct/p/gencrateds/code/ci/defaulvtree!
2. Use ./Denos/People/run-gen-gnaphal. sh (or the following) to generate a module that implements a Strawberry
GraphQL server:
$ generateDs.py \
“F\
“0 people_graphgl.py \
anespacedef= 'xnlns:pl=
~¢ people_catalog.xnl \
“graphql="people:peopletype" \
people. xsd
yttp://kuhlman .con/people. xs
3. Run the Strawberry GraphQL server (use ./Denoes/People/run-test-graphql .sh) or the following:
hitps:www-davekuhiman orgigenerateds-grapnl him! 569117124, 9:05 PM ‘Suppor for Graphal. in generateDS - Dave's pa
$ GRAPHQL_ARGS=people.xml straberry server people_graphql
5.5 Emdb — the Electron Microscopy Data Bank
Emdb provides a good test for generateDS + GraphQL,
Use these instructions to try it out
1. (Optional) Create and switch to an empty directory in which to create files and run your test.
2. In that directory, run the following:
#1/usr/bin/bash -x
wget https: //-Ftp.ebi .ac.uk/pub/databases/encb/doc/XML -schenas/endb-schenas/current/endb.xsd
wget https://ftp.ebi .ac.uk/pub/databases /endb/ structures/=¥0-16296/header/end-16296-v32.xml
generateDS.py -F -o test0i.py --menber-specs=dict endb.xsd
python
[email protected] end-16296-v30. xn]
generateDS.py -f -o endb_graphql .py --graphal="end:entry_type" endb.xsd
GRAPHQL_ARGS=end-16296-V30.xnl strawberry server endb_graphql
3. Point your browser at hi
Notes:
+ The first two lines of the above script download the Emdb XML schema and a sample XML data file that validates
against that schema
+ The next two lines generate and test a standard generateos module and test it against our Emdb sample data file
+ And, the final two lines generate a generateDs module that contains support for GraphQL and then starts the
Strawberry GraphQL server using that module.
Published
Fri 14 July 2023
Last Updated
Fri 25 August 2023
Category
Contact
Powered by: Pelican Theme: Elegant
hitps:www-davekuhiman orgigenerateds-grapnl him!
as