{"id":13727359,"url":"https://github.com/zth/relay-store-types-generator","last_synced_at":"2025-03-24T02:32:04.146Z","repository":{"id":57352771,"uuid":"187692631","full_name":"zth/relay-store-types-generator","owner":"zth","description":"Generate types for the Relay store from your GraphQL schema.","archived":false,"fork":false,"pushed_at":"2019-05-21T16:43:53.000Z","size":101,"stargazers_count":19,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-08T17:30:56.912Z","etag":null,"topics":["graphql","react","relay","relay-modern","relaymodern"],"latest_commit_sha":null,"homepage":null,"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/zth.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-05-20T18:23:09.000Z","updated_at":"2023-12-07T00:08:41.000Z","dependencies_parsed_at":"2022-09-05T11:40:41.803Z","dependency_job_id":null,"html_url":"https://github.com/zth/relay-store-types-generator","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/zth%2Frelay-store-types-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Frelay-store-types-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Frelay-store-types-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zth%2Frelay-store-types-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zth","download_url":"https://codeload.github.com/zth/relay-store-types-generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221931696,"owners_count":16903799,"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":["graphql","react","relay","relay-modern","relaymodern"],"created_at":"2024-08-03T01:03:51.750Z","updated_at":"2024-10-28T20:36:28.399Z","avatar_url":"https://github.com/zth.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# relay-store-types-generator\n\n_THIS IS HIGHLY EXPERIMENTAL AND PROBABLY NOT READY FOR ACTUAL USE YET_\n\nGenerate types for the Relay store from your GraphQL schema. More type safety for your Relay store with zero runtime cost and a very small set of changes needed for your code.\n\n_Currently only generates Flow types, TypeScript mode coming soon_.\n\n## The idea\n\nWhen you write queries, mutations and fragments, Relay generates types for you that you can use for type-safety. Relay also lets you manually interact with its store through `updaters` after mutations, `commitLocalUpdate` and so on. But, when interacting with the store you're left with a generalized API with general types.\n\nThis package generates types for Relay's store tailored to your specific GraphQL schema, so that your interaction with the store can be\nfully type safe, just like interacting with the data Relay gives you.\n\n### Example\n\nImagine the following schema:\n\n```graphql\nschema {\n  query: Query\n}\n\ntype Query {\n  bestFriend: User\n}\n\ntype User {\n  firstName: String!\n  age: Int!\n}\n```\n\nInstead of doing this:\n\n```javascript\ncommitLocalUpdate(environment, store =\u003e {\n  const root = store.getRoot(); // Gets you a RecordProxy\n  const bestFriend = root.getLinkedRecord('bestFriend'); // Also gets you a RecordProxy, but nullable\n  if (bestFriend) {\n    const age = bestFriend.getValue('agee'); // This is allowed even though it's misspelled\n    bestFriend.setValue(123, 'firstName'); // This is allowed as well, even though firstName is supposed to be a string\n  }\n});\n```\n\n...cast your store to `Store$RecordSourceSelectorProxy` like this:\n\n```javascript\nimport type { Store$RecordSourceSelectorProxy } from '../path/to/generated/relay-store-types.js.flow';\n\n// Cast to typed version of the store from the generated type file\ncommitLocalUpdate(environment, (store: Store$RecordSourceSelectorProxy) =\u003e {\n  const root = store.getRoot(); // Gets you a RecordProxy$Query with a shape corresponding to your root query\n  const bestFriend = root.getLinkedRecord('bestFriend'); // Returns a ?RecordProxy$User since this is a user\n  if (bestFriend) {\n    const age = bestFriend.getValue('agee'); // This will not be allowed since there's no getValue method for \"agee\" on RecordProxy$User\n    bestFriend.setValue(123, 'firstName'); // This won't be allowed either, because the method that accepts \"firstName\" as key expects the value to be ?string\n  }\n});\n```\n\n## Usage\n\n```\nyarn add --dev relay-store-types-generator\n\n./node_moduels/.bin/relay-store-types-generator --flow --schema ./path/to/schema.graphql --out-dir ./path/to/output/dir --custom-scalars-path ./path/to/file/exporting/custom/scalars\n\n# You can add it to package.json\n...\n\"scripts\": {\n    \"generate:relay-store-types\": \"relay-store-types-generator --flow --schema ./path/to/schema.graphql --out-dir ./path/to/output/dir --custom-scalars-path ./path/to/file/exporting/custom/scalars\"\n...\n\n# ...and then run like\nyarn generate:relay-store-types\n```\n\n```\nrelay-store-types-generator\n\nOptions:\n  --schema [path]               Path to schema.graphql\n  --custom-scalars-path [path]  Path to file exporting custom scalars.\n  --out-dir [path]              Path to directory to output type file.\n  --flow                        Output Flow types.\n  --typescript                  Output TypeScript types.\n  -h, --help                    output usage information\n```\n\nPreferably set this up to run after whatever you use to persist the introspection of your GraphQL schema. That way you always have a fresh version.\n\n## FAQ\n\n### What if I start using this and find it's not for me, do I need to do a lot of invasive changes to my code to get this to work?\n\nNot at all! That's a primary feature. Wherever you interact with the Relay store and want to do so in a type-safe way, just cast the store to the\ngenerated store type, like `(store: Store$RecordSourceSelectorProxy) =\u003e ...`. Want to go back? Remove the cast and work with the store as usual!\n\n### What about my custom scalars, have you forgot about them?\n\nI'm hurt you even ask! You can pass a path to a file exporting your custom scalars. Example:\n\n```\nrelay-store-types-generator --flow --schema ./schema.graphql --out-dir ./types --custom-scalars-path ./src/customScalars.js\n\n# customScalars.js\nmodule.exports = {\n  \"Datetime\": \"string\",\n  \"Cursor\": \"string\",\n  \"BigInt\": \"number\",\n  \"Flag\": \"boolean\"\n};\n```\n\n### There's _lots_ of types generated... Can I reduce the size some way?\n\nCurrently no, but I have a few ideas for how to reduce the size of the generated types.\nHowever, ultimately, generating types for a large GraphQL schema will always result in lots of types,\nsince the number of combinations of keys/types/methods and so on the types need to cover are large.\n\n## TODO\n\n- [ ] TypeScript mode\n- [ ] Type `args` for every field\n- [ ] Type `filters` for connections\n- [ ] Optimize amount of generated code\n- [ ] Handle subscriptions (?)\n- [ ] Parse extensions in schema\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzth%2Frelay-store-types-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzth%2Frelay-store-types-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzth%2Frelay-store-types-generator/lists"}