0
# Core Schema Creation
1
2
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.
3
4
## Capabilities
5
6
### Schema Creation
7
8
Creates a GraphQLSchema instance from SDL type definitions and bindable objects that provide Python logic for the schema.
9
10
```python { .api }
11
def make_executable_schema(
12
type_defs: Union[str, list[str]],
13
*bindables: Union[SchemaBindable, type[Enum], list[Union[SchemaBindable, type[Enum]]]],
14
directives: Optional[dict[str, type[SchemaDirectiveVisitor]]] = None,
15
convert_names_case: Union[bool, SchemaNameConverter] = False,
16
) -> GraphQLSchema:
17
"""
18
Create a GraphQLSchema instance that can be used to execute queries.
19
20
Parameters:
21
- type_defs: SDL string or list of SDL strings defining GraphQL schema
22
- bindables: Instances of schema bindables (ObjectType, ScalarType, etc.)
23
- directives: Dict mapping directive names to SchemaDirectiveVisitor classes
24
- convert_names_case: Enable automatic case conversion between camelCase and snake_case
25
26
Returns:
27
GraphQLSchema instance with Python logic bound to schema elements
28
"""
29
```
30
31
### Schema Loading
32
33
Loads GraphQL schema definitions from .graphql files on disk.
34
35
```python { .api }
36
def load_schema_from_path(path: str) -> str:
37
"""
38
Load GraphQL schema from .graphql, .gql, or .graphqls files.
39
40
Parameters:
41
- path: Path to file or directory containing GraphQL schema files
42
43
Returns:
44
Combined SDL string from all loaded schema files
45
"""
46
```
47
48
### Schema Validation
49
50
Helper function that provides GraphQL syntax validation and improved error messages.
51
52
```python { .api }
53
def gql(string: str) -> str:
54
"""
55
Validate GraphQL SDL string and provide enhanced error messages.
56
57
Parameters:
58
- string: GraphQL SDL string to validate
59
60
Returns:
61
The same SDL string if validation passes
62
63
Raises:
64
GraphQLSyntaxError: If SDL contains syntax errors
65
"""
66
```
67
68
### Name Conversion
69
70
Utilities for converting field names between camelCase (GraphQL convention) and snake_case (Python convention).
71
72
```python { .api }
73
class SchemaNameConverter:
74
"""Base class for schema name conversion strategies."""
75
def convert_schema_names(self, schema: GraphQLSchema) -> GraphQLSchema: ...
76
77
def convert_schema_names(
78
schema: GraphQLSchema,
79
name_converter: SchemaNameConverter
80
) -> GraphQLSchema:
81
"""
82
Apply name conversion to existing schema.
83
84
Parameters:
85
- schema: GraphQL schema to convert
86
- name_converter: Strategy for name conversion
87
88
Returns:
89
New schema with converted names
90
"""
91
92
def convert_camel_case_to_snake(name: str) -> str:
93
"""Convert camelCase string to snake_case."""
94
95
def convert_kwargs_to_snake_case(func: Callable) -> Callable:
96
"""Decorator that converts camelCase kwargs to snake_case."""
97
98
def type_implements_interface(
99
graphql_type: GraphQLObjectType,
100
interface: GraphQLInterfaceType
101
) -> bool:
102
"""Check if GraphQL object type implements specific interface."""
103
```
104
105
### Enum Default Values
106
107
Utilities for handling enum default values in GraphQL schemas.
108
109
```python { .api }
110
def repair_schema_default_enum_values(schema: GraphQLSchema) -> GraphQLSchema:
111
"""
112
Repair enum default values in schema to ensure GraphQL compliance.
113
114
Parameters:
115
- schema: GraphQL schema to repair
116
117
Returns:
118
Schema with corrected enum default values
119
"""
120
121
def validate_schema_default_enum_values(schema: GraphQLSchema) -> None:
122
"""
123
Validate that enum default values in schema are valid.
124
125
Parameters:
126
- schema: GraphQL schema to validate
127
128
Raises:
129
ValueError: If invalid enum default values are found
130
"""
131
```
132
133
## Usage Examples
134
135
### Basic Schema Creation
136
137
```python
138
from ariadne import gql, make_executable_schema, ObjectType
139
140
# Define schema using SDL
141
type_defs = gql("""
142
type Query {
143
hello: String!
144
user(id: ID!): User
145
}
146
147
type User {
148
id: ID!
149
name: String!
150
email: String!
151
}
152
""")
153
154
# Create bindables
155
query = ObjectType("Query")
156
157
@query.field("hello")
158
def resolve_hello(*_):
159
return "Hello, World!"
160
161
@query.field("user")
162
def resolve_user(_, info, id):
163
return {"id": id, "name": "John Doe", "email": "john@example.com"}
164
165
# Create executable schema
166
schema = make_executable_schema(type_defs, query)
167
```
168
169
### Loading Schema from Files
170
171
```python
172
from ariadne import load_schema_from_path, make_executable_schema
173
174
# Load schema from .graphql files
175
type_defs = load_schema_from_path("./schema/")
176
177
# Create schema with loaded definitions
178
schema = make_executable_schema(type_defs, query, user_type)
179
```
180
181
### Automatic Case Conversion
182
183
```python
184
from ariadne import make_executable_schema, SnakeCaseFallbackResolversSetter
185
186
# Enable automatic camelCase to snake_case conversion
187
schema = make_executable_schema(
188
type_defs,
189
query,
190
convert_names_case=True # or pass custom SchemaNameConverter
191
)
192
193
# Using snake_case fallback resolvers
194
schema = make_executable_schema(
195
type_defs,
196
query,
197
SnakeCaseFallbackResolversSetter()
198
)
199
```