• Stars
    star
    550
  • Rank 80,305 (Top 2 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Turn complex GraphQL queries into optimized database queries.

graphql-compiler

Build Status Coverage Status License PyPI Python PyPI Version PyPI Status PyPI Wheel Code style: black

Turn complex GraphQL queries into optimized database queries.

pip install graphql-compiler

Quick Overview

GraphQL compiler is a library that simplifies data querying and exploration by exposing one simple query language written using GraphQL syntax to target multiple database backends. It currently supports OrientDB. and multiple SQL database management systems, such as PostgreSQL, MSSQL and MySQL.

For a detailed overview, see our blog post. To get started, see our Read the Docs documentation. To contribute, please see our contributing guide.

Examples

OrientDB

from graphql.utils.schema_printer import print_schema
from graphql_compiler import (
    get_graphql_schema_from_orientdb_schema_data, graphql_to_match
)
from graphql_compiler.schema.schema_info import CommonSchemaInfo
from graphql_compiler.schema_generation.orientdb.utils import ORIENTDB_SCHEMA_RECORDS_QUERY

# Step 1: Get schema metadata from hypothetical Animals database.
client = your_function_that_returns_an_orientdb_client()
schema_records = client.command(ORIENTDB_SCHEMA_RECORDS_QUERY)
schema_data = [record.oRecordData for record in schema_records]

# Step 2: Generate GraphQL schema from metadata.
schema, type_equivalence_hints = get_graphql_schema_from_orientdb_schema_data(schema_data)

print(print_schema(schema))
# schema {
#    query: RootSchemaQuery
# }
#
# directive @filter(op_name: String!, value: [String!]!) on FIELD | INLINE_FRAGMENT
#
# directive @tag(tag_name: String!) on FIELD
#
# directive @output(out_name: String!) on FIELD
#
# directive @output_source on FIELD
#
# directive @optional on FIELD
#
# directive @recurse(depth: Int!) on FIELD
#
# directive @fold on FIELD
#
# type Animal {
#     name: String
#     net_worth: Int
#     limbs: Int
# }
#
# type RootSchemaQuery{
#     Animal: [Animal]
# }

# Step 3: Write GraphQL query that returns the names of all animals with a certain net worth.
# Note that we prefix net_worth with '$' and surround it with quotes to indicate it's a parameter.
graphql_query = '''
{
    Animal {
        name @output(out_name: "animal_name")
        net_worth @filter(op_name: "=", value: ["$net_worth"])
    }
}
'''
parameters = {
    'net_worth': '100',
}

# Step 4: Use autogenerated GraphQL schema to compile query into the target database language.
common_schema_info = CommonSchemaInfo(schema, type_equivalence_hints)
compilation_result = graphql_to_match(common_schema_info, graphql_query, parameters)
print(compilation_result.query)
# SELECT Animal___1.name AS `animal_name`
# FROM  ( MATCH  { class: Animal, where: ((net_worth = decimal("100"))), as: Animal___1 }
# RETURN $matches)

SQL

from graphql_compiler import get_sqlalchemy_schema_info, graphql_to_sql
from sqlalchemy import MetaData, create_engine

engine = create_engine('<connection string>')

# Reflect the default database schema. Each table must have a primary key. Otherwise see:
# https://graphql-compiler.readthedocs.io/en/latest/supported_databases/sql.html#including-tables-without-explicitly-enforced-primary-keys
metadata = MetaData(bind=engine)
metadata.reflect()

# Wrap the schema information into a SQLAlchemySchemaInfo object.
sql_schema_info = get_sqlalchemy_schema_info(metadata.tables, {}, engine.dialect)

# Write GraphQL query.
graphql_query = '''
{
    Animal {
        name @output(out_name: "animal_name")
    }
}
'''
parameters = {}

# Compile and execute query.
compilation_result = graphql_to_sql(sql_schema_info, graphql_query, parameters)
query_results = [dict(row) for row in engine.execute(compilation_result.query)]

License

Licensed under the Apache 2.0 License. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2017-present Kensho Technologies, LLC. The present date is determined by the timestamp of the most recent commit in the repository.

More Repositories

1

pyctcdecode

A fast and lightweight python-based CTC beam search decoder for speech recognition.
Python
421
star
2

orama

Plug and play React charts
JavaScript
123
star
3

qwikidata

Python tools for interacting with Wikidata
Python
121
star
4

pytest-annotate

Generate PyAnnotate annotations from your pytest tests.
Python
107
star
5

check-more-types

Lots and lots of predicates and checks for JavaScript (Node/Browser)
JavaScript
65
star
6

sequence_align

Efficient implementations of Needleman-Wunsch and other sequence alignment algorithms written in Rust with Python bindings via PyO3.
Python
63
star
7

grift

A clean approach to app configuration
Python
54
star
8

bubs

Keras Implementation of Flair's Contextualized Embeddings
Python
27
star
9

ng-alertify

[DEPRECATED] AngularJS wrapper around alertify popup library
JavaScript
18
star
10

special_k

Safe serialization of ML models
Python
16
star
11

kwnlp-sql-parser

Utilities for parsing Wikipedia MySQL/MariaDB dumps.
Python
11
star
12

eagr

Python gRPC servers and clients, made friendlier
Python
11
star
13

game-of-graphql

Demo project for the GraphQL compiler using Game of Thrones data
Python
9
star
14

eslint-config

Shared ESLint config
JavaScript
5
star
15

wikiwhatsthis

An opensource R&D repo for the WikiWhatsThis project
Python
4
star
16

kwnlp-preprocessor

Download, parse, and convert raw Wikimedia data into standard formats.
Python
3
star
17

babel-preset

Babel preset to transpile ES2020, TS(X), and language proposals
JavaScript
2
star
18

prettier-config

Shared Prettier config
TypeScript
2
star
19

benchmarks-pipeline

Python
2
star
20

tsconfig

Shared TypeScript config used across Kensho projects
TypeScript
1
star
21

kwnlp-dump-downloader

Utilities for downloading and checking the status of Wikimedia dumps.
Python
1
star
22

kensho-service-auth

Example code and documentation for authenticating with Kensho services
Java
1
star