Ariadne is a Python library for implementing GraphQL servers using a schema-first approach.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Essential functionality for creating executable GraphQL schemas from type definitions and binding Python logic to schema elements. This is the foundation of Ariadne's schema-first approach.
Creates a GraphQLSchema instance from SDL type definitions and bindable objects that provide Python logic for the schema.
def make_executable_schema(
type_defs: Union[str, list[str]],
*bindables: Union[SchemaBindable, type[Enum], list[Union[SchemaBindable, type[Enum]]]],
directives: Optional[dict[str, type[SchemaDirectiveVisitor]]] = None,
convert_names_case: Union[bool, SchemaNameConverter] = False,
) -> GraphQLSchema:
"""
Create a GraphQLSchema instance that can be used to execute queries.
Parameters:
- type_defs: SDL string or list of SDL strings defining GraphQL schema
- bindables: Instances of schema bindables (ObjectType, ScalarType, etc.)
- directives: Dict mapping directive names to SchemaDirectiveVisitor classes
- convert_names_case: Enable automatic case conversion between camelCase and snake_case
Returns:
GraphQLSchema instance with Python logic bound to schema elements
"""Loads GraphQL schema definitions from .graphql files on disk.
def load_schema_from_path(path: str) -> str:
"""
Load GraphQL schema from .graphql, .gql, or .graphqls files.
Parameters:
- path: Path to file or directory containing GraphQL schema files
Returns:
Combined SDL string from all loaded schema files
"""Helper function that provides GraphQL syntax validation and improved error messages.
def gql(string: str) -> str:
"""
Validate GraphQL SDL string and provide enhanced error messages.
Parameters:
- string: GraphQL SDL string to validate
Returns:
The same SDL string if validation passes
Raises:
GraphQLSyntaxError: If SDL contains syntax errors
"""Utilities for converting field names between camelCase (GraphQL convention) and snake_case (Python convention).
class SchemaNameConverter:
"""Base class for schema name conversion strategies."""
def convert_schema_names(self, schema: GraphQLSchema) -> GraphQLSchema: ...
def convert_schema_names(
schema: GraphQLSchema,
name_converter: SchemaNameConverter
) -> GraphQLSchema:
"""
Apply name conversion to existing schema.
Parameters:
- schema: GraphQL schema to convert
- name_converter: Strategy for name conversion
Returns:
New schema with converted names
"""
def convert_camel_case_to_snake(name: str) -> str:
"""Convert camelCase string to snake_case."""
def convert_kwargs_to_snake_case(func: Callable) -> Callable:
"""Decorator that converts camelCase kwargs to snake_case."""
def type_implements_interface(
graphql_type: GraphQLObjectType,
interface: GraphQLInterfaceType
) -> bool:
"""Check if GraphQL object type implements specific interface."""Utilities for handling enum default values in GraphQL schemas.
def repair_schema_default_enum_values(schema: GraphQLSchema) -> GraphQLSchema:
"""
Repair enum default values in schema to ensure GraphQL compliance.
Parameters:
- schema: GraphQL schema to repair
Returns:
Schema with corrected enum default values
"""
def validate_schema_default_enum_values(schema: GraphQLSchema) -> None:
"""
Validate that enum default values in schema are valid.
Parameters:
- schema: GraphQL schema to validate
Raises:
ValueError: If invalid enum default values are found
"""from ariadne import gql, make_executable_schema, ObjectType
# Define schema using SDL
type_defs = gql("""
type Query {
hello: String!
user(id: ID!): User
}
type User {
id: ID!
name: String!
email: String!
}
""")
# Create bindables
query = ObjectType("Query")
@query.field("hello")
def resolve_hello(*_):
return "Hello, World!"
@query.field("user")
def resolve_user(_, info, id):
return {"id": id, "name": "John Doe", "email": "john@example.com"}
# Create executable schema
schema = make_executable_schema(type_defs, query)from ariadne import load_schema_from_path, make_executable_schema
# Load schema from .graphql files
type_defs = load_schema_from_path("./schema/")
# Create schema with loaded definitions
schema = make_executable_schema(type_defs, query, user_type)from ariadne import make_executable_schema, SnakeCaseFallbackResolversSetter
# Enable automatic camelCase to snake_case conversion
schema = make_executable_schema(
type_defs,
query,
convert_names_case=True # or pass custom SchemaNameConverter
)
# Using snake_case fallback resolvers
schema = make_executable_schema(
type_defs,
query,
SnakeCaseFallbackResolversSetter()
)Install with Tessl CLI
npx tessl i tessl/pypi-ariadne