Skip to content

Commit 29d44ba

Browse files
pmantica1bojanserafimov
authored andcommitted
Improve getting started documentation (kensho-technologies#323)
* Improving schema autogeneration documentation * Fix README.md example * Nits * Fix typo * Fix step number * Revert testing config changes * Fix end-to-end example * Shorten README.md * Nit * Remove boilerplate database startup code * Fix typo * Communicate assumptions through code * Nits * Remove getting started section
1 parent 615033b commit 29d44ba

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

README.md

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ query languages.
2323

2424
Furthermore, the GraphQL compiler validates queries through the use of a GraphQL schema
2525
that specifies the underlying schema of the database. We can currently autogenerate a
26-
GraphQL schema by introspecting an OrientDB database, (see [Querying OrientDB with GraphQL](#querying-orientdb-with-graphql)).
26+
GraphQL schema by introspecting an OrientDB database, (see [End to End Example](#end-to-end-example)).
2727

2828
In the near future, we plan to add schema autogeneration from SQLAlchemy metadata as well.
2929

@@ -32,7 +32,7 @@ For a more detailed overview and getting started guide, please see
3232

3333
## Table of contents
3434
* [Features](#features)
35-
* [Querying OrientDB with GraphQL](#querying-orientdb-with-graphql)
35+
* [End to End Example](#end-to-end-example)
3636
* [Definitions](#definitions)
3737
* [Directives](#directives)
3838
* [@optional](#optional)
@@ -77,27 +77,56 @@ For a more detailed overview and getting started guide, please see
7777
these databases. See the [SQL](#sql) section for more details.
7878
* **GraphQL Language Features:** We prioritized and implemented a subset of all functionality supported by the GraphQL language. We hope to add more functionality over time.
7979

80-
## Querying OrientDB with GraphQL
81-
```python3
80+
## End-to-End Example
81+
Even though this example specifically targets an OrientDB database, it is meant as a generic
82+
end-to-end example of how to use the GraphQL compiler.
83+
84+
```python
85+
from graphql.utils.schema_printer import print_schema
8286
from graphql_compiler import (
8387
get_graphql_schema_from_orientdb_schema_data, graphql_to_match
8488
)
8589
from graphql_compiler.schema_generation.orientdb.utils import ORIENTDB_SCHEMA_RECORDS_QUERY
86-
from graphql_compiler.tests.conftest import init_integration_graph_client
87-
88-
# The following code is meant to serve as a mock example and will not run
89-
# unless you are in the development environment outlined by CONTRIBUTING.md.
90-
91-
# Step 1: Initialize dummy OrientDB database and get pyorient OrientDB client
92-
client = init_integration_graph_client()
9390

94-
# Step 2: Generate GraphQL schema from queried OrientDB schema records
91+
# Step 1: Get schema metadata from hypothetical Animals database.
92+
client = your_function_that_returns_an_orientdb_client()
9593
schema_records = client.command(ORIENTDB_SCHEMA_RECORDS_QUERY)
96-
schema_data = [x.oRecordData for x in schema_records]
94+
schema_data = [record.oRecordData for record in schema_records]
95+
96+
# Step 2: Generate GraphQL schema from metadata.
9797
schema, type_equivalence_hints = get_graphql_schema_from_orientdb_schema_data(schema_data)
9898

99-
# Step 3: Write GraphQL query to get the names of all animals with a particular net worth
100-
# Note that we prefix net_worth with '$' and surround it with quotes to indicate it's a parameter
99+
print(print_schema(schema))
100+
# schema {
101+
# query: RootSchemaQuery
102+
# }
103+
#
104+
# directive @filter(op_name: String!, value: [String!]!) on FIELD | INLINE_FRAGMENT
105+
#
106+
# directive @tag(tag_name: String!) on FIELD
107+
#
108+
# directive @output(out_name: String!) on FIELD
109+
#
110+
# directive @output_source on FIELD
111+
#
112+
# directive @optional on FIELD
113+
#
114+
# directive @recurse(depth: Int!) on FIELD
115+
#
116+
# directive @fold on FIELD
117+
#
118+
# type Animal {
119+
# name: String
120+
# net_worth: Int
121+
# limbs: Int
122+
# }
123+
#
124+
# type RootSchemaQuery{
125+
# Animal: [Animal]
126+
# }
127+
128+
# Step 3: Write GraphQL query that returns the names of all animals with a certain net worth.
129+
# Note that we prefix net_worth with '$' and surround it with quotes to indicate it's a parameter.
101130
graphql_query = '''
102131
{
103132
Animal {
@@ -110,24 +139,14 @@ parameters = {
110139
'net_worth': '100',
111140
}
112141

113-
# Step 4: Use autogenerated GraphQL schema to compile query into Match, an OrientDB query language
142+
# Step 4: Use autogenerated GraphQL schema to compile query into the target database language.
114143
compilation_result = graphql_to_match(schema, graphql_query, parameters, type_equivalence_hints)
115-
116-
# Step 5: Run query in OrientDB
117-
query = compilation_result.query
118-
results = [row.oRecordData for row in client.command(query)]
119-
assert results == [{'animal_name': 'Animal 1'}]
144+
print(compilation_result.query)
145+
# SELECT Animal___1.name AS `animal_name`
146+
# FROM ( MATCH { class: Animal, where: ((net_worth = decimal("100"))), as: Animal___1 }
147+
# RETURN $matches)
120148
```
121149
## Definitions
122-
- **GraphQLSchema object**- To be able to compile GraphQL, the first thing you will need is a GraphQLSchema object describing
123-
the underlying database. To build it you can use `get_graphql_schema_from_orientdb_schema_data` as
124-
demonstrated in the code example below.
125-
```python
126-
# Generate GraphQL schema from queried OrientDB schema records
127-
schema_records = client.command(ORIENTDB_SCHEMA_RECORDS_QUERY)
128-
schema_data = [x.oRecordData for x in schema_records]
129-
schema, type_equivalence_hints = get_graphql_schema_from_orientdb_schema_data(schema_data)
130-
```
131150
- **Vertex field**: A field corresponding to a vertex in the graph. In the below example, `Animal`
132151
and `out_Entity_Related` are vertex fields. The `Animal` field is the field at which querying
133152
starts, and is therefore the **root vertex field**. In any scope, fields with the prefix `out_`

0 commit comments

Comments
 (0)