jjzjj

javascript - 没有 `graphql-tools` 的 GraphQL 自定义标量定义

coder 2025-02-26 原文

阅读官方文档中的本演练后:

http://graphql.org/graphql-js/object-types/

我很困惑如何在没有第三方库的情况下制作自定义标量类型解析器。这是文档中的示例代码:

var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');

// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
  type RandomDie {
    numSides: Int!
    rollOnce: Int!
    roll(numRolls: Int!): [Int]
  }

  type Query {
    getDie(numSides: Int): RandomDie
  }
`);

// This class implements the RandomDie GraphQL type
class RandomDie {
  constructor(numSides) {
    this.numSides = numSides;
  }

  rollOnce() {
    return 1 + Math.floor(Math.random() * this.numSides);
  }

  roll({numRolls}) {
    var output = [];
    for (var i = 0; i < numRolls; i++) {
      output.push(this.rollOnce());
    }
    return output;
  }
}

// The root provides the top-level API endpoints
var root = {
  getDie: function ({numSides}) {
    return new RandomDie(numSides || 6);
  }
}

var app = express();
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');

我知道我可以使用 graphql-tools 从基于字符串的类型定义和解析器对象创建“可执行模式”。我想知道的是,为什么没有可用于定义和解析自定义标量类型的较低级别/命令式 graphql-js API?换句话说,graphql-tools 是如何工作的?

提前致谢!


编辑:

这里是一些概述问题的示例代码。在第 4 行,您可以看到我正在导入 GraphQLJSON,但它从未被使用过。我知道如何使用 graphql-tools 来完成这项工作,但我想了解如何它是如何工作的。换句话说,如果 graphql-tools 不存在,我将如何注入(inject)自定义标量类型,同时仍然使用 graphql 语法编写我的模式?据我所知,唯一的 graphql-js 解决方案是使用非声明性方法来编写模式(下面的第二个示例)

import express from 'express';
import graphqlHTTP from 'express-graphql';
import { buildSchema } from 'graphql';
import GraphQLJSON from 'graphql-type-json'; // where should I inject this?

const schema = buildSchema(`
  type Image {
    id: ID!
    width: Int!
    height: Int!
    metadata: JSON!
  }

  type Query {
    getImage(id: ID!): Image!
  }

  scalar JSON
`);

class Image {
  constructor(id) {
    this.id = id;
    this.width = 640;
    this.height = 480;
  }
  metadata() {
    // what do I need to do in order to have this return value parsed by GraphQLJSON
    return { foo: 'bar' };
  }
}

const rootValue = {
  getImage: function({ id }) {
    return new Image(id);
  },
};

const app = express();
app.use(
  '/graphql',
  graphqlHTTP({
    schema: schema,
    rootValue: rootValue,
    graphiql: true,
  })
);
app.listen(4000);

运行此查询:

{
    getImage(id: "foo") {
    id
    width
    height
    metadata
  }
}

导致此错误:

需要一个\"JSON\"类型的值但收到:[object Object]

我正在寻找的答案将帮助我在不使用 graphql-tools 的情况下返回 JSON 类型。我不反对这个库,但我必须使用第三方库来处理 graphql-js 中类型解析系统如此基础的东西,这对我来说似乎很奇怪。在采用它之前,我想更多地了解为什么需要这种依赖。

这里有另一种方法来完成这项工作:

import { GraphQLObjectType, GraphQLInt, GraphQLID } from 'graphql/type';

const foo = new GraphQLObjectType({
  name: 'Image',
  fields: {
    id: { type: GraphQLID },
    metadata: { type: GraphQLJSON },
    width: { type: GraphQLInt },
    height: { type: GraphQLInt },
  },
});

但是,这不允许我使用 graphql 语法编写我的模式,这是我的目标。

最佳答案

更新

经过一番澄清后,您似乎正在尝试将自定义标量添加到使用模式语言创建的模式中。由于 buildSchema(或其他客户端工具)构建的模式没有用于 serializeparseValueparseLiteral 的处理函数绑定(bind),您需要修改构建的架构以包含这些。你可以做类似的事情

import { buildSchema } from 'graphql'
import GraphQLJSON from 'graphql-type-json'

const definition = `
type Foo {
  config: JSON
}

scalar JSON

Query {
  readFoo: Foo
}

schema {
  query: Query
}`

const schema = buildSchema(definition)
Object.assign(schema._typeMap.JSON, GraphQLJSON)

或者,您也可以执行以下操作,这可能有助于将标量重命名为其他名称

Object.assign(schema._typeMap.JSON, {
  name: 'JSON',
  serialize: GraphQLJSON.serialize,
  parseValue: GraphQLJSON.parseValue,
  parseLiteral: GraphQLJSON.parseLiteral
})

原始答案

buildSchema 确实创建了一个模式,但该模式没有解析、序列化、parseLiteral 等与之关联的函数。我相信 graphql-tools 只允许您将解析器函数映射到字段,这在您尝试创建自定义标量时对您没有帮助。

graphql-js 有一个 GraphQLScalarType,您可以使用它来构建自定义标量。请参阅 http://graphql.org/graphql-js/type/#graphqlscalartype 上的官方文档和示例

npm中还有几个package可以作为例子

我觉得非常有用的一个是 https://github.com/taion/graphql-type-json/blob/master/src/index.js

例如,如果您想创建一个 base64 类型,将字符串存储为 base64 并在响应中返回之前解码 base64 字符串,您可以像这样创建自定义 base64 标量

import { GraphQLScalarType, GraphQLError, Kind } from 'graphql'

const Base64Type = new GraphQLScalarType({
  name: 'Base64',
  description: 'Serializes and Deserializes Base64 strings',
  serialize (value) {
    return (new Buffer(value, 'base64')).toString()
  },
  parseValue (value) {
    return (new Buffer(value)).toString('base64')
  },
  parseLiteral (ast) {
    if (ast.kind !== Kind.STRING) {
      throw new GraphQLError('Expected Base64 to be a string but got: ' + ast.kind, [ast])
    }
    return (new Buffer(ast.value)).toString('base64')
  }
})

关于javascript - 没有 `graphql-tools` 的 GraphQL 自定义标量定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47824603/

有关javascript - 没有 `graphql-tools` 的 GraphQL 自定义标量定义的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby - 如何使用文字标量样式在 YAML 中转储字符串? - 2

    我有一大串格式化数据(例如JSON),我想使用Psychinruby​​同时保留格式转储到YAML。基本上,我希望JSON使用literalstyle出现在YAML中:---json:|{"page":1,"results":["item","another"],"total_pages":0}但是,当我使用YAML.dump时,它不使用文字样式。我得到这样的东西:---json:!"{\n\"page\":1,\n\"results\":[\n\"item\",\"another\"\n],\n\"total_pages\":0\n}\n"我如何告诉Psych以想要的样式转储标量?解

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  6. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  7. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  8. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  9. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  10. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

随机推荐