{"id":13516732,"url":"https://github.com/graphile-contrib/postgraphile-plugin-derived-field","last_synced_at":"2025-05-12T17:05:18.718Z","repository":{"id":42206286,"uuid":"125497672","full_name":"graphile-contrib/postgraphile-plugin-derived-field","owner":"graphile-contrib","description":"Add derived fields in PostGraphile","archived":false,"fork":false,"pushed_at":"2023-01-06T01:52:26.000Z","size":1186,"stargazers_count":29,"open_issues_count":6,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-12T17:05:01.411Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/graphile-contrib.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":"2018-03-16T09:56:45.000Z","updated_at":"2024-08-23T07:11:17.000Z","dependencies_parsed_at":"2023-02-05T02:02:12.334Z","dependency_job_id":null,"html_url":"https://github.com/graphile-contrib/postgraphile-plugin-derived-field","commit_stats":null,"previous_names":["mattbretl/postgraphile-plugin-derived-field"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile-contrib%2Fpostgraphile-plugin-derived-field","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile-contrib%2Fpostgraphile-plugin-derived-field/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile-contrib%2Fpostgraphile-plugin-derived-field/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graphile-contrib%2Fpostgraphile-plugin-derived-field/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graphile-contrib","download_url":"https://codeload.github.com/graphile-contrib/postgraphile-plugin-derived-field/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253784908,"owners_count":21963899,"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":[],"created_at":"2024-08-01T05:01:25.330Z","updated_at":"2025-05-12T17:05:18.694Z","avatar_url":"https://github.com/graphile-contrib.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"[![Package on npm](https://img.shields.io/npm/v/postgraphile-plugin-derived-field.svg)](https://www.npmjs.com/package/postgraphile-plugin-derived-field) [![CircleCI](https://circleci.com/gh/graphile-contrib/postgraphile-plugin-derived-field.svg?style=svg)](https://circleci.com/gh/graphile-contrib/postgraphile-plugin-derived-field)\n\n# postgraphile-plugin-derived-field\n\nThis plugin provides an interface for adding derived fields \u003c!--and wrapping existing resolvers--\u003e to the schema generated by PostGraphile v4.\n\nThe term \"derived fields\" is used to differentiate this approach from the standard [Computed Columns](https://www.graphile.org/postgraphile/computed-columns/) support in PostGraphile.  This plugin effectively adds \"computed columns in JavaScript\" to your toolbelt.\n\n## Getting Started\n\nDefine your derived fields in the `derivedFieldDefinitions` property of `graphileBuildOptions`:\n\n``` js\nconst express = require(\"express\");\nconst { postgraphile } = require(\"postgraphile\");\nconst PostGraphileDerivedFieldPlugin = require(\"postgraphile-plugin-derived-field\");\n\nconst app = express();\n\napp.use(\n  postgraphile(pgConfig, schema, {\n    graphiql: true,\n    appendPlugins: [PostGraphileDerivedFieldPlugin],\n    graphileBuildOptions: {\n      derivedFieldDefinitions: [\n        // your definitions here\n      ]\n    }\n  })\n);\n\napp.listen(5000);\n```\n\nProvide `derivedFieldDefinitions` as an array of objects with the following structure:\n\n```\n{\n  identifiers: Array\u003cIdentifier\u003e,\n  inflect: function,\n  resolve: function,\n  type?: string | build =\u003e T,\n  description?: string\n}\n```\nThe Scenarios section below provides guidance on structuring `identifiers`, `inflect`, and `resolve` for your specific use case.\n\nUse `type` to specify the GraphQL type that the `resolve` function returns. This can be a string identifying the GraphQL type name, or a function (with the `build` helper available as the first argument) that returns a GraphQL type. Default: String\n\nUse `description` to populate the field description in the schema.\n\n## Scenarios\n\n### Derive a new field from one database column\n\n``` js\n{\n  identifiers: [\n    {\n      table: \"my_schema.my_table\",\n      columns: [\"my_column\"],\n    },\n  ],\n  inflect: fieldName =\u003e `derivedFrom${fieldName}`,\n  resolve: val =\u003e `Value derived from ${val}`,\n}\n```\n\n### Derive a new field from multiple database columns\n\n``` js\n{\n  identifiers: [\n    {\n      table: \"my_schema.my_table\",\n      columns: [\"my_column\", \"my_other_column\"],\n    },\n  ],\n  inflect: (...fieldNames) =\u003e\n    `derivedFrom${fieldNames.map(upperFirst).join(\"And\")}`,\n  resolve: (my_column, my_other_column) =\u003e\n    `Value derived from ${my_column} and ${my_other_column}`,\n}\n```\n\n### Derive a new field from columns that have a specific tag\n\nThis approach uses the \"smart comments\" feature of PostGraphile to match columns that have been assigned a specific tag.\n\n``` js\n{\n  identifiers: [\n    {\n      tag: \"mytag\",\n    },\n  ],\n  inflect: fieldName =\u003e `derivedFrom${fieldName}`,\n  resolve: val =\u003e `Value derived from ${val}`,\n}\n```\n\n\u003c!--### Wrap one or more existing fields\n\nTo wrap existing fields with additional resolver logic, simply exclude the `inflect` parameter.--\u003e\n\n## Alternative syntax\n\nColumn names can be provided as a string: `\"my_schema.my_table.my_column\"`.  This syntax does not support generating a derived field from multiple database columns.\n\nTag names can be provided as a string: `\"@mytag\"`\n\n## Examples\n\n\u003cdetails\u003e\n\n\u003csummary\u003eGenerate pre-signed URLs for client-side S3 GET requests\u003c/summary\u003e\n\n``` js\nconst express = require(\"express\");\nconst { postgraphile } = require(\"postgraphile\");\nconst PostGraphileDerivedFieldPlugin = require(\"postgraphile-plugin-derived-field\");\n\nconst AWS = require(\"aws-sdk\");\nconst s3 = new AWS.S3();\nconst bucket = \"postgraphile-plugin-test\";\n\nconst app = express();\n\napp.use(\n  postgraphile(pgConfig, schema, {\n    graphiql: true,\n    appendPlugins: [PostGraphileDerivedFieldPlugin],\n    graphileBuildOptions: {\n      derivedFieldDefinitions: [\n        {\n          identifiers: [\"my_schema.my_table.my_column\"],\n          inflect: fieldName =\u003e `${fieldName}SignedUrl`,\n          resolve: val =\u003e s3.getSignedUrl('getObject', {Bucket: bucket, Key: val, Expires: 900})\n        }\n      ]\n    }\n  })\n);\n\napp.listen(5000);\n```\n\n\u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphile-contrib%2Fpostgraphile-plugin-derived-field","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraphile-contrib%2Fpostgraphile-plugin-derived-field","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraphile-contrib%2Fpostgraphile-plugin-derived-field/lists"}