我已經按照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)));
我也看到嘲笑與自定義標量類型完全相同的方式失敗。序列化函數默認情況下返回null /是否沒有人注意到這一點?有人真的在使用該軟件嗎? –