{"id":22377815,"url":"https://github.com/someimportantcompany/graphql-keyvalue","last_synced_at":"2025-03-26T18:22:39.037Z","repository":{"id":40704667,"uuid":"356033190","full_name":"someimportantcompany/graphql-keyvalue","owner":"someimportantcompany","description":"Standalone GraphQL Scalar type for Key-Value hashes.","archived":false,"fork":false,"pushed_at":"2023-01-07T06:34:49.000Z","size":667,"stargazers_count":0,"open_issues_count":7,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-31T23:44:36.210Z","etag":null,"topics":["graphql","keyvalue","nodejs"],"latest_commit_sha":null,"homepage":"","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/someimportantcompany.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-04-08T19:59:16.000Z","updated_at":"2021-06-01T10:03:58.000Z","dependencies_parsed_at":"2023-02-06T14:15:44.482Z","dependency_job_id":null,"html_url":"https://github.com/someimportantcompany/graphql-keyvalue","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/someimportantcompany%2Fgraphql-keyvalue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/someimportantcompany%2Fgraphql-keyvalue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/someimportantcompany%2Fgraphql-keyvalue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/someimportantcompany%2Fgraphql-keyvalue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/someimportantcompany","download_url":"https://codeload.github.com/someimportantcompany/graphql-keyvalue/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245709313,"owners_count":20659692,"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","keyvalue","nodejs"],"created_at":"2024-12-04T22:15:39.987Z","updated_at":"2025-03-26T18:22:39.009Z","avatar_url":"https://github.com/someimportantcompany.png","language":"JavaScript","readme":"# graphql-keyvalue\n\n[![NPM](https://badge.fury.io/js/graphql-keyvalue.svg)](https://npm.im/graphql-keyvalue)\n[![CI](https://github.com/someimportantcompany/graphql-keyvalue/actions/workflows/ci.yml/badge.svg)](https://github.com/someimportantcompany/graphql-keyvalue/actions/workflows/ci.yml)\n[![Coverage](https://coveralls.io/repos/github/someimportantcompany/graphql-keyvalue/badge.svg)](https://coveralls.io/github/someimportantcompany/graphql-keyvalue)\n\nStandalone GraphQL Scalar type for Key-Value hashes in JavaScript.\n\n```graphql\ntype User {\n  id: ID!\n  name: String!\n  state: KeyValue!\n}\n\nextend type Query {\n  user: User!\n}\n\nextend type Mutation {\n  updateUserState(id: ID!, state: KeyValue!) KeyValue!\n}\n```\n\n## Install\n\n```\n$ npm install --save graphql graphql-keyvalue\n```\n\nFrom your codebase, you can either use predefined items (type definition \u0026 resolver) directly in your project or define the scalar yourself \u0026 include the scalar instance in your resolvers.\n\nThe following example uses [`graphql-tools`](https://npm.im/graphql-tools) \u0026 the predefined items:\n\n```javascript\nconst assert = require('assert');\nconst { typeDefs: keyValueTypeDefs, resolvers: keyValueResolvers } = require('graphql-keyvalue');\n\nconst typeDefs = /* GraphQL */`\n  type User {\n    id: ID!\n    name: String!\n    email: String!\n    state: KeyValue!\n  }\n\n  type Query {\n    user(id: ID!): User!\n  }\n\n  type Mutation {\n    updateUserState(id: ID!, state: KeyValue!) KeyValue!\n  }\n`;\n\nconst resolvers = {\n  Query: {\n    async user(_, { id }) {\n      const user = await getUser(id);\n      return user \u0026\u0026 user.id ? user : null;\n    },\n  },\n  Mutation: {\n    async updateUserState(_, { id, state }) {\n      const user = await getUser(id);\n      assert(user \u0026\u0026 user.id, 'User not found');\n\n      // Merge in the user state\n      user.state = { ...user.state, ...state };\n\n      await setUser(id, user);\n      return user.state;\n    },\n  },\n};\n\nconst schema = makeExecutableSchema({\n  typeDefs: [ typeDefs, keyValueTypeDefs ],\n  resolvers: [ resolvers, keyValueResolvers ],\n});\n```\n\nWhereas this example uses [`apollo-server`](https://npm.im/apollo-server) \u0026 includes the scalar instance `KeyValue` in its resolvers:\n\n```javascript\nconst { ApolloServer, gql } = require('apollo-server');\nconst { KeyValue } = require('graphql-keyvalue');\n\nconst typeDefs = gql`\n  type User {\n    id: ID!\n    name: String!\n    email: String!\n    state: KeyValue!\n  }\n\n  type Query {\n    user(id: ID!): User!\n  }\n\n  type Mutation {\n    updateUserState(id: ID!, state: KeyValue!) KeyValue!\n  }\n\n  # @NOTE You must define the scalar yourself\n  scalar KeyValue\n`;\n\nconst resolvers = {\n  Query: {\n    async user(_, { id }) {\n      const user = await getUser(id);\n      return user \u0026\u0026 user.id ? user : null;\n    },\n  },\n  Mutation: {\n    async updateUserState(_, { id, state }) {\n      const user = await getUser(id);\n      assert(user \u0026\u0026 user.id, 'User not found');\n\n      // Merge in the user state\n      user.state = { ...user.state, ...state };\n\n      await setUser(id, user);\n      return user.state;\n    },\n  },\n  KeyValue,\n};\n\nconst server = new ApolloServer({\n  typeDefs,\n  resolvers,\n});\n```\n\n## Usage\n\nOnce the type definition \u0026 resolver is configured, you can send \u0026 receive simple key-value objects in GraphQL in both queries \u0026 mutations.\n\n### Query\n\n```graphql\nquery GetUser {\n  user(id: \"1\") {\n    id\n    name\n    state\n  }\n}\n```\n```json\n{\n  \"data\": {\n    \"user\": {\n      \"id\": \"1\",\n      \"name\": \"jdrydn\",\n      \"state\": {\n        \"signedInWith\": \"APPLE\",\n        \"finishedOnboarding\": true\n      }\n    }\n  }\n}\n```\n\n### Mutation\n\n```graphql\nmutation UpdateUser {\n  updateUserState(id: \"1\", state: { \"finishedOnboarding\": true })\n}\n```\n```json\n{\n  \"data\": {\n    \"updateUserState\": {\n      \"signedInWith\": \"APPLE\",\n      \"finishedOnboarding\": true\n    }\n  }\n}\n```\n\n## Notes\n\n- The scalar will pass an object for input values \u0026 expects an object to be passed for output values.\n- Trying to send/receive a JSON object/array will throw an error.\n- **Why only one-level deep?** One of the benefits of GraphQL is the strictly typed schema that is produced. This isn't trying to defy the GraphQL schema, merely extend it to cover more use-cases.\n- After more than one level? Check out [graphql-type-json](https://npmjs.im/graphql-type-json).\n- Any questions or suggestions please [open an issue](https://github.com/someimportantcompany/graphql-keyvalue/issues).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsomeimportantcompany%2Fgraphql-keyvalue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsomeimportantcompany%2Fgraphql-keyvalue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsomeimportantcompany%2Fgraphql-keyvalue/lists"}