使用graphql工具来模拟一个GraphQL服务器似乎打破
问题描述:
我已经按照the documentation about using graphql-tools to mock a GraphQL server,然而这将引发错误的自定义类型,如:使用graphql工具来模拟一个GraphQL服务器似乎打破
Expected a value of type "JSON" but received: [object Object]
关于明确嘲讽graphql工具文档指出他们支持自定义类型,甚至提供了使用graphql-type-json项目中的GraphQLJSON自定义类型的示例。
我提供a demo of a solution on github它采用graphql的工具来成功地嘲笑一个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()
}
也许我做错了什么在我的演示,但没有上面的猴子补丁代码,我得到上述自定义类型的错误。
有没有人有比我的演示或任何线索更好的解决方案,我可能做错了什么,以及如何改变代码,使演示工作没有猴子修补模式?
在演示index.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)));
答
我和其他几个人看到使用实时数据源的一个类似的问题(在我的情况的MongoDB /猫鼬)。我怀疑这是graphql-tools makeExecutableSchema内部的东西,以及它使用自定义类型摄取基于文本的模式的方式。
下面是在这个问题上另一篇文章:How to use graphql-type-json package with GraphQl
我还没有尝试过打造代码架构的建议,所以不能确认是否有用,或者没有。
我当前的解决方法是将JSON字段(在连接器中)提供给客户端(并在客户端进行解析)时进行字符串化,反之亦然。有点笨拙,但我没有真正使用GraphQL来查询和/或选择性地提取JSON对象中的属性。这对于我怀疑的大型JSON对象来说不是最佳的。
我也看到嘲笑与自定义标量类型完全相同的方式失败。序列化函数默认情况下返回null /是否没有人注意到这一点?有人真的在使用该软件吗? –