{"id":17919927,"url":"https://github.com/eavanvalkenburg/graphql_function","last_synced_at":"2025-04-03T10:23:24.291Z","repository":{"id":43957481,"uuid":"510730064","full_name":"eavanvalkenburg/graphql_function","owner":"eavanvalkenburg","description":"Azure Function to create GraphQL endpoint against Cosmos DB","archived":false,"fork":false,"pushed_at":"2022-07-12T13:29:01.000Z","size":35,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-08T23:44:28.079Z","etag":null,"topics":["ariadne","azure","azure-function","azure-function-python","azure-function-v4","azurefunction","cosmosdb","function","graphql","graphql-cosmos","graphql-server","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eavanvalkenburg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-05T12:37:43.000Z","updated_at":"2024-10-22T08:23:30.000Z","dependencies_parsed_at":"2022-08-03T00:30:53.876Z","dependency_job_id":null,"html_url":"https://github.com/eavanvalkenburg/graphql_function","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/eavanvalkenburg%2Fgraphql_function","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eavanvalkenburg%2Fgraphql_function/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eavanvalkenburg%2Fgraphql_function/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eavanvalkenburg%2Fgraphql_function/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eavanvalkenburg","download_url":"https://codeload.github.com/eavanvalkenburg/graphql_function/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246981867,"owners_count":20863980,"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":["ariadne","azure","azure-function","azure-function-python","azure-function-v4","azurefunction","cosmosdb","function","graphql","graphql-cosmos","graphql-server","python"],"created_at":"2024-10-28T20:19:57.126Z","updated_at":"2025-04-03T10:23:24.204Z","avatar_url":"https://github.com/eavanvalkenburg.png","language":"Python","readme":"# Azure Function that implements GraphQL against a CosmosDB SQL endpoint\n\nThis is a ready to use Azure Function, based on the v4 runtime and Python 3.9. It creates a [GraphQL] endpoint to query against a CosmosDB SQL endpoint, with a GET to the `/api/graphql` endpoint you get a portal you can then use to query against the DB (with POST requests).\n\nThe documents for this are the sample documents you get when creating a CosmosDB, they have just an address as field (and that field is also the partition_key), as well as all CosmosDB default fields.\n\nThis project uses [Ariadne], which is a schema-first GraphQL package. So in this case the schema of your Cosmos DB document needs to be included in the schemas folder, the [Container schema](schemas//container.graphql) is a good starting place. Also provided are interfaces for [Cosmos](schemas//interface_cosmos.graphql) (other API's) and [Cosmos SQL](schemas//interface_cosmos_sql.graphql) that can be used to kickstart your schema. The container schema is the return type of a query to container\n\nQueries are done against the schema, where `address` and `ID` are fields that can be included as a filter, if both are included then Cosmos will do a point lookup, otherwise a query is used, if `address` is not given that is a cross-partition query, which is potentially more expensive, so including the partititon key (in this case `address`) is advisable. The fields of `Container` can be selected in the query.\n\nThe mutation function uses the `upsert_item` function in Cosmos, so if the `ID` is given (`address` is mandatory), if you change the `address` it will actually create a new item in the updated partition, if you change something other then the partition key (`address` in this case) it will update the item.\n\nFor all details of how to implement the graphql refer to the [Ariadne] documentation. But there are four places that need to be updated to adopt this to your schema.\n\n1. Schema's in the [schema](schema//) folder: \n    1. the container.graphql (also shown below) is the most important one to adopt, no need to change the name `Container`, it is internal only.\n    1. the query and mutation schema's, remember that the return type of the Query is Container, the word `container` (no cap) can be changed to represent the type of object you want to query.\n1. [Resolvers](graphql//resolvers//), you only need a resolver if you want to change a field's representation, if you want to create additional fields, or if you have to change the name between the item in Cosmos and the field name in the query, the [resolvers_specific.py](graphql//resolvers//resolvers_specific.py) file has an example of a resolver that takes the timestamp, converts it into a local timestamp and returns that as a ISO formatted field.\n1. [Schema.py](graphql//schema.py) here the resolvers are added to the ObjectTypes (Ariadne specific thing) and linked to specific fields. \n1. Environment variables, if running this locally using the azure-func-tools, create a `local.settings.json` based on the [sample](sample.settings.json), if necessary (for instance if you want to use AAD auth with Cosmos instead of Key-based), change the way these are imported and used in [const](graphql//const.py) and [cosmos](graphql//cosmos.py). If you deploy this function to Azure make sure to sync the settings (VSCode can do this with a command) to the app settings there so that it works.\n\n### Container definition\nContainer definition without descriptions, [full schema here][Container schema].\n```graphql\ntype Container implements CosmosSQL {\n    id: ID\n    partition_key: String\n    partition_key_field: String\n    address: String\n    _rid: String\n    _self: String\n    _etag: String\n    _attachments: String\n    _ts: Int\n    timestamp: String\n}\n```\n\n### Query\n[Query schema](schemas//query.graphql)\n```graphql\n{\n  container(address: \"2007, NE 37TH PL\") {\n  \taddress\n    id\n    timestamp\n    _ts\n  }\n  costs\n  continuation\n}\n```\n\n### Mutation\n[Mutation schema](schemas//mutation.graphql)\n```graphql\nmutation {\n  container(input: { address: \"4200, 54th Avenue South\" })\n  costs\n}\n```\n\n### Introspection\n[Introspection documentation](https://graphql.org/learn/introspection/)\n```graphql\n{\n  __type(name: \"Container\") {\n    name\n    description\n    fields {\n      name\n      description\n      type {\n        name\n        kind\n      }\n    }\n  }\n}\n```\n\n[GraphQL]: https://graphql.org/\n[Ariadne]: https://ariadnegraphql.org/\n[Container schema]: schemas//container.graphql\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feavanvalkenburg%2Fgraphql_function","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feavanvalkenburg%2Fgraphql_function","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feavanvalkenburg%2Fgraphql_function/lists"}