ich the documentation about using graphql-tools to mock a GraphQL server befolgt haben, jedoch wirft dies einen Fehler für benutzerdefinierte Typen, wie zB:graphql-Tools Mit einem GraphQL Server zu verspotten scheint
Expected a value of type "JSON" but received: [object Object]
Die Dokumentation graphql-Tools über explizit spöttisch erklärt, dass sie unterstützt benutzerdefinierte Typen und bietet sogar ein Beispiel für die Verwendung des benutzerdefinierten Typs GraphQLJSON aus dem Projekt graphql-type-json.
Ich habe a demo of a solution on github vorgesehen, die graphql-Tools verwendet erfolgreich GraphQL Server zu verspotten, aber dies beruht auf Affen-Patching des integrierten Schema:
// 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()
}
Möglicherweise etwas falsch in meiner Demo ich tue, Aber ohne den Code, der oben mit Affe gepatcht wurde, bekomme ich den Fehler bezüglich der oben erwähnten benutzerdefinierten Typen.
Hat jemand eine bessere Lösung als meine Demo, oder irgendwelche Hinweise, was ich falsch machen könnte, und wie kann ich den Code ändern, so dass die Demo funktioniert, ohne das Schema zu patchen?
Der entsprechende Code in der Demo ist index.js
wie folgt:
/*
** 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)));
Ich sehe auch Spott mit benutzerdefinierten skalaren Typen genau auf die gleiche Weise scheitern. Die Serialisierungsfunktion ist standardmäßig etwas, das null zurückgibt/Hat niemand sonst dies bemerkt? Benutzt jemand tatsächlich die Software? –