【问题标题】:Using graphql-tools to mock a GraphQL server seems broken使用 graphql-tools 模拟 GraphQL 服务器似乎坏了
【发布时间】:2017-08-08 20:24:33
【问题描述】:

我关注了the documentation about using graphql-tools to mock a GraphQL server,但是这会引发自定义类型的错误,例如:

Expected a value of type "JSON" but received: [object Object]

关于模拟的 graphql-tools 文档明确声明它们支持自定义类型,甚至提供了使用来自 graphql-type-json 项目的 GraphQLJSON 自定义类型的示例。

我提供了 a demo of a solution on github,它使用 graphql-tools 成功模拟了 GraphQL 服务器,但这依赖于猴子修补构建的架构:

// Here we Monkey-patch the schema, as otherwise it will fall back
// to the default serialize which simply returns null.
schema._typeMap.JSON._scalarConfig.serialize = () => {
    return { result: 'mocking JSON monkey-patched' }
}

schema._typeMap.MyCustomScalar._scalarConfig.serialize = () => {
    return mocks.MyCustomScalar()
}

可能我在演示中做错了什么,但如果没有上面的猴子补丁代码,我会收到有关上述自定义类型的错误。

有没有人有比我的演示更好的解决方案,或者任何关于我可能做错了什么的线索,以及我可以如何更改代码以使演示在没有猴子修补架构的情况下工作?

demoindex.js中的相关代码如下:

/*
** As per:
** http://dev.apollodata.com/tools/graphql-tools/mocking.html
** Note that there are references on the web to graphql-tools.mockServer,
** but these seem to be out of date.
*/

const { graphql, GraphQLScalarType } = require('graphql');
const { makeExecutableSchema, addMockFunctionsToSchema } = require('graphql-tools');
const GraphQLJSON = require('graphql-type-json');

const myCustomScalarType = new GraphQLScalarType({
  name: 'MyCustomScalar',
  description: 'Description of my custom scalar type',
  serialize(value) {
    let result;
    // Implement your own behavior here by setting the 'result' variable
    result = value || "I am the results of myCustomScalarType.serialize";
    return result;
  },
  parseValue(value) {
    let result;
    // Implement your own behavior here by setting the 'result' variable
    result = value || "I am the results of myCustomScalarType.parseValue";
    return result;
  },
  parseLiteral(ast) {
    switch (ast.kind) {
      // Implement your own behavior here by returning what suits your needs
      // depending on ast.kind
    }
  }
});

const schemaString = `
    scalar MyCustomScalar
    scalar JSON

    type Foo {
        aField: MyCustomScalar
        bField: JSON
        cField: String
    }

    type Query {
        foo: Foo
    }
`;
const resolverFunctions = {
    Query: {
        foo: {
            aField: () => {
                return 'I am the result of resolverFunctions.Query.foo.aField'
            },
            bField: () => ({ result: 'of resolverFunctions.Query.foo.bField' }),
            cField: () => {
                return 'I am the result of resolverFunctions.Query.foo.cField'
            }
        },
    },
};

const mocks = {
    Foo: () => ({
        // aField: () => mocks.MyCustomScalar(),
        // bField: () => ({ result: 'of mocks.foo.bField' }),
        cField: () => {
            return 'I am the result of mocks.foo.cField'
        }
    }),

    cField: () => {
        return 'mocking cField'
    },

    MyCustomScalar: () => {
        return 'mocking MyCustomScalar'
    },

    JSON: () => {
        return { result: 'mocking JSON'}
    }
}

const query = `
{
  foo {
      aField
      bField
      cField
  }
}
`;

const schema = makeExecutableSchema({
    typeDefs: schemaString,
    resolvers: resolverFunctions
})

addMockFunctionsToSchema({
    schema,
    mocks
});

// Here we Monkey-patch the schema, as otherwise it will fall back
// to the default serialize which simply returns null.
schema._typeMap.JSON._scalarConfig.serialize = () => {
    return { result: 'mocking JSON monkey-patched' }
}

schema._typeMap.MyCustomScalar._scalarConfig.serialize = () => {
    return mocks.MyCustomScalar()
}

graphql(schema, query).then((result) => console.log('Got result', JSON.stringify(result, null, 4)));

【问题讨论】:

  • 我也看到自定义标量类型以完全相同的方式模拟失败。默认情况下,serialize 函数返回 null/没有其他人注意到这一点吗?真的有人在用这个软件吗?

标签: graphql


【解决方案1】:

如果其他人来自 Google 结果,我的解决方案是将 JSON 解析器作为参数添加到 makeExecutableSchema 调用。这里有描述:

https://github.com/apollographql/apollo-test-utils/issues/28#issuecomment-377794825

这使嘲笑对我有用。

【讨论】:

    【解决方案2】:

    我和其他一些人在实时数据源(在我的例子中是 MongoDB/Mongoose)中看到了类似的问题。我怀疑这是 graphql-tools makeExecutableSchema 内部的东西,以及它使用自定义类型摄取基于文本的模式的方式。

    这是关于这个问题的另一篇文章:How to use graphql-type-json package with GraphQl

    我没有尝试在代码中构建架构的建议,所以无法确认它是否有效。

    我当前的解决方法是在将 JSON 字段(在连接器中)提供给客户端(并在客户端解析)时对它们进行字符串化,反之亦然。有点笨拙,但我并没有真正使用 GraphQL 来查询和/或选择性地提取 JSON 对象中的属性。对于我怀疑的大型 JSON 对象,这不是最佳选择。

    【讨论】:

      猜你喜欢
      • 2017-07-01
      • 2020-01-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 2020-12-02
      • 2021-04-16
      • 2019-03-22
      • 2021-03-01
      相关资源
      最近更新 更多