2017-03-17 64 views
1

我已經按照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))); 
+0

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

回答

0

我和其他幾個人看到使用實時數據源的一個類似的問題(在我的情況的MongoDB /貓鼬)。我懷疑這是graphql-tools makeExecutableSchema內部的東西,以及它使用自定義類型攝取基於文本的模式的方式。

下面是在這個問題上另一篇文章:How to use graphql-type-json package with GraphQl

我還沒有嘗試過打造代碼架構的建議,所以不能確認是否有用,或者沒有。

我當前的解決方法是將JSON字段(在連接器中)提供給客戶端(並在客戶端進行解析)時進行字符串化,反之亦然。有點笨拙,但我沒有真正使用GraphQL來查詢和/或選擇性地提取JSON對象中的屬性。這對於我懷疑的大型JSON對象來說不是最佳的。