Skip to main content

FastGraphQL is intended to help developer create code driven GraphQL APIs

Project description

FastGraphQL

FastGraphQL is intended to help developer create code driven GraphQL APIs.

pypi Python Versions License

codecov tests

Code Smells Security Rating Maintainability Rating Vulnerabilities Bugs Duplicated Lines (%) Technical Debt

Disclaimer

This is still a work in progress

Motivation

So far most of the projects that uses GraphQL need to duplicate many definitions to be able to have a consistent GraphQL API schema alongside well-defined models that governs the development and the application.

FastGraphQL tries to shortcut the path between python models and GraphQL schema using Pydantic models. This ensures not only a single source of truth when comes to type, inputs, query and mutation definition reflected in classes and methods, but also the ability to use Pydantic to validate models.

Installation

pip install fastgraphql

Usage

GraphQL Types and Inputs

Using annotation driven definitions and Pydantic, defining GraphQL types and inputs can be done by simple annotating Pydantic models with FastGraphQL.graphql_type() of FastGraphQL.graphql_input()

from datetime import datetime
from typing import Optional
from pydantic import BaseModel
from fastgraphql import FastGraphQL

fast_graphql = FastGraphQL()

@fast_graphql.graphql_type()
class Model(BaseModel):
    t_int: int
    t_opt_int: Optional[int]
    t_str: str
    t_opt_str: Optional[str]
    t_float: float
    t_opt_float: Optional[float]
    t_datatime: datetime
    t_opt_datatime: Optional[datetime]
    t_boolean: bool
    t_opt_boolean: Optional[bool]

@fast_graphql.graphql_type()
class Input(BaseModel):
    t_int: int
    
print(fast_graphql.render())

The above code example generates a schema as follows:

scalar DateTime

type Model {
    t_int: Int!
    t_opt_int: Int
    t_str: String!
    t_opt_str: String
    t_float: Float!
    t_opt_float: Float
    t_datatime: DateTime!
    t_opt_datatime: DateTime
    t_boolean: Boolean!
    t_opt_boolean: Boolean
}

type Input {
    t_int: Int!
}

Query and Mutation

Following the same approach with annotation driven defitions, query and mutations can easily be defined using FastGraphQL.graphql_query and FastGraphQL.mutation.

Note that all function arguments annotated with FastGraphQL.graphql_query_field are considered to be input arguments for the GraphQL API and simples types and Pydantic models can be used and arguments and also as return type and they don't need to be explicitly annotated.

from fastgraphql import FastGraphQL
from pydantic import BaseModel
fast_graphql = FastGraphQL()

class Model(BaseModel):
    param: str

@fast_graphql.graphql_query()
def my_first_query(
        model: Model = fast_graphql.graphql_query_field(),
        param: str = fast_graphql.graphql_query_field()
) -> str:
    ...

print(fast_graphql.render())

The above code example generates a schema as follows:

input Model {
    param: String!
}
type Query {
    my_first_query(model: Model!, param: String!): String!
}

Dependecy Injection

Query and Mutation can have dependencies injected using FastGraphQL.depende(...) as showed bellow:`

from fastgraphql import FastGraphQL
from pydantic import BaseModel
fast_graphql = FastGraphQL()

class Model(BaseModel):
    param: str

def create_dependency() -> str:
    return ""
    
@fast_graphql.graphql_query()
def my_first_query(
        model: Model = fast_graphql.graphql_query_field(),
        dependecy: str = fast_graphql.depends(create_dependency)
) -> str:
    ...

In this example the parameter dependecy will be injected once the query is called.

Integrations

Ariadne

...

FastAPI

...

Acknowledgment

Thanks FastAPI for inpirations

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastgraphql-0.0.3.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

fastgraphql-0.0.3-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file fastgraphql-0.0.3.tar.gz.

File metadata

  • Download URL: fastgraphql-0.0.3.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for fastgraphql-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c67816f61366bb0461c861628e37714ee4cd83091add17c44e6827028f49765d
MD5 2f3786d30d89258ab139b178c883f5f1
BLAKE2b-256 7fba02609d9a175498b89baf90ebd361c1e728cda4d663a1ba37496a50f3a327

See more details on using hashes here.

File details

Details for the file fastgraphql-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: fastgraphql-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for fastgraphql-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6fc5dce701bdb18932cf2214c475429e35dcb361f3dc1fe7443a2896961d06ec
MD5 67eba83df948b3804b2010b354284996
BLAKE2b-256 5aee5b57ca915a24e7ed1cfca30094264635752c41b71276e77dd8dca00261d0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page