Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobmoshipco committed Sep 27, 2024
0 parents commit bb0152a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

.venv/
**/__pycache__

22 changes: 22 additions & 0 deletions field_directive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@


from fastapi import FastAPI
import strawberry
from strawberry.schema_directive import Location
from strawberry.fastapi import GraphQLRouter

@strawberry.schema_directive(locations=[Location.FIELD_DEFINITION])
class TestDirective: pass

@strawberry.type
class Query:
@strawberry.field(directives=[TestDirective()])
def test() -> str:
return "Test"

schema = strawberry.Schema(Query)

router = GraphQLRouter(schema, prefix='/graphql', graphql_ide="apollo-sandbox")
app = FastAPI()
app.include_router(router)

48 changes: 48 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

import traceback
from typing import Callable
from strawberry.cli.utils import load_schema
from strawberry.printer import print_schema

DIRECTIVE_NAMES_QUERY = 'query { __schema { directives { name } } }'

def test_field_directive_schema_export():
schema_symbol = load_schema("field_directive:schema", ".")
output = print_schema(schema_symbol)
assert "testDirective" in output

def test_one_of_schema_export():
schema_symbol = load_schema("one_of_directive:schema", ".")
output = print_schema(schema_symbol)
assert "oneOf" in output

def test_field_directive_introspection():
from field_directive import schema
result = schema.execute_sync(DIRECTIVE_NAMES_QUERY)
directives = { directive['name'] for directive in result.data['__schema']['directives'] }
assert "testDirective" in directives

def test_one_of_introspection():
from one_of_directive import schema
result = schema.execute_sync(DIRECTIVE_NAMES_QUERY)
directives = { directive['name'] for directive in result.data['__schema']['directives'] }
assert "oneOf" in directives


def run_test(test: Callable):
print("\n" + test.__name__.replace('_', ' ') + ": ", end="")
try:
test()
print("PASS")
except:
print("FAIL")
traceback.print_exc()

def run_tests():
run_test(test_field_directive_schema_export)
run_test(test_one_of_schema_export)
run_test(test_field_directive_introspection)
run_test(test_one_of_introspection)

if __name__ == '__main__':
run_tests()
23 changes: 23 additions & 0 deletions one_of_directive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@


from fastapi import FastAPI
import strawberry
from strawberry.fastapi import GraphQLRouter

@strawberry.input(one_of=True)
class TestInput:
a: str | None = strawberry.UNSET
b: str | None = strawberry.UNSET

@strawberry.type
class Query:
@strawberry.field
def test(x: TestInput) -> str:
return "Test"

schema = strawberry.Schema(Query)

router = GraphQLRouter(schema, prefix='/graphql', graphql_ide="apollo-sandbox")
app = FastAPI()
app.include_router(router)

25 changes: 25 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
annotated-types==0.7.0
anyio==4.6.0
click==8.1.7
fastapi==0.115.0
graphql-core==3.2.4
h11==0.14.0
idna==3.10
libcst==1.4.0
markdown-it-py==3.0.0
mdurl==0.1.2
pydantic==2.9.2
pydantic_core==2.23.4
Pygments==2.18.0
python-dateutil==2.9.0.post0
python-multipart==0.0.10
PyYAML==6.0.2
rich==13.8.1
shellingham==1.5.4
six==1.16.0
sniffio==1.3.1
starlette==0.38.6
strawberry-graphql==0.243.1
typer==0.12.5
typing_extensions==4.12.2
uvicorn==0.30.6

0 comments on commit bb0152a

Please sign in to comment.