GraphQL Modules for Ariadne
Project description
Ariadne GraphQL Modules
Ariadne package for implementing Ariadne GraphQL schemas using modular approach.
For reasoning behind this work, please see this GitHub discussion.
See API reference file for documentation.
Examples
Basic example
from datetime import date
from ariadne.asgi import GraphQL, gql
from ariadne_graphql_modules import ObjectType, make_executable_schema
class Query(ObjectType):
__schema__ = gql(
"""
type Query {
message: String!
year: Int!
}
"""
)
def resolve_message(*_):
return "Hello world!"
def resolve_year(*_):
return date.today().year
schema = make_executable_schema(Query)
app = GraphQL(schema=schema, debug=True)
Dependency injection
If __schema__
string contains other type, its definition should be provided via __requires__
attribute:
from typing import List, Optional
from ariadne.asgi import GraphQL, gql
from ariadne_graphql_modules import ObjectType, make_executable_schema
from my_app.users import User, get_user, get_last_users
class UserType(ObjectType):
__schema__ = gql(
"""
type User {
id: ID!
name: String!
email: String
}
"""
)
def resolve_email(user: User, info):
if info.context["is_admin"]:
return user.email
return None
class UsersQueries(ObjectType):
__schema__ = gql(
"""
type Query {
user(id: ID!): User
users: [User!]!
}
"""
)
__requires__ = [UserType]
def resolve_user(*_, id: string) -> Optional[User]:
return get_user(id=id)
def resolve_users(*_, id: string) -> List[User]:
return get_last_users()
# UsersQueries already knows about `UserType` so it can be omitted
# in make_executable_schema arguments
schema = make_executable_schema(UsersQueries)
app = GraphQL(schema=schema, debug=True)
Deferred dependencies
Optionally dependencies can be declared as deferred so they can be provided directly to make_executable_schema
:
from typing import List, Optional
from ariadne.asgi import GraphQL, gql
from ariadne_graphql_modules import DeferredType, ObjectType, make_executable_schema
from my_app.users import User, get_user, get_last_users
class UserType(ObjectType):
__schema__ = gql(
"""
type User {
id: ID!
name: String!
email: String
}
"""
)
def resolve_email(user: User, info):
if info.context["is_admin"]:
return user.email
return None
class UsersQueries(ObjectType):
__schema__ = gql(
"""
type Query {
user(id: ID!): User
users: [User!]!
}
"""
)
__requires__ = [DeferredType("User")]
def resolve_user(*_, id: string) -> Optional[User]:
return get_user(id=id)
def resolve_users(*_, id: string) -> List[User]:
return get_last_users()
schema = make_executable_schema(UserType, UsersQueries)
app = GraphQL(schema=schema, debug=True)
Automatic case convertion between python_world
and clientWorld
Resolving fields values
Use __aliases__ = convert_case
to automatically set aliases for fields that convert case
from ariadne.asgi import gql
from ariadne_graphql_modules import ObjectType, convert_case
class UserType(ObjectType):
__schema__ = gql(
"""
type User {
id: ID!
fullName: String!
}
"""
)
__aliases__ = convert_case
Converting fields arguments
Use __fields_args__ = convert_case
on type to automatically convert field arguments to python case in resolver kwargs:
from ariadne.asgi import gql
from ariadne_graphql_modules import MutationType, convert_case
from my_app import create_user
class UserRegisterMutation(MutationType):
__schema__ = gql(
"""
type Mutation {
registerUser(fullName: String!, email: String!): Boolean!
}
"""
)
__fields_args__ = convert_case
async def resolve_mutation(*_, full_name: str, email: str):
user = await create_user(
full_name=full_name,
email=email,
)
return bool(user)
Converting inputs fields
Use __args__ = convert_case
on type to automatically convert input fields to python case in resolver kwargs:
from ariadne.asgi import gql
from ariadne_graphql_modules import InputType, MutationType, convert_case
from my_app import create_user
class UserRegisterInput(InputType):
__schema__ = gql(
"""
input UserRegisterInput {
fullName: String!
email: String!
}
"""
)
__args__ = convert_case
class UserRegisterMutation(MutationType):
__schema__ = gql(
"""
type Mutation {
registerUser(input: UserRegisterInput!): Boolean!
}
"""
)
__requires__ = [UserRegisterInput]
async def resolve_mutation(*_, input: dict):
user = await create_user(
full_name=input["full_name"],
email=input["email"],
)
return bool(user)
Roots merging
Query
, Mutation
and Subscription
types are automatically merged into one by make_executable_schema
:
from datetime import date
from ariadne.asgi import GraphQL, gql
from ariadne_graphql_modules import ObjectType, make_executable_schema
class YearQuery(ObjectType):
__schema__ = gql(
"""
type Query {
year: Int!
}
"""
)
def resolve_year(*_):
return date.today().year
class MessageQuery(ObjectType):
__schema__ = gql(
"""
type Query {
message: String!
}
"""
)
def resolve_message(*_):
return "Hello world!"
schema = make_executable_schema(YearQuery, MessageQuery)
app = GraphQL(schema=schema, debug=True)
Final schema will contain single Query
type thats result of merged tupes:
type Query {
message: String!
year: Int!
}
Fields on final type will be ordered alphabetically.
Contributing
We are welcoming contributions to Ariadne! If you've found a bug or issue, feel free to use GitHub issues. If you have any questions or feedback, don't hesitate to catch us on GitHub discussions.
For guidance and instructions, please see CONTRIBUTING.md.
Website and the docs have their own GitHub repository: mirumee/ariadne-website
Also make sure you follow @AriadneGraphQL on Twitter for latest updates, news and random musings!
Crafted with ❤️ by Mirumee Software hello@mirumee.com
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ariadne-graphql-modules-0.2.0.dev2.tar.gz
.
File metadata
- Download URL: ariadne-graphql-modules-0.2.0.dev2.tar.gz
- Upload date:
- Size: 14.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5a48926da32a9bbcb76f9b53cf4056d2f7bce5af6514f09067a3ead83476b033 |
|
MD5 | 330911ffb6dbd5679e56ef5e51429070 |
|
BLAKE2b-256 | 5f3401ce055f5bc0fe13cca90369bb53a43023eff074c4e2f04515be237f0fb2 |
File details
Details for the file ariadne_graphql_modules-0.2.0.dev2-py3-none-any.whl
.
File metadata
- Download URL: ariadne_graphql_modules-0.2.0.dev2-py3-none-any.whl
- Upload date:
- Size: 21.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | afa6553ea4bd05948449eca514f363e95566cf768a17d0f1e16748de5d3441f3 |
|
MD5 | f73b2f77cbee6a5729eb045adb6dd455 |
|
BLAKE2b-256 | 6e70e97b33832e0dbcf9b707f68d130cae981255b3fdd0c451a0fd96fa8f17e0 |