【问题标题】:GraphQL custom scalar definition without `graphql-tools`没有 `graphql-tools` 的 GraphQL 自定义标量定义
【发布时间】:2018-05-29 04:58:03
【问题描述】:

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

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 不存在,我将如何注入自定义标量类型,同时仍使用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
  }
}

导致此错误:

Expected a value of type \"JSON\" but received: [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 语法创作我的架构,这是我的目标。

【问题讨论】:

    标签: javascript graphql graphql-js


    【解决方案1】:

    更新

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

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

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

    npm 中还有几个包可以作为示例使用

    我觉得非常有用的是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')
      }
    })
    

    【讨论】:

    • 感谢您的回复。我了解如何创建 自定义标量类型——这是有据可查的。我将编辑我的问题以提供一个更具体的示例来概述问题所在
    • 如果您的问题是如何为使用字符串定义的自定义标量设置 serializeparseValueparseLiteral 处理程序,则需要使用 graphql-js buildSchema创建架构,或任何工具从中创建架构,然后您可以设置schema._typeMap.CustomScalarName.serialize = serializeHandler
    • 我确实尝试过schema._typeMap.JSON = GraphQLJSON,但这没有用
    • 你不能那样做。您需要将 scalar JSON 添加到字符串定义中,然后您可以执行类似 Object.assign(schema._typeMap.JSON, GraphQLJSON) 的操作
    • Object.assign 工作——太棒了!您能否编辑您的答案,我会将其标记为接受未来的观众?谢谢你
    猜你喜欢
    • 2020-08-03
    • 2020-10-21
    • 2018-02-12
    • 1970-01-01
    • 2022-08-05
    • 2019-10-12
    • 2021-09-24
    • 2020-08-03
    • 2018-06-30
    相关资源
    最近更新 更多