-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from jhnnsrs/union-support
- Adds union support for both schema and document generation. - Also adds support for model options parameters
- Loading branch information
Showing
14 changed files
with
484 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ query Countries { | |
countries { | ||
phone | ||
capital | ||
emojiU | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
query Nana { | ||
hallo { | ||
... on Bar { | ||
nana | ||
} | ||
... on Foo { | ||
forward | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"This is foo" | ||
type Foo { | ||
"This is a forward ref" | ||
forward: String! | ||
} | ||
|
||
type Bar { | ||
"This is a forward ref" | ||
nana: Int! | ||
} | ||
|
||
union Element = Foo | Bar | ||
|
||
type Query { | ||
hallo: Element | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import ast | ||
|
||
import pytest | ||
from .utils import build_relative_glob, unit_test_with, ExecuteError | ||
from turms.config import GeneratorConfig, OptionsConfig | ||
from turms.plugins.funcs import ( | ||
FunctionDefinition, | ||
FuncsPlugin, | ||
FuncsPluginConfig, | ||
) | ||
from turms.run import generate_ast | ||
from turms.plugins.enums import EnumsPlugin | ||
from turms.plugins.inputs import InputsPlugin | ||
from turms.plugins.fragments import FragmentsPlugin | ||
from turms.plugins.operations import OperationsPlugin | ||
from turms.stylers.default import DefaultStyler | ||
from turms.helpers import build_schema_from_introspect_url | ||
|
||
|
||
@pytest.fixture() | ||
def countries_schema(): | ||
return build_schema_from_introspect_url("https://countries.trevorblades.com/") | ||
|
||
|
||
def test_allow_population_by_field_name(countries_schema): | ||
config = GeneratorConfig( | ||
documents=build_relative_glob("/documents/countries/**.graphql"), | ||
options=OptionsConfig( | ||
enabled=True, | ||
allow_population_by_field_name=True, | ||
) | ||
) | ||
|
||
generated_ast = generate_ast( | ||
config, | ||
countries_schema, | ||
stylers=[DefaultStyler()], | ||
plugins=[ | ||
EnumsPlugin(), | ||
InputsPlugin(), | ||
FragmentsPlugin(), | ||
OperationsPlugin(), | ||
], | ||
) | ||
|
||
md = ast.Module(body=generated_ast, type_ignores=[]) | ||
generated = ast.unparse(ast.fix_missing_locations(md)) | ||
unit_test_with(generated_ast, "Countries(countries=[CountriesCountries(emoji_u='soinsisn',phone='sdf', capital='dfsdf')]).countries[0].emoji_u") | ||
assert "from enum import Enum" in generated, "EnumPlugin not working" | ||
|
||
|
||
def test_extra_forbid(countries_schema): | ||
config = GeneratorConfig( | ||
documents=build_relative_glob("/documents/countries/**.graphql"), | ||
options=OptionsConfig( | ||
enabled=True, | ||
extra="forbid", | ||
) | ||
) | ||
|
||
generated_ast = generate_ast( | ||
config, | ||
countries_schema, | ||
stylers=[DefaultStyler()], | ||
plugins=[ | ||
EnumsPlugin(), | ||
InputsPlugin(), | ||
FragmentsPlugin(), | ||
OperationsPlugin(), | ||
], | ||
) | ||
|
||
md = ast.Module(body=generated_ast, type_ignores=[]) | ||
generated = ast.unparse(ast.fix_missing_locations(md)) | ||
with pytest.raises(ExecuteError): | ||
unit_test_with(generated_ast, "Countries(countries=[CountriesCountries(emojiU='soinsisn', phone='sdf', capital='dfsdf', hundi='soinsoin')]).countries[0].emoji_u") | ||
|
||
def test_extra_allow(countries_schema): | ||
config = GeneratorConfig( | ||
documents=build_relative_glob("/documents/countries/**.graphql"), | ||
options=OptionsConfig( | ||
enabled=True, | ||
extra="allow", | ||
) | ||
) | ||
|
||
generated_ast = generate_ast( | ||
config, | ||
countries_schema, | ||
stylers=[DefaultStyler()], | ||
plugins=[ | ||
EnumsPlugin(), | ||
InputsPlugin(), | ||
FragmentsPlugin(), | ||
OperationsPlugin(), | ||
], | ||
) | ||
|
||
md = ast.Module(body=generated_ast, type_ignores=[]) | ||
generated = ast.unparse(ast.fix_missing_locations(md)) | ||
unit_test_with(generated_ast, "Countries(countries=[CountriesCountries(emojiU='soinsisn', phone='sdf', capital='dfsdf', hundi='soinsoin')]).countries[0].emoji_u") | ||
|
||
|
||
def test_orm_mode(countries_schema): | ||
config = GeneratorConfig( | ||
documents=build_relative_glob("/documents/countries/**.graphql"), | ||
options=OptionsConfig( | ||
enabled=True, | ||
orm_mode=True, | ||
) | ||
) | ||
|
||
generated_ast = generate_ast( | ||
config, | ||
countries_schema, | ||
stylers=[DefaultStyler()], | ||
plugins=[ | ||
EnumsPlugin(), | ||
InputsPlugin(), | ||
FragmentsPlugin(), | ||
OperationsPlugin(), | ||
], | ||
) | ||
|
||
md = ast.Module(body=generated_ast, type_ignores=[]) | ||
generated = ast.unparse(ast.fix_missing_locations(md)) | ||
unit_test_with(generated_ast, "Countries(countries=[CountriesCountries(emojiU='soinsisn', phone='sdf', capital='dfsdf')]).countries[0].emoji_u") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import ast | ||
|
||
import pytest | ||
from .utils import build_relative_glob, unit_test_with | ||
from turms.config import GeneratorConfig | ||
from turms.run import generate_ast | ||
from turms.plugins.enums import EnumsPlugin | ||
from turms.plugins.inputs import InputsPlugin | ||
from turms.plugins.fragments import FragmentsPlugin | ||
from turms.plugins.operations import OperationsPlugin | ||
from turms.plugins.funcs import ( | ||
FunctionDefinition, | ||
FuncsPlugin, | ||
FuncsPluginConfig, | ||
) | ||
from turms.stylers.snake_case import SnakeCaseStyler | ||
from turms.stylers.capitalize import CapitalizeStyler | ||
from turms.helpers import build_schema_from_glob | ||
|
||
|
||
@pytest.fixture() | ||
def union_schema(): | ||
return build_schema_from_glob(build_relative_glob("/schemas/union.graphql")) | ||
|
||
|
||
def test_nested_input_funcs(union_schema): | ||
config = GeneratorConfig( | ||
documents=build_relative_glob("/documents/unions/*.graphql"), | ||
) | ||
generated_ast = generate_ast( | ||
config, | ||
union_schema, | ||
stylers=[CapitalizeStyler(), SnakeCaseStyler()], | ||
plugins=[ | ||
EnumsPlugin(), | ||
InputsPlugin(), | ||
FragmentsPlugin(), | ||
OperationsPlugin(), | ||
FuncsPlugin( | ||
config=FuncsPluginConfig( | ||
definitions=[ | ||
FunctionDefinition( | ||
type="mutation", | ||
use="mocks.aquery", | ||
is_async=False, | ||
), | ||
FunctionDefinition( | ||
type="mutation", | ||
use="mocks.aquery", | ||
is_async=True, | ||
), | ||
] | ||
), | ||
), | ||
], | ||
) | ||
|
||
unit_test_with(generated_ast, 'Nana(hallo={"__typename": "Foo","forward": "yes"}).hallo.forward') | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.