{"id":14975700,"url":"https://github.com/risingstack/graffiti-mongoose","last_synced_at":"2025-04-05T01:03:53.667Z","repository":{"id":35112511,"uuid":"39296706","full_name":"RisingStack/graffiti-mongoose","owner":"RisingStack","description":"⚠️ DEVELOPMENT DISCONTINUED - Mongoose (MongoDB) adapter for graffiti (Node.js GraphQL ORM)","archived":false,"fork":false,"pushed_at":"2017-05-23T02:24:14.000Z","size":353,"stargazers_count":380,"open_issues_count":32,"forks_count":50,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-04-05T01:03:47.682Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://risingstack-graffiti.signup.team/","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/RisingStack.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-18T11:51:51.000Z","updated_at":"2024-12-27T00:43:43.000Z","dependencies_parsed_at":"2022-09-16T14:10:39.941Z","dependency_job_id":null,"html_url":"https://github.com/RisingStack/graffiti-mongoose","commit_stats":null,"previous_names":[],"tags_count":48,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RisingStack%2Fgraffiti-mongoose","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RisingStack%2Fgraffiti-mongoose/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RisingStack%2Fgraffiti-mongoose/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RisingStack%2Fgraffiti-mongoose/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RisingStack","download_url":"https://codeload.github.com/RisingStack/graffiti-mongoose/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247271515,"owners_count":20911587,"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-09-24T13:52:24.744Z","updated_at":"2025-04-05T01:03:53.642Z","avatar_url":"https://github.com/RisingStack.png","language":"JavaScript","readme":"# \u0026#9888; Notice: the development of the package is discontinued. Use it for educational purposes and hobby projects only.\n\n# ![graffiti](https://cloud.githubusercontent.com/assets/1764512/8900273/9ed758dc-343e-11e5-95ba-e82f876cf52d.png) Mongoose\n\n[![npm version](https://badge.fury.io/js/%40risingstack%2Fgraffiti-mongoose.svg)](https://badge.fury.io/js/%40risingstack%2Fgraffiti-mongoose)\n[![CircleCI](https://circleci.com/gh/RisingStack/graffiti-mongoose.svg?style=svg)](https://circleci.com/gh/RisingStack/graffiti-mongoose)\n[![bitHound Overall Score](https://www.bithound.io/github/RisingStack/graffiti-mongoose/badges/score.svg)](https://www.bithound.io/github/RisingStack/graffiti-mongoose)\n[![Known Vulnerabilities](https://snyk.io/test/npm/@risingstack/graffiti-mongoose/badge.svg)](https://snyk.io/test/npm/@risingstack/graffiti-mongoose)\n\n[Mongoose](http://mongoosejs.com) (MongoDB) adapter for [GraphQL](https://github.com/graphql/graphql-js).\n\n`graffiti-mongoose` generates `GraphQL` types and schemas from your existing `mongoose` models, that's how simple it is. The generated schema is compatible with [Relay](https://facebook.github.io/relay/).\n\nFor quick jump check out the [Usage section](https://github.com/RisingStack/graffiti-mongoose#usage).\n\n## Install\n\n```shell\nnpm install graphql @risingstack/graffiti-mongoose  --save\n```\n\n## Example\n\nCheck out the [/example](https://github.com/RisingStack/graffiti-mongoose/tree/master/example) folder.\n\n```shell\ncd graffiti-mongoose\nnpm install # install dependencies in the main folder\ncd example\nnpm install # install dependencies in the example folder\nnpm start # run the example application and open your browser: http://localhost:8080\n```\n\n## Usage\n\nThis adapter is written in `ES6` and `ES7` with [Babel](https://babeljs.io) but it's published as transpiled `ES5` JavaScript code to `npm`, which means you don't need `ES7` support in your application to run it.  \n\n__Example queries can be found in the [example folder](https://github.com/RisingStack/graffiti-mongoose/tree/master/example#example-queries).__\n\n##### usual mongoose model(s)\n```javascript\nimport mongoose from 'mongoose';\n\nconst UserSchema = new mongoose.Schema({\n  name: {\n    type: String,\n    // field description\n    description: 'the full name of the user'\n  },\n  hiddenField: {\n    type: Date,\n    default: Date.now,\n    // the field is hidden, not available in GraphQL\n    hidden: true\n  },\n  age: {\n    type: Number,\n    indexed: true\n  },\n  friends: [{\n    type: mongoose.Schema.Types.ObjectId,\n    ref: 'User'\n  }]\n});\n\nconst User = mongoose.model('User', UserSchema);\nexport default User;\n```\n\n##### graffiti-mongoose\n```javascript\nimport {getSchema} from '@risingstack/graffiti-mongoose';\nimport graphql from 'graphql';\nimport User from './User';\n\nconst options = {\n  mutation: false, // mutation fields can be disabled\n  allowMongoIDMutation: false // mutation of mongo _id can be enabled\n};\nconst schema = getSchema([User], options);\n\nconst query = `{\n    users(age: 28) {\n      name\n      friends(first: 2) {\n        edges {\n          cursor\n          node {\n            name\n            age\n          }\n        }\n        pageInfo {\n          startCursor\n          endCursor\n          hasPreviousPage\n          hasNextPage\n        }\n      }\n    }\n  }`;\n\ngraphql(schema, query)\n  .then((result) =\u003e {\n    console.log(result);\n  });\n```\n\n## Supported mongoose types\n\n* Number\n* String\n* Boolean\n* Date\n* [Number]\n* [String]\n* [Boolean]\n* [Date]\n* ObjectId with ref (reference to other document, populate)\n\n## Supported query types\n\n* query\n  * singular: for example `user`\n  * plural: for example `users`\n  * [node](https://facebook.github.io/relay/docs/graphql-object-identification.html): takes a single argument, a unique `!ID`, and returns a `Node`\n  * viewer: singular and plural queries as fields\n\n## Supported query arguments\n\n* indexed fields\n* \"id\" on singular type\n* array of \"id\"s on plural type\n\nWhich means, you are able to filter like below, if the age is indexed in your mongoose model:\n\n```\nusers(age: 19) {}\nuser(id: \"mongoId1\") {}\nuser(id: \"relayId\") {}\nusers(id: [\"mongoId\", \"mongoId2\"]) {}\nusers(id: [\"relayId1\", \"relayId2\"]) {}\n```\n\n## Supported mutation types\n\n* mutation\n  * addX: for example `addUser`\n  * updateX: for example `updateUser`\n  * deleteX: for example `deleteUser`\n\n## Supported mutation arguments\n\n* scalar types\n* arrays\n* references\n\nExamples:\n```\nmutation addX {\n  addUser(input: {name: \"X\", age: 11, clientMutationId: \"1\"}) {\n    changedUserEdge {\n      node {\n        id\n        name\n      }\n    }\n  }\n}\n```\n\n```\nmutation updateX {\n  updateUser(input: {id: \"id=\", age: 10, clientMutationId: \"2\"}) {\n    changedUser {\n      id\n      name\n      age\n    }\n  }\n}\n```\n\n```\nmutation deleteX {\n  deleteUser(input: {id: \"id=\", clientMutationId: \"3\"}) {\n    ok\n  }\n}\n```\n\n## Resolve hooks\n\nYou can specify pre- and post-resolve hooks on fields in order to manipulate arguments and data passed in to the database resolve function, and returned by the GraphQL resolve function.\n\nYou can add hooks to type fields and query fields (singular \u0026 plural queries, mutations) too.\nBy passing arguments to the `next` function, you can modify the parameters of the next hook or the return value of the `resolve` function.\n\nExamples:\n- Query, mutation hooks (`viewer`, `singular`, `plural`, `mutation`)\n```javascript\nconst hooks = {\n  viewer: {\n    pre: (next, root, args, request) =\u003e {\n      // authorize the logged in user based on the request\n      authorize(request);\n      next();\n    },\n    post: (next, value) =\u003e {\n      console.log(value);\n      next();\n    }\n  },\n  // singular: {\n  //   pre: (next, root, args, context) =\u003e next(),\n  //   post: (next, value, args, context) =\u003e next()\n  // },\n  // plural: {\n  //   pre: (next, root, args, context) =\u003e next(),\n  //   post: (next, value, args, context) =\u003e next()\n  // },\n  // mutation: {\n  //   pre: (next, args, context) =\u003e next(),\n  //   post: (next, value, args, context) =\u003e next()\n  // }\n};\nconst schema = getSchema([User], {hooks});\n```\n\n- Field hooks\n```javascript\nconst UserSchema = new mongoose.Schema({\n  name: {\n    type: String,\n    hooks: {\n      pre: (next, root, args, request) =\u003e {\n        // authorize the logged in user based on the request\n        // throws error if the user has no right to request the user names\n        authorize(request);\n        next();\n      },\n      // manipulate response\n      post: [\n        (next, name) =\u003e next(`${name} first hook`),\n        (next, name) =\u003e next(`${name} \u0026 second hook`)\n      ]\n    }\n  }\n});\n```\n```\nquery UsersQuery {\n  viewer {\n    users(first: 1) {\n      edges {\n        node {\n          name\n        }\n      }\n    }\n  }\n}\n```\n```json\n{\n  \"data\": {\n    \"viewer\": {\n      \"users\": {\n        \"edges\": [\n          {\n            \"node\": {\n              \"name\": \"User0 first hook \u0026 second hook\"\n            }\n          }\n        ]\n      }\n    }\n  }\n}\n```\n\n## Test\n\n```shell\nnpm test\n```\n\n## Contributing\n\nPlease read the [CONTRIBUTING.md](https://github.com/RisingStack/graffiti-mongoose/tree/master/.github/CONTRIBUTING.md) file.\n\n## License\n\n[MIT](https://github.com/RisingStack/graffiti-mongoose/tree/master/LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frisingstack%2Fgraffiti-mongoose","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frisingstack%2Fgraffiti-mongoose","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frisingstack%2Fgraffiti-mongoose/lists"}