GraphQL Type JSON 项目使用教程

GraphQL Type JSON 项目使用教程

graphql-type-jsonJSON scalar type for GraphQL.js项目地址:https://gitcode.com/gh_mirrors/gr/graphql-type-json

1. 项目的目录结构及介绍

graphql-type-json/
├── LICENSE
├── README.md
├── package.json
├── src/
│   ├── index.js
│   └── index.test.js
└── yarn.lock
  • LICENSE: 项目的许可证文件。
  • README.md: 项目的说明文档。
  • package.json: 项目的依赖管理文件。
  • src/: 源代码目录。
    • index.js: 项目的主入口文件。
    • index.test.js: 项目的测试文件。
  • yarn.lock: Yarn 包管理器的锁定文件。

2. 项目的启动文件介绍

项目的启动文件是 src/index.js。这个文件定义了 GraphQLJSON 对象,它是一个自定义的标量类型,用于在 GraphQL 中处理 JSON 数据。

import { GraphQLScalarType } from 'graphql';
import { Kind } from 'graphql/language';

const GraphQLJSON = new GraphQLScalarType({
  name: 'JSON',
  description: 'The `JSON` scalar type represents JSON values as specified by ECMA-404.',
  serialize(value) {
    return value;
  },
  parseValue(value) {
    return value;
  },
  parseLiteral(ast) {
    switch (ast.kind) {
      case Kind.STRING:
      case Kind.BOOLEAN:
        return ast.value;
      case Kind.INT:
      case Kind.FLOAT:
        return parseFloat(ast.value);
      case Kind.OBJECT:
        return ast.fields.reduce((accumulator, field) => {
          accumulator[field.name.value] = parseLiteral(field.value);
          return accumulator;
        }, {});
      case Kind.LIST:
        return ast.values.map(parseLiteral);
      default:
        return null;
    }
  },
});

export default GraphQLJSON;

3. 项目的配置文件介绍

项目的配置文件主要是 package.json。这个文件包含了项目的元数据和依赖信息。

{
  "name": "graphql-type-json",
  "version": "0.3.1",
  "description": "JSON scalar type for GraphQL.js",
  "main": "src/index.js",
  "scripts": {
    "test": "jest"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/taion/graphql-type-json.git"
  },
  "keywords": [
    "graphql",
    "json"
  ],
  "author": "Jimmy Jia",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/taion/graphql-type-json/issues"
  },
  "homepage": "https://github.com/taion/graphql-type-json#readme",
  "devDependencies": {
    "jest": "^23.6.0"
  },
  "peerDependencies": {
    "graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
  }
}
  • name: 项目的名称。
  • version: 项目的版本号。
  • description: 项目的描述。
  • main: 项目的主入口文件。
  • scripts: 项目的脚本命令,例如测试命令 npm test
  • repository: 项目的代码仓库信息。
  • keywords: 项目的关键词。
  • author: 项目的作者。
  • license: 项目的许可证。
  • bugs: 项目的问题追踪地址。
  • homepage: 项目的主页。
  • devDependencies: 开发依赖包。
  • peerDependencies: 对等依赖包。

graphql-type-jsonJSON scalar type for GraphQL.js项目地址:https://gitcode.com/gh_mirrors/gr/graphql-type-json

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白秦朔Beneficient

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值