{"id":28028596,"url":"https://github.com/syfun/python-gql","last_synced_at":"2025-05-11T07:19:16.230Z","repository":{"id":54044588,"uuid":"228270928","full_name":"syfun/python-gql","owner":"syfun","description":"Python schema-first GraphQL library based on GraphQL-core.","archived":false,"fork":false,"pushed_at":"2021-12-07T10:14:08.000Z","size":198,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T18:45:42.700Z","etag":null,"topics":["async","graphql","python","schema-first"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syfun.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}},"created_at":"2019-12-16T00:06:38.000Z","updated_at":"2024-09-01T00:57:27.000Z","dependencies_parsed_at":"2022-08-13T06:11:08.505Z","dependency_job_id":null,"html_url":"https://github.com/syfun/python-gql","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fpython-gql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fpython-gql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fpython-gql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syfun%2Fpython-gql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syfun","download_url":"https://codeload.github.com/syfun/python-gql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253529334,"owners_count":21922687,"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":["async","graphql","python","schema-first"],"created_at":"2025-05-11T07:19:15.730Z","updated_at":"2025-05-11T07:19:16.222Z","avatar_url":"https://github.com/syfun.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# python-gql\n\nPython schema-first GraphQL library based on GraphQL-core.\n\n\n## Requirements\n\nPython 3.7+\n\n## Installation\n\n`pip install python-gql`\n\n## Getting start\n\n```python\nimport graphql\nfrom gql import gql, make_schema, query, mutate\n\ntype_defs = gql(\"\"\"\ntype Query {\n    hello(name: String!): String!\n}\n\ntype Post {\n    author: String!\n    comment: String!\n}\ntype Mutation {\n    addPost(author: String, comment: String): Post!\n}\n\"\"\")\n\n\n@query\ndef hello(parent, info, name: str) -\u003e str:\n    return name\n\n\n@mutate\ndef add_post(parent, info, author: str = None, comment: str = None) -\u003e dict:\n    return {'author': author, 'comment': comment}\n\n\nschema = make_schema(type_defs)\n\nq = \"\"\"\nquery {\n    hello(name: \"graphql\")\n}\n\"\"\"\nresult = graphql.graphql_sync(schema, q)\nprint(result.data)\n\n# result: {'hello': 'graphql'}\n\nq = \"\"\"\nmutation {\n    addPost(author: \"syfun\", comment: \"This is a good library.\") {\n        author\n        comment\n    }\n}\n\"\"\"\nresult = graphql.graphql_sync(schema, q)\nprint(result.data)\n\n# result: {'addPost': {'author': 'syfun', 'comment': 'This is a good library.'}}\n```\n\n## Build schema\n\nThis library is `schema-first`, so you must build a schema explicitly.\n\nHere, we have two methods to build a schema, by `a type definitions` or `a schema file`.\n\n```python\nfrom gql import gql, make_schema\n\ntype_defs = gql(\"\"\"\ntype Query {\n    hello(name: String!): String!\n}\n\"\"\")\n\nschema = make_schema(type_defs)\n```\n\n\u003e `gql` function will check your type definitions syntax.\n\n```python\nfrom gql import make_schema_from_file\n\nschema = make_schema_from_file('./schema.graphql')\n```\n\n## Resolver decorators\n\n\u003e In Python, `decorator` is my favorite function, it save my life!\n\nWe can use `query`, `mutation`, `subscribe` to bind functions to GraphQL resolvers.\n\n```python\n@query\ndef hello(parent, info, name: str) -\u003e str:\n    return name\n```\n\nThese decorators will auto convert the snake function to camel one.\n\n```python\n# add_port =\u003e addPost\n@mutate\ndef add_post(parent, info, author: str = None, comment: str = None) -\u003e dict:\n    return {'author': author, 'comment': comment}\n```\n\nWhen the funcation name different from the resolver name, you can give a name argument to these decorators.\n\n```python\n@query('hello')\ndef hello_function(parent, info, name: str) -\u003e str:\n    return name\n```\n\nAbout `subscribe`, please see [gql-subscriptions](gql-subscriptions).\n\n## Enum type decorator\n\nUse `enum_type` decorator with a python Enum class.\n\n```python\nfrom enum import Enum\n\nfrom gql import enum_type\n\n\n@enum_type\nclass Gender(Enum):\n    MALE = 1\n    FEMALE = 2\n```\n\n## Custom Scalar\n\nUse `scalar_type` decorator with a python class.\n\n```python\nfrom gql import scalar_type\n\n\n@scalar_type\nclass JSONString:\n    description = \"The `JSONString` represents a json string.\"\n\n    @staticmethod\n    def serialize(value: Any) -\u003e str:\n        return json.dumps(value)\n\n    @staticmethod\n    def parse_value(value: Any) -\u003e dict:\n        if not isinstance(value, str):\n            raise TypeError(f'JSONString cannot represent non string value: {inspect(value)}')\n        return json.loads(value)\n\n    @staticmethod\n    def parse_literal(ast, _variables=None):\n        if isinstance(ast, StringValueNode):\n            return json.loads(ast.value)\n\n        return INVALID\n\n```\n\n## Custom directive\n\n```python\nfrom gql import gql, make_schema, query, SchemaDirectiveVisitor\nfrom gql.resolver import default_field_resolver\n\n\ntype_defs = gql(\"\"\"\ndirective @upper on FIELD_DEFINITION\n\ntype Query {\n    hello(name: String!): String! @upper\n}\n\"\"\")\n\nclass UpperDirective(SchemaDirectiveVisitor):\n    def visit_field_definition(self, field, object_type):\n        original_resolver = field.resolve or default_field_resolver\n\n        def resolve_upper(obj, info, **kwargs):\n            result = original_resolver(obj, info, **kwargs)\n            if result is None:\n                return None\n            return result.upper()\n\n        field.resolve = resolve_upper\n        return field\n\nschema = make_schema(type_defs, directives={'upper': UpperDirective})\n```\n\n## Apollo Federation\n\n[Example](https://github.com/syfun/starlette-graphql/tree/master/examples/federation)\n\n[Apollo Federation](https://www.apollographql.com/docs/apollo-server/federation/introduction/)\n\nThanks to [Ariadne](https://ariadnegraphql.org/docs/apollo-federation)\n\n\n## Framework support\n\n- [Starlette GraphQL](https://github.com/syfun/starlette-graphql)\n- [Django GraphQL](https://github.com/syfun/django-graphql)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyfun%2Fpython-gql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyfun%2Fpython-gql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyfun%2Fpython-gql/lists"}