{"id":21927933,"url":"https://github.com/giuliano-marinelli/apollo-dynamic","last_synced_at":"2026-06-19T18:31:30.572Z","repository":{"id":224614881,"uuid":"763746128","full_name":"giuliano-marinelli/apollo-dynamic","owner":"giuliano-marinelli","description":"Apollo utility to create dynamic selection sets on queries, mutations and subscriptions, based on decorated schema.","archived":false,"fork":false,"pushed_at":"2024-02-27T22:15:34.000Z","size":27,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-10T01:14:50.856Z","etag":null,"topics":["apollo","apollo-client","apollo-graphql","decorators","dynamic","selection-set","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/giuliano-marinelli.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-26T21:03:29.000Z","updated_at":"2024-02-26T21:46:12.000Z","dependencies_parsed_at":"2024-08-23T00:01:39.929Z","dependency_job_id":"452bd35b-800b-49e4-82af-caec147b07e0","html_url":"https://github.com/giuliano-marinelli/apollo-dynamic","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"e31400ed693457d6ab06ec32e46d6dafe31d2548"},"previous_names":["giuliano-marinelli/apollo-dynamic"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/giuliano-marinelli/apollo-dynamic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuliano-marinelli%2Fapollo-dynamic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuliano-marinelli%2Fapollo-dynamic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuliano-marinelli%2Fapollo-dynamic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuliano-marinelli%2Fapollo-dynamic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/giuliano-marinelli","download_url":"https://codeload.github.com/giuliano-marinelli/apollo-dynamic/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/giuliano-marinelli%2Fapollo-dynamic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34544403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-19T02:00:06.005Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["apollo","apollo-client","apollo-graphql","decorators","dynamic","selection-set","typescript"],"created_at":"2024-11-28T22:19:27.903Z","updated_at":"2026-06-19T18:31:30.555Z","avatar_url":"https://github.com/giuliano-marinelli.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Apollo Dynamic [![npm version](https://badge.fury.io/js/apollo-dynamic.svg)](https://badge.fury.io/js/apollo-dynamic)\n\nApollo Dynamic allows to create dynamic selection sets on queries, mutations and subscriptions when using [`@apollo/client`](https://github.com/apollographql/apollo-client) for consult GraphQL resolvers. It works by decorating entity classes with `@SelectionType` and `@SelectionField` which allows to fabric dynamics selections set with a similar syntax as TypeORM repositories (relations).\n\nThis library can be used with any wrapper of Apollo Client, it offer simple functions and decorators that can be imported directly with TypeScript.\n\n## Installation\n\n```bash\n$ npm install apollo-dynamic\n```\n\n## Usage\n\nWith Apollo Client you can make GraphQL queries like this:\n\n```typescript\nimport { gql, useQuery } from '@apollo/client';\n\nconst GET_PERSONS = gql`\n  query GetPersons {\n    persons {\n      id\n      firstname\n      lastname\n      secret\n      profile {\n        avatar\n        nickname\n      }\n    }\n  }\n`;\n```\n\nAnd make the http call using this:\n\n```typescript\nconst { loading, error, data } = useQuery(GET_PERSONS);\n```\n\nIt works fine at first, but what happened if we want to not get the profile in some queries, or if we want to hide some parameters by a condition. Well, you probably may answer this:\n\n```typescript\nconst GET_PERSONS = gql`\n  query GetPersons($isSuperAgent: Boolean!, $includeProfile: Boolean!) {\n    persons {\n      id\n      firstname\n      lastname\n      secret @include(if: $isSuperAgent)\n      profile @include(if: $includeProfile) {\n        avatar\n        nickname\n      }\n    }\n  }\n`;\n```\n\nAnd you are right, but what happens if we have relationships that can be nested indefinitely, or if the relationship is in both ways?. In this cases (most cases) you must repeat all the logic for the opposite side of the relationship, in the other entity queries. Some say that this can be afforded with code generation. But i bring here a superior solution: Dynamic Selection Sets!\n\n### Decorators\n\n---\n\nIn the typical CRUD systems, we always have entities that we use as interfaces to move data from here to there. So we can assume that if you are using the GetPersons resolver, you probably have a Person entity class or type, like this:\n\n```typescript\nexport class Person {\n    id?: string;\n    firstname?: string;\n    lastname?: string;\n    secret?: string;\n    profile: Profile;\n    articles: Article[];\n}\n\nexport class Profile {\n    avatar: string;\n    nickname: string;\n}\n\nexport class Article {\n    id: string,\n    name: string;\n    person: Person;\n    type: ArticleType;\n}\n\nexport class ArticleType {\n    category: string;\n    section: string;\n}\n```\n\n_We add some more entities for the example._\n\nThe proposed library want to make use of this precious interfaces that we don't want to repeat on all the code, because if something change in the entity model we have to change it everywhere. So here we introduce the `@SelectionType` and `@SelectionField` decorators:\n\n```typescript\nimport { SelectionType, SelectionField } from 'apollo-dynamic'\n\n@SelectionType('Person')\nexport class Person {\n    @SelectionField()\n    id?: string;\n\n    @SelectionField()\n    firstname?: string;\n\n    @SelectionField()\n    lastname?: string;\n\n    @SelectionField({ include: 'isSuperAgent' })\n    secret?: string;\n\n    @SelectionField(() =\u003e Profile)\n    profile: Profile;\n\n    @SelectionField(() =\u003e Article)\n    articles: Article[];\n}\n\n@SelectionType('Profile')\nexport class Profile {\n    @SelectionField()\n    avatar: string;\n\n    @SelectionField()\n    nickname: string;\n}\n\n@SelectionType('Article',{\n    default: { relations: { artType: true } }\n})\nexport class Article {\n    @SelectionField({ skip: (cond) =\u003e cond.noIDsPlease })\n    id: string,\n\n    @SelectionField()\n    name: string;\n\n    @SelectionField(() =\u003e Person)\n    person: Person;\n\n    @SelectionField(() =\u003e ArticleType)\n    artType: ArticleType;\n}\n\n@SelectionType('ArticleType')\nexport class ArticleType {\n    @SelectionField()\n    category: string;\n\n    @SelectionField()\n    section: string;\n}\n```\n\nNow with this decorators we can use the Selection Types on our queries and the selection set will be generated automatically based on the input parameters we send.\n\n### Queries, Mutations and Subscriptions\n\n---\n\nGoing back to our query example, we can rewrite our GraphQL query using the new Selection Types like this (we use the names from `@SelectionType` decorator):\n\n```typescript\nconst GET_PERSONS = gql`\n  query GetPersons {\n    persons {\n      Person\n    }\n  }\n`;\n```\n\nAnd use it like this:\n\n```typescript\nconst { loading, error, data } = useQuery(\n  select(GET_PERSONS, {\n    relations: { profile: false, article: { artType: true } },\n    conditions: { isSuperAgent: false }\n  })\n);\n```\n\nThis will get us something like this:\n\n```typescript\nconst GET_PERSONS = gql`\n  query GetPersons {\n    persons {\n      id\n      firstname\n      lastname\n      article {\n        id\n        name\n        artType {\n          category\n          section\n        }\n      }\n    }\n  }\n`;\n```\n\nWe can also create the query from the Article side or from whatever entity we want. The magic is we can define the GraphQL interfaces once and then use em wherever we want without losing the capability of customize the result set. So we don't have to repeat the queries strings for every different selection set we want to ask for. Or moreover, we don't need to mediate with include or skip parameters that can or cannot be relevant (it can't be nullified on common queries).\n\nGoing forward, we can do the same with **mutations**:\n\n```typescript\nconst CREATE_PERSON = gql`\n  query CreatePerson($personCreateInput: PersonCreateInput!) {\n    createPerson(personCreateInput: $personCreateInput) {\n      Person\n    }\n  }\n`;\n\nconst [mutateFunction, { data, loading, error }] = useMutation(\n  select(CREATE_PERSON, { relations: { profile: true } }),\n  { variables: { firstname: 'Jonh', lastname: 'Doe' } }\n);\n```\n\nAnd with **subscriptions**:\n\n```typescript\nconst ARTICLES_SUBSCRIPTION = gql`\n  subscription OnArticleAdded($id: ID!) {\n    articleAdded(id: $id) {\n      Article\n    }\n  }\n`;\n\nconst { data, loading } = useSubscription(select(ARTICLES_SUBSCRIPTION, { relations: { artType: true } }), {\n  variables: { id: 'someid' }\n});\n```\n\n## Stay in touch\n\n- Author - [Giuliano Marinelli](https://www.linkedin.com/in/giuliano-marinelli/)\n- Website - [https://github.com/giuliano-marinelli](https://github.com/giuliano-marinelli)\n\n## License\n\nThis package is [MIT licensed](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiuliano-marinelli%2Fapollo-dynamic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgiuliano-marinelli%2Fapollo-dynamic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgiuliano-marinelli%2Fapollo-dynamic/lists"}