Python graphene.Schema() Examples
The following are 30
code examples of graphene.Schema().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
graphene
, or try the search function
.
Example #1
Source File: test_types.py From graphene-gae with BSD 3-Clause "New" or "Revised" License | 8 votes |
def testQuery_excludedField(self): Article(headline="h1", summary="s1").put() class ArticleType(NdbObjectType): class Meta: model = Article exclude_fields = ['summary'] class QueryType(graphene.ObjectType): articles = graphene.List(ArticleType) def resolve_articles(self, info): return Article.query() schema = graphene.Schema(query=QueryType) query = ''' query ArticlesQuery { articles { headline, summary } } ''' result = schema.execute(query) self.assertIsNotNone(result.errors) self.assertTrue('Cannot query field "summary"' in result.errors[0].message)
Example #2
Source File: test_util.py From graphql-over-kafka with MIT License | 6 votes |
def test_graphql_mutation_from_summary(self): # create a mock mutation summary mock_summary = summarize_crud_mutation(model=MockModelService(), method="delete") # create the mutation mutation = graphql_mutation_from_summary(mock_summary) # create a schema to test the mutation mock_schema = graphene.Schema() # get the corresponding object type mutation_object = mock_schema.T(mutation) mutation_fields = list(mutation_object.get_fields().keys()) # there should be one field named status assert mutation_fields == ['status'], ( "Delete mutation did not have correct output" )
Example #3
Source File: test_util.py From graphql-over-kafka with MIT License | 6 votes |
def test_graphql_mutation_with_object_io_from_summary(self): # create a mock mutation summary with a io that's an object mock_summary = summarize_crud_mutation(model=MockModelService(), method="create") # create the mutation mutation = graphql_mutation_from_summary(mock_summary) # create a schema to test the mutation mock_schema = graphene.Schema() # get the corresponding object type mutation_object = mock_schema.T(mutation) # make sure there is a resulting 'testModel' in the mutation assert 'testModel' in mutation_object.get_fields(), ( "Generated create mutation from summary does not have a service record in its output." ) # the fields of the mutation result output_fields = set(mutation_object.get_fields()['testModel'].type.get_fields().keys()) # make sure the object has the right types assert output_fields == {'date', 'name', 'id'}, ( "Mutation output did not have the correct fields." )
Example #4
Source File: test_types.py From graphene-gae with BSD 3-Clause "New" or "Revised" License | 6 votes |
def testQuery_onlyFields(self): Article(headline="h1", summary="s1").put() class ArticleType(NdbObjectType): class Meta: model = Article only_fields = ['headline'] class QueryType(graphene.ObjectType): articles = graphene.List(ArticleType) def resolve_articles(self, info): return Article.query() schema = graphene.Schema(query=QueryType) query = ''' query ArticlesQuery { articles { headline } } ''' result = schema.execute(query) self.assertIsNotNone(result.data) self.assertEqual(result.data['articles'][0]['headline'], 'h1') query = ''' query ArticlesQuery { articles { headline, summary } } ''' result = schema.execute(query) self.assertIsNotNone(result.errors) self.assertTrue('Cannot query field "summary"' in result.errors[0].message)
Example #5
Source File: test_313.py From graphene with MIT License | 6 votes |
def test_create_post(): query_string = """ mutation { createPost(text: "Try this out") { result { __typename } } } """ schema = graphene.Schema(query=Query, mutation=Mutations) result = schema.execute(query_string) assert not result.errors assert result.data["createPost"]["result"]["__typename"] == "Success"
Example #6
Source File: test_schema.py From graphql-over-kafka with MIT License | 6 votes |
def test_does_not_auto_camel_case(self): # a query to test with a snake case field class TestQuery(ObjectType): test_field = String() def resolve_test_field(self, args, info): return 'hello' # assign the query to the schema self.schema.query = TestQuery # the query to test test_query = "query {test_field}" # execute the query resolved_query = self.schema.execute(test_query) assert 'test_field' in resolved_query.data, ( "Schema did not have snake_case field." ) assert resolved_query.data['test_field'] == 'hello', ( "Snake_case field did not have the right value" )
Example #7
Source File: register_graphene_root_schema_hook.py From flask-unchained with MIT License | 6 votes |
def run_hook(self, app: FlaskUnchained, bundles: List[Bundle], unchained_config: Optional[Dict[str, Any]] = None, ) -> None: """ Create the root :class:`graphene.Schema` from queries, mutations, and types discovered by the other hooks and register it with the Graphene Bundle. """ mutations = tuple(self.bundle.mutations.values()) queries = tuple(self.bundle.queries.values()) types = list(self.bundle.types.values()) self.bundle.root_schema = graphene.Schema( query=queries and type('Queries', queries, {}) or None, mutation=mutations and type('Mutations', mutations, {}) or None, types=types or None)
Example #8
Source File: test_relay_query.py From graphene-mongo with MIT License | 6 votes |
def test_should_filter_by_id(fixtures): # Notes: https://goo.gl/hMNRgs class Query(graphene.ObjectType): reporter = Node.Field(nodes.ReporterNode) query = """ query ReporterQuery { reporter (id: "UmVwb3J0ZXJOb2RlOjE=") { id, firstName, awards } } """ expected = { "reporter": { "id": "UmVwb3J0ZXJOb2RlOjE=", "firstName": "Allen", "awards": ["2010-mvp"], } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #9
Source File: test_utils.py From graphene-sqlalchemy with MIT License | 6 votes |
def test_get_session(): session = "My SQLAlchemy session" class Query(ObjectType): x = String() def resolve_x(self, info): return get_session(info.context) query = """ query ReporterQuery { x } """ schema = Schema(query=Query) result = schema.execute(query, context_value={"session": session}) assert not result.errors assert result.data["x"] == session
Example #10
Source File: test_mutation.py From graphene-django with MIT License | 5 votes |
def test_model_form_mutation_creates_new(pet_type): class PetMutation(DjangoModelFormMutation): pet = Field(pet_type) class Meta: form_class = PetForm class Mutation(ObjectType): pet_mutation = PetMutation.Field() schema = Schema(query=MockQuery, mutation=Mutation) result = schema.execute( """ mutation PetMutation { petMutation(input: { name: "Mia", age: 10 }) { pet { name age } errors { field messages } } } """ ) assert result.errors is None assert result.data["petMutation"]["pet"] == {"name": "Mia", "age": 10} assert Pet.objects.count() == 1 pet = Pet.objects.get() assert pet.name == "Mia" assert pet.age == 10
Example #11
Source File: test_query.py From graphene-django with MIT License | 5 votes |
def test_should_query_list(): r1 = Reporter(last_name="ABA") r1.save() r2 = Reporter(last_name="Griffin") r2.save() class ReporterType(DjangoObjectType): class Meta: model = Reporter interfaces = (Node,) class Query(graphene.ObjectType): all_reporters = graphene.List(ReporterType) debug = graphene.Field(DjangoDebug, name="__debug") def resolve_all_reporters(self, info, **args): return Reporter.objects.all() query = """ query ReporterQuery { allReporters { lastName } __debug { sql { rawSql } } } """ expected = { "allReporters": [{"lastName": "ABA"}, {"lastName": "Griffin"}], "__debug": {"sql": [{"rawSql": str(Reporter.objects.all().query)}]}, } schema = graphene.Schema(query=Query) result = schema.execute( query, context_value=context(), middleware=[DjangoDebugMiddleware()] ) assert not result.errors assert result.data == expected
Example #12
Source File: test_types.py From graphene-django with MIT License | 5 votes |
def test_django_objecttype_convert_choices_enum_empty_list(self, PetModel): class Pet(DjangoObjectType): class Meta: model = PetModel convert_choices_to_enum = [] class Query(ObjectType): pet = Field(Pet) schema = Schema(query=Query) assert str(schema) == dedent( """\ schema { query: Query } type Pet { id: ID! kind: String! cuteness: Int! } type Query { pet: Pet } """ )
Example #13
Source File: test_types.py From graphene-django with MIT License | 5 votes |
def test_django_objecttype_convert_choices_enum_list(self, PetModel): class Pet(DjangoObjectType): class Meta: model = PetModel convert_choices_to_enum = ["kind"] class Query(ObjectType): pet = Field(Pet) schema = Schema(query=Query) assert str(schema) == dedent( """\ schema { query: Query } type Pet { id: ID! kind: PetModelKind! cuteness: Int! } enum PetModelKind { CAT DOG } type Query { pet: Pet } """ )
Example #14
Source File: test_types.py From graphene-django with MIT License | 5 votes |
def test_django_objecttype_convert_choices_enum_false(self, PetModel): class Pet(DjangoObjectType): class Meta: model = PetModel convert_choices_to_enum = False class Query(ObjectType): pet = Field(Pet) schema = Schema(query=Query) assert str(schema) == dedent( """\ schema { query: Query } type Pet { id: ID! kind: String! cuteness: Int! } type Query { pet: Pet } """ )
Example #15
Source File: test_fields.py From graphene-django with MIT License | 5 votes |
def test_list_field_default_queryset(self): class Reporter(DjangoObjectType): class Meta: model = ReporterModel fields = ("first_name",) class Query(ObjectType): reporters = DjangoListField(Reporter) schema = Schema(query=Query) query = """ query { reporters { firstName } } """ ReporterModel.objects.create(first_name="Tara", last_name="West") ReporterModel.objects.create(first_name="Debra", last_name="Payne") result = schema.execute(query) assert not result.errors assert result.data == { "reporters": [{"firstName": "Tara"}, {"firstName": "Debra"}] }
Example #16
Source File: test_multiple_model_serializers.py From graphene-django with MIT License | 5 votes |
def test_create_schema(): schema = Schema(mutation=Mutation, types=[ParentType, ChildType]) assert schema
Example #17
Source File: test_mutation.py From graphene-django with MIT License | 5 votes |
def test_model_form_mutation_invalid_input(pet_type): class PetMutation(DjangoModelFormMutation): pet = Field(pet_type) class Meta: form_class = PetForm class Mutation(ObjectType): pet_mutation = PetMutation.Field() schema = Schema(query=MockQuery, mutation=Mutation) result = schema.execute( """ mutation PetMutation { petMutation(input: { name: "Mia", age: 99 }) { pet { name age } errors { field messages } } } """ ) assert result.errors is None assert result.data["petMutation"]["pet"] is None assert result.data["petMutation"]["errors"] == [ {"field": "age", "messages": ["Too old"]} ] assert Pet.objects.count() == 0
Example #18
Source File: test_schema.py From graphql-over-kafka with MIT License | 5 votes |
def setUp(self): # create a nautilus schema to test self.schema = nautilus.api.Schema() # create an ioloop to use self.io_loop = self.get_new_ioloop()
Example #19
Source File: create_model_schema.py From graphql-over-kafka with MIT License | 5 votes |
def create_model_schema(target_model): """ This function creates a graphql schema that provides a single model """ from nautilus.database import db # create the schema instance schema = graphene.Schema(auto_camelcase=False) # grab the primary key from the model primary_key = target_model.primary_key() primary_key_type = convert_peewee_field(primary_key) # create a graphene object class ModelObjectType(PeeweeObjectType): class Meta: model = target_model pk = Field(primary_key_type, description="The primary key for this object.") @graphene.resolve_only_args def resolve_pk(self): return getattr(self, self.primary_key().name) class Query(graphene.ObjectType): """ the root level query """ all_models = List(ModelObjectType, args=args_for_model(target_model)) @graphene.resolve_only_args def resolve_all_models(self, **args): # filter the model query according to the arguments # print(filter_model(target_model, args)[0].__dict__) return filter_model(target_model, args) # add the query to the schema schema.query = Query return schema
Example #20
Source File: test_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_custom_kwargs(fixtures): class Query(graphene.ObjectType): editors = graphene.List(types.EditorType, first=graphene.Int()) def resolve_editors(self, *args, **kwargs): editors = models.Editor.objects() if "first" in kwargs: editors = editors[: kwargs["first"]] return list(editors) query = """ query EditorQuery { editors(first: 2) { firstName, lastName } } """ expected = { "editors": [ {"firstName": "Penny", "lastName": "Hardaway"}, {"firstName": "Grant", "lastName": "Hill"}, ] } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #21
Source File: test_relay_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_filter_mongoengine_queryset(fixtures): class Query(graphene.ObjectType): players = MongoengineConnectionField(nodes.PlayerNode) query = """ query players { players(firstName_Istartswith: "M") { edges { node { firstName } } } } """ expected = { "players": { "edges": [ {"node": {"firstName": "Michael"}}, {"node": {"firstName": "Magic"}}, ] } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert json.dumps(result.data, sort_keys=True) == json.dumps( expected, sort_keys=True )
Example #22
Source File: test_relay_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_get_queryset_returns_dict_filters(fixtures): class Query(graphene.ObjectType): node = Node.Field() articles = MongoengineConnectionField( nodes.ArticleNode, get_queryset=lambda *_, **__: {"headline": "World"} ) query = """ query ArticlesQuery { articles { edges { node { headline, pubDate, editor { firstName } } } } } """ expected = { "articles": { "edges": [ { "node": { "headline": "World", "editor": {"firstName": "Grant"}, "pubDate": "2020-01-01T00:00:00", } } ] } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #23
Source File: test_relay_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_query_with_embedded_document(fixtures): class Query(graphene.ObjectType): professors = MongoengineConnectionField(nodes.ProfessorVectorNode) query = """ query { professors { edges { node { vec, metadata { firstName } } } } } """ expected = { "professors": { "edges": [ {"node": {"vec": [1.0, 2.3], "metadata": {"firstName": "Steven"}}} ] } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #24
Source File: test_relay_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_before(fixtures): class Query(graphene.ObjectType): players = MongoengineConnectionField(nodes.PlayerNode) query = """ query EditorQuery { players(before: "YXJyYXljb25uZWN0aW9uOjI=") { edges { cursor, node { firstName } } } } """ expected = { "players": { "edges": [ { "cursor": "YXJyYXljb25uZWN0aW9uOjA=", "node": {"firstName": "Michael"}, }, {"cursor": "YXJyYXljb25uZWN0aW9uOjE=", "node": {"firstName": "Magic"}}, ] } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #25
Source File: test_relay_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_after(fixtures): class Query(graphene.ObjectType): players = MongoengineConnectionField(nodes.PlayerNode) query = """ query EditorQuery { players(after: "YXJyYXljb25uZWN0aW9uOjA=") { edges { cursor, node { firstName } } } } """ expected = { "players": { "edges": [ {"cursor": "YXJyYXljb25uZWN0aW9uOjE=", "node": {"firstName": "Magic"}}, {"cursor": "YXJyYXljb25uZWN0aW9uOjI=", "node": {"firstName": "Larry"}}, {"cursor": "YXJyYXljb25uZWN0aW9uOjM=", "node": {"firstName": "Chris"}}, ] } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #26
Source File: test_relay_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_filter_by_reference_field(fixtures): class Query(graphene.ObjectType): articles = MongoengineConnectionField(nodes.ArticleNode) query = """ query ArticlesQuery { articles(editor: "RWRpdG9yTm9kZTox") { edges { node { headline, editor { firstName } } } } } """ expected = { "articles": { "edges": [{"node": {"headline": "Hello", "editor": {"firstName": "Penny"}}}] } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #27
Source File: test_relay_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_filter(fixtures): class Query(graphene.ObjectType): articles = MongoengineConnectionField(nodes.ArticleNode) query = """ query ArticlesQuery { articles(headline: "World") { edges { node { headline, pubDate, editor { firstName } } } } } """ expected = { "articles": { "edges": [ { "node": { "headline": "World", "editor": {"firstName": "Grant"}, "pubDate": "2020-01-01T00:00:00", } } ] } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #28
Source File: test_relay_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_filter_editors_by_id(fixtures): class Query(graphene.ObjectType): editors = MongoengineConnectionField(nodes.EditorNode) query = """ query EditorQuery { editors(id: "RWRpdG9yTm9kZToy") { edges { node { id, firstName, lastName } } } } """ expected = { "editors": { "edges": [ { "node": { "id": "RWRpdG9yTm9kZToy", "firstName": "Grant", "lastName": "Hill", } } ] } } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #29
Source File: test_query.py From graphene-mongo with MIT License | 5 votes |
def test_should_query_child(fixtures): class Query(graphene.ObjectType): children = graphene.List(types.ChildType) def resolve_children(self, *args, **kwargs): return list(models.Child.objects.all()) query = """ query Query { children { bar, baz, loc { type, coordinates } } } """ expected = { "children": [ {"bar": "BAR", "baz": "BAZ", "loc": None}, { "bar": "bar", "baz": "baz", "loc": {"type": "Point", "coordinates": [10.0, 20.0]}, }, ] } schema = graphene.Schema(query=Query) result = schema.execute(query) assert not result.errors assert result.data == expected
Example #30
Source File: admin.py From backend.ai-manager with GNU Lesser General Public License v3.0 | 5 votes |
def init(app: web.Application) -> None: app['admin.gql_executor'] = AsyncioExecutor() app['admin.gql_schema'] = graphene.Schema( query=Queries, mutation=Mutations, auto_camelcase=False)