{"id":16002824,"url":"https://github.com/danstarns/graphql-code-directive","last_synced_at":"2026-05-07T01:31:51.861Z","repository":{"id":57253392,"uuid":"334734272","full_name":"danstarns/graphql-code-directive","owner":"danstarns","description":"Use a GraphQL Schema Directive to define Javascript logic, have that logic executed as a resolver. ","archived":false,"fork":false,"pushed_at":"2021-08-01T10:25:11.000Z","size":124,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-09-28T05:55:43.080Z","etag":null,"topics":["code","directive","graphql","graphql-directive","nodejs","schema-directive"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/graphql-code-directive","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/danstarns.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":"2021-01-31T19:05:03.000Z","updated_at":"2023-03-08T21:03:27.000Z","dependencies_parsed_at":"2022-08-31T22:11:58.736Z","dependency_job_id":null,"html_url":"https://github.com/danstarns/graphql-code-directive","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/danstarns/graphql-code-directive","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danstarns%2Fgraphql-code-directive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danstarns%2Fgraphql-code-directive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danstarns%2Fgraphql-code-directive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danstarns%2Fgraphql-code-directive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danstarns","download_url":"https://codeload.github.com/danstarns/graphql-code-directive/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danstarns%2Fgraphql-code-directive/sbom","scorecard":{"id":321757,"data":{"date":"2025-08-11","repo":{"name":"github.com/danstarns/graphql-code-directive","commit":"8b55b2dace22f1e0207b5509534e2af8d60b1b80"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":2.7,"checks":[{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Pinned-Dependencies","score":3,"reason":"dependency not pinned by hash detected -- score normalized to 3","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/danstarns/graphql-code-directive/nodejs.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/nodejs.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/danstarns/graphql-code-directive/nodejs.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npmpublish.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/danstarns/graphql-code-directive/npmpublish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npmpublish.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/danstarns/graphql-code-directive/npmpublish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npmpublish.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/danstarns/graphql-code-directive/npmpublish.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/npmpublish.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/danstarns/graphql-code-directive/npmpublish.yml/main?enable=pin","Info:   0 out of   6 GitHub-owned GitHubAction dependencies pinned","Info:   3 out of   3 npmCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Code-Review","score":0,"reason":"Found 0/25 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/nodejs.yml:1","Warn: no topLevel permission defined: .github/workflows/npmpublish.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":0,"reason":"29 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-qm7x-rc44-rrqw","Warn: Project is vulnerable to: GHSA-2p3c-p3qw-69r4","Warn: Project is vulnerable to: GHSA-j5g3-5c8r-7qfx","Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c","Warn: Project is vulnerable to: GHSA-wm7h-9275-46v2","Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc","Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx","Warn: Project is vulnerable to: GHSA-4q6p-r6v2-jvc5","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-qrpm-p2h7-hrv2","Warn: Project is vulnerable to: GHSA-mwcw-c2x4-8c55","Warn: Project is vulnerable to: GHSA-r683-j2x4-v87g","Warn: Project is vulnerable to: GHSA-v39p-96qg-c8rf","Warn: Project is vulnerable to: GHSA-8v63-cqqc-6r2c","Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j","Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg","Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p","Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-18T01:31:55.939Z","repository_id":57253392,"created_at":"2025-08-18T01:31:55.939Z","updated_at":"2025-08-18T01:31:55.939Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32719449,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-07T00:29:05.620Z","status":"ssl_error","status_checked_at":"2026-05-07T00:28:57.074Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["code","directive","graphql","graphql-directive","nodejs","schema-directive"],"created_at":"2024-10-08T10:04:21.164Z","updated_at":"2026-05-07T01:31:51.830Z","avatar_url":"https://github.com/danstarns.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# graphql-code-directive\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/graphql-code-directive\"\u003e\n    \u003cimg alt=\"npm package\" src=\"https://badge.fury.io/js/graphql-code-directive.svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/danstarns/graphql-code-directive/actions/workflows/npmpublish.yml\"\u003e\n    \u003cimg alt=\"Publish\" src=\"https://github.com/danstarns/graphql-code-directive/actions/workflows/npmpublish.yml/badge.svg\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/danstarns/graphql-code-directive/actions/workflows/nodejs.yml\"\u003e\n    \u003cimg alt=\"Tests\" src=\"https://github.com/danstarns/graphql-code-directive/actions/workflows/nodejs.yml/badge.svg\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nUse a GraphQL Schema Directive to define Javascript logic, have that logic executed as a resolver.\n\n## Links\n\n1. [NPM](https://www.npmjs.com/package/graphql-code-directive)\n2. [Examples](https://github.com/danstarns/graphql-code-directive/blob/main/examples/fetch.js)\n3. [Blog Post](https://medium.com/@danstarns/writing-javascript-directly-in-your-graphql-schema-285ae7906d94)\n\n## What\n\nIts a directive you can use on Fields:\n\n```graphql\ndirective @code(source: String!) on FIELD_DEFINITION\n```\n\nYou define Javascript logic in the `source` argument. The source is wrapped in an IFFE \u0026 passed into a [https://nodejs.org/api/vm.html](https://nodejs.org/api/vm.html). Supports Promises \u0026 you can use dependency injection via the context.\n\n## Installing\n\n```bash\n$ npm install graphql-code-directive\n```\n\n## Usage\n\n```js\nconst { ApolloServer } = require(\"apollo-server\");\nconst codeDirective = require(\"graphql-code-directive\");\nconst fetch = require(\"node-fetch\");\n\nconst typeDefs = `\n    type Todo {\n        id: ID!\n        userId: ID!\n        title: String!\t\n        completed: Boolean\t\n    }\n\n    type Query {\n        todos: [Todo] \n            @code(\n                source: \"\"\"\n                const response = await context.fetch('https://jsonplaceholder.typicode.com/todos');\n                return response.json()\n                \"\"\"\n            )\t\t\n    }\n`;\n\nconst server = new ApolloServer({\n  typeDefs: [codeDirective.typeDefs, typeDefs],\n  schemaDirectives: {\n    code: codeDirective.CodeDirective,\n  },\n  context: () =\u003e {\n    return {\n      fetch,\n    };\n  },\n});\n\nserver.listen(4000).then(() =\u003e console.log(\"http://localhost:4000\"));\n```\n\n## FAQ\n\n### How to dependency inject ?\n\nInside the code source you have access to the four global variables:\n\n1. `rootValue`\n2. `args`\n3. `context`\n4. `resolveInfo`\n\nSo for example if you were to write a 'Normal' Javascript resolver the variables would map to each argument:\n\n```javascript\nfunction myResolver(rootValue, args, context, resolveInfo) {}\n```\n\n### Is this safe ?\n\nMake sure you disable introspection \u0026 yes it is safe. You are in control of what the VM has access to, via `context`, and the Node.js team has done a good job at isolating the VM.\n\n### Is it testable ?\n\nUnit testing could become cumbersome, this is because you would have to parse the definitions into an AST in order to access the `source`. You can however write solid integration tests, why not checkout [https://www.apollographql.com/docs/apollo-server/testing/testing/](https://www.apollographql.com/docs/apollo-server/testing/testing/) ?\n\n### Can I use the new Schema Directives ?\n\nYes! There are 3 exports:\n\n1. `typeDefs` - A string of the directives type definitions\n2. `CodeDirective` - A legacy `SchemaDirectiveVisitor` that you are most likely to be familiar with\n3. `codeDirective` - A functional approach to Schema Directives that returns a transformer\n\n#### Using with new Schema Directives\n\n```js\nconst { codeDirective } = require(\"graphql-code-directive\");\nconst { makeExecutableSchema } = require(\"@graphql-tools/schema\");\n\nconst { codeDirectiveTypeDefs, codeDirectiveTransformer } = codeDirective();\n\nlet schema = makeExecutableSchema({\n  typeDefs: [codeDirectiveTypeDefs, typeDefs],\n  resolvers: {},\n});\n\nschema = codeDirectiveTransformer(schema);\n```\n\n## Licence\n\nMIT licence.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanstarns%2Fgraphql-code-directive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanstarns%2Fgraphql-code-directive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanstarns%2Fgraphql-code-directive/lists"}