{"id":19670361,"url":"https://github.com/iamfoysal/graph-api","last_synced_at":"2025-05-05T20:21:36.241Z","repository":{"id":111399395,"uuid":"541664481","full_name":"iamfoysal/graph-api","owner":"iamfoysal","description":"simple graphql api using  graphene-django","archived":false,"fork":false,"pushed_at":"2024-10-13T08:04:43.000Z","size":172,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-11-11T17:05:59.224Z","etag":null,"topics":["api","database","django-graphene","graphql","graphql-api","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iamfoysal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2022-09-26T15:49:48.000Z","updated_at":"2024-10-13T08:04:47.000Z","dependencies_parsed_at":"2023-05-18T15:01:26.441Z","dependency_job_id":null,"html_url":"https://github.com/iamfoysal/graph-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamfoysal%2Fgraph-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamfoysal%2Fgraph-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamfoysal%2Fgraph-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iamfoysal%2Fgraph-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iamfoysal","download_url":"https://codeload.github.com/iamfoysal/graph-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233292650,"owners_count":18654091,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","database","django-graphene","graphql","graphql-api","python"],"created_at":"2024-11-11T17:06:02.712Z","updated_at":"2025-01-10T03:32:47.672Z","avatar_url":"https://github.com/iamfoysal.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"###### create and run the migrations for our database:\r\n\r\n     $ python manage.py makemigrations\r\n     $ python manage.py migrate\r\n     \r\n  \r\n ###### run demo data the command below to import the data into the database:\r\n    \r\n      $ python manage.py loaddata data.json\r\n \r\n \r\n The ``CreateBook`` class will be used to create and save new `Book` entries to the database. For every mutation class we must have an ``Arguments`` inner class and a ``mutate()`` class method.\r\n\r\nWe defined an instance of the ``BookInput`` class we created earlier as our arguments, and we made it mandatory with the ``required=True`` option. After that we defined the model we are working with by doing this ``book = graphene.Field(BookType)``.\r\n\r\nIn the ``mutate`` method we are saving a new book by calling the ``save()`` method on a new ``Book`` instance created from the ``book_data`` values passed as argument.\r\n\r\nBelow you can see the implementation of the ``UpdateBook`` mutation. Add this code at the bottom of ``api/schema.py``:\r\n\r\n  class UpdateBook(graphene.Mutation):\r\n    class Arguments:\r\n        book_data = BookInput(required=True)\r\n\r\n    book = graphene.Field(BookType)\r\n\r\n    @staticmethod\r\n    def mutate(root, info, book_data=None):\r\n\r\n        book_instance = Book.objects.get(pk=book_data.id)\r\n\r\n        if book_instance:\r\n            book_instance.title = book_data.title\r\n            book_instance.author = book_data.author\r\n            book_instance.year_published = book_data.year_published\r\n            book_instance.review = book_data.review\r\n            book_instance.save()\r\n\r\n            return UpdateBook(book=book_instance)\r\n        return UpdateBook(book=None)\r\n        \r\n######  Let’s start the Django server:\r\n   \r\n          $ python manage.py runserver\r\n          \r\nNow visit ``http://127.0.0.1:8000/api/graphql`` in your browser. You should see the GraphIQL interface for interactive testing of the GraphQL API. \r\n\r\n\r\n##### Issuing a query\r\n\r\n       query {\r\n              allBooks {\r\n                id\r\n                title\r\n                author\r\n                yearPublished\r\n                review\r\n              }\r\n            }\r\n\r\nThe GraphQL code below is requesting all the books from the database\r\n\r\n###### For single query following the query, which requests a single book by its id:\r\n\r\n          query {\r\n            book(bookId: 2) {\r\n                   id\r\n                   title\r\n                   author\r\n                 }\r\n               }\r\n\r\n\r\n##### for create new Book. GraphQL snippet defines a mutation that adds a new book to the database: \r\n\r\n          mutation createMutation {\r\n            createBook(bookData: {title: \"The Chronicles\", author: \"Jhon Deo\", yearPublished: \"1980\", review: 42}) {\r\n              book {\r\n                title,\r\n                author,\r\n                yearPublished,\r\n                review\r\n              }\r\n            }\r\n          }\r\n\r\n##### The next GraphQL mutation updates the book with id=5:\r\n\r\n          mutation updateMutation {\r\n            updateBook(bookData: {id: 5, title: \"The Lord of the Rings\", author: \"J.J.R\", yearPublished: \"1948\", review: 20}) {\r\n              book {\r\n                title,\r\n                author,\r\n                yearPublished,\r\n                review\r\n              }\r\n            }\r\n          }\r\n          \r\n##### The final mutation example deletes the book with id=4 from the database: \r\n\r\n          mutation deleteMutation{\r\n            deleteBook(id: 6) {\r\n              book {\r\n                id\r\n              } \r\n            }\r\n          }\r\n\r\n               \r\n               \r\n\r\n\r\n\r\n      \r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamfoysal%2Fgraph-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiamfoysal%2Fgraph-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiamfoysal%2Fgraph-api/lists"}