{ "info": { "author": "Jo\u00e3o Vitor Silvestre", "author_email": "joaovitorsilvestresousa@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "# GrapheneMongo\n\nGrapheneMongodb is a library that integrates Graphene with MongoEngine\n  [![Build Status](https://travis-ci.org/joaovitorsilvestre/graphene-mongo.svg?branch=master)](https://travis-ci.org/joaovitorsilvestre/graphene-mongo)\n[![Coverage Status](https://coveralls.io/repos/github/joaovitorsilvestre/graphene-mongo/badge.svg?branch=master)](https://coveralls.io/github/joaovitorsilvestre/graphene-mongo?branch=master)\n
\n\n### Examples\nGiven that mongoengine Document:\n```python\nclass User(Document):\n username = StringField()\n creation_date = DateTimeField()\n favourite_color = ListField(StringField())\n```\nTo generate a graphene schema for that Document we create a class that is subclass of graphene_mongodb.MongoSchema, or we can also just call it passing the model as first argument:\n```python\nfrom graphene_mongodb import MongoSchema\n\nclass UserSchema(MongoSchema):\n model = User\n# OR\nUserSchema = MongoSchema(User)\n```\nThe schema now it's generated. Now it's necessary to create a graphene object Query:\n```python\nimport graphene\n\nclass Query(graphene.ObjectType):\n user = UserSchema.single\n\nschema = graphene.Schema(query=Query)\n\n# now we can do the query:\nresult = schema.execute(\"\"\"\nquery Data {\n user(username: \"John\") {\n id\n username\n }\n}\"\"\")\n```\n\nYou may notice the UserSchema.single attribute in the example above, the class UserSchema has many other attributes. All they are explained below:\n\n| Attribute | Description |\n| ------------- | ------------- |\n| single | We use single we want that the query result be a unique result. That's the same that make the query in mongoengine calling .first() to get the first object that matches the query. |\n| list | List is used when we want a list of the documents that matches the query. |\n| model | That's easy, this attribute stores the original Document of mongoengine that you created. |\n| fields | This field is more consult, you can use the fields that was converted from mongoengine to graphene. For instance, in our UserSchema class the attribute field will be a dict like this: {'username': graphene.String}|\n| mutation | Mutate is the attribute that we use when creating Mutations with graphene. See more in [Mutations](#mutations) |\n\n
\n\n## Mutations\n\nSometimes we need to save new data in mongodb instead of doing queries. Mutations do that job.\nLet's use again the UserSchema that we created in the examples. As before we create a graphene object called Query to handle the query, and we now need to do the same to a Mutation:\n\n```python\nclass Mutation(graphene.ObjectType):\n create_user = UserSchema.mutate\n\nschema = graphene.Schema(query=Query, mutation=Mutation)\n```\nNotice that we updated the variable schema to have a mutation object too.\nNow we can do the mutation query and create a new user in our database:\n```python\nresult = schema.execute(\"\"\"\nmutation testMutation {\n createUser(username:\"John\") {\n person {\n id\n username\n }\n }\n}\"\"\")\n```\n\nWe pass the attributes that we want to save in the 'params', as we did in \"...createPerson(username:\"John\")...\"\n\nIn this example GrapheneMongodb handled the save of the object for you, but sometimes you need to make validations before actually saving the object to the database. How you can do that is explained next.\n\n### Verifications before save\nTo use your own function for saving, you need to create a function in the MongoSchema class called mutate. Said that, lets update our UserSchema as follows:\n```python\nclass UserSchema(MongoSchema):\n model = User\n\n @staticmethod\n def mutate(args, context):\n \tnew_user = User(**args)\n new_user.creation_date = datetime.now()\n new_user.save()\n return new_user\n```\n\nThere's not many rules here, you only need to be sure that the method receives two parameters, has the static method decorator and returns the instance of the object.\n\nThe context parameter has the request object of the framework that you're using. If you're using flask for instance, that parameter will be the flask global request.\n\n\n\n## Operators in query\n\nMongoengine offers many kinds of operators to use as 'in', 'gte', etc. See all operators in mongoengine documentation. With GrapheneMongodb you can use them in your query:\n```python\nresult = schema.execute(\"\"\"\nquery Data {\n user(username_Icontains: \"John\", creationDate_Gte:\"1997-04-28\", favouriteColor_In:[\"red\"]) {\n id\n username\n }\n}\"\"\")\n```\n\nThe best is that they are all supported by GrapheneMongodb.\n\n\n## Validate permissions to do queries and mutations\nTo validate if a user can access some field you can create an attribute called validator:\n```python\nclass UserSchema(MongoSchema):\n model = User\n\n def validator(model, fields, query, special_params):\n \tif 'not_alowed_field' in fields:\n \traise Exception('Unauthorized Access')\n```\n\nWhen a validator function exists, GrapheneMongodb will call it in every query or mutation of the respective schema.\nJust be sure to raise a exception that graphene will be responsible to send back to graphql client in the frontend.\n\n
\n
\n\n### TODOs\n* Accept user mutation return None;\n* Support ReferenceField of 'self' without raising 'maximum recursion' error.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "https://github.com/joaovitorsilvestre/graphene-mongo/archive/1.0.1.tar.gz", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/joaovitorsilvestre/graphene-mongo", "keywords": "graphene,graphql,mongo,mongodb,mongoDB,mongoengine,graphene-mongo,graphene-mongodb", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "graphene-mongodb", "package_url": "https://pypi.org/project/graphene-mongodb/", "platform": "", "project_url": "https://pypi.org/project/graphene-mongodb/", "project_urls": { "Download": "https://github.com/joaovitorsilvestre/graphene-mongo/archive/1.0.1.tar.gz", "Homepage": "https://github.com/joaovitorsilvestre/graphene-mongo" }, "release_url": "https://pypi.org/project/graphene-mongodb/1.0.1/", "requires_dist": [ "aniso8601 (>=3.0.0)", "appdirs (>=1.4.3)", "graphene (>=2.1)", "graphql-core (>=2.0)", "graphql-relay (>=0.4.5)", "iso8601 (>=0.1.12)", "mongoengine (>=0.15.0)", "promise (>=2.1)", "pymongo (>=3.6.1)", "pyparsing (>=2.2.0)", "rx (>=1.6.1)", "six (>=1.11.0)", "typing (>=3.6.4)" ], "requires_python": "", "summary": "GrapheneMongo is a library that integrates Graphene with MongoEngine", "version": "1.0.1" }, "last_serial": 3820944, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "16a847341fc1883816e35b4f2becda5f", "sha256": "4d5b41f91d6fc1db8bb0a24a73e19ad1e69827488d49cabf3b048476cd30f1d2" }, "downloads": -1, "filename": "graphene_mongodb-1.0.0-py3-none-any.whl", "has_sig": false, "md5_digest": "16a847341fc1883816e35b4f2becda5f", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 4971, "upload_time": "2018-04-22T21:50:37", "url": "https://files.pythonhosted.org/packages/6d/c5/efd59e0bf393e9fabefc4d1f33f443d6667c252d2d89936c2f9ab9ee84a2/graphene_mongodb-1.0.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "556254e612b7fdd40fa7f59272d9a911", "sha256": "a17efc1239ef655811c0c7e34bc82cc6ce452096b9bfe3cb44d168f8be54ad5c" }, "downloads": -1, "filename": "graphene-mongodb-1.0.0.tar.gz", "has_sig": false, "md5_digest": "556254e612b7fdd40fa7f59272d9a911", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3552, "upload_time": "2018-04-22T21:50:38", "url": "https://files.pythonhosted.org/packages/a0/c7/c0ef7c05e1d4fffa3925b37193912a474c82ba8029ca879c2f3374626c65/graphene-mongodb-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "a22784239061bb65d97776c1439cd573", "sha256": "2551b7002c1d492c08d944f9daf036f5f5af1a8e90dc4c3243cd0d4c0dce3759" }, "downloads": -1, "filename": "graphene_mongodb-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "a22784239061bb65d97776c1439cd573", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 12717, "upload_time": "2018-04-30T14:20:20", "url": "https://files.pythonhosted.org/packages/ef/cd/f8415fcf3cbce6337dbbe61600f3fca8020c3fac8160754fe698cc9decc9/graphene_mongodb-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "9243005c2aea21b7b8d26e70a013b164", "sha256": "3e824fdcdc05661259313cc6d81b4bbf4db0383fbd2664278f9bddce1f7c6148" }, "downloads": -1, "filename": "graphene_mongodb-1.0.1.tar.gz", "has_sig": false, "md5_digest": "9243005c2aea21b7b8d26e70a013b164", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9581, "upload_time": "2018-04-30T14:20:21", "url": "https://files.pythonhosted.org/packages/00/c9/9aaad1fcf7d45a93b079b917be647cefaff8e5bdedca68ba224b877c2b48/graphene_mongodb-1.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "a22784239061bb65d97776c1439cd573", "sha256": "2551b7002c1d492c08d944f9daf036f5f5af1a8e90dc4c3243cd0d4c0dce3759" }, "downloads": -1, "filename": "graphene_mongodb-1.0.1-py3-none-any.whl", "has_sig": false, "md5_digest": "a22784239061bb65d97776c1439cd573", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 12717, "upload_time": "2018-04-30T14:20:20", "url": "https://files.pythonhosted.org/packages/ef/cd/f8415fcf3cbce6337dbbe61600f3fca8020c3fac8160754fe698cc9decc9/graphene_mongodb-1.0.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "9243005c2aea21b7b8d26e70a013b164", "sha256": "3e824fdcdc05661259313cc6d81b4bbf4db0383fbd2664278f9bddce1f7c6148" }, "downloads": -1, "filename": "graphene_mongodb-1.0.1.tar.gz", "has_sig": false, "md5_digest": "9243005c2aea21b7b8d26e70a013b164", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9581, "upload_time": "2018-04-30T14:20:21", "url": "https://files.pythonhosted.org/packages/00/c9/9aaad1fcf7d45a93b079b917be647cefaff8e5bdedca68ba224b877c2b48/graphene_mongodb-1.0.1.tar.gz" } ] }