【问题标题】:Mocking apollo graphql client模拟 apollo graphql 客户端
【发布时间】:2019-06-22 17:01:57
【问题描述】:

我编写了以下节点客户端,它与 graphql 服务器交互并使用 apollo-tools 节点模块。我无法在节点中找到以下任何模拟测试。请告诉我有没有办法模拟下面的代码。

const batchIdFetchClient = new ApolloClient({
        uri: `http://localhost:3435`,
        fetch,
    })

    await batchfetchclient.query({
        query: gql`
  query batcidId($batchid: String!) {
    batchIds(batchid: $batchid){
        batchrenewedId 
    }
  }
`,
        variables: {
            batchid: 'exdsfsdfsfdid1234', // As of now hardcoded
        },
    })
        .then(data => {
            logger.info('BatchId Database Successful Response =>' + JSON.stringify(data))
        })
        .catch(error => {
            logger.error('BatchId Database Error Response =>' + error)
        })

【问题讨论】:

    标签: node.js apollo react-apollo apollo-client apollo-server


    【解决方案1】:

    也许你可以尝试使用easygraphql-tester,它会是这样的:

    您需要传递您的架构才能模拟它

    const EasyGraphQLTester = require('easygraphql-tester')
    
    const tester = new EasyGraphQLTester(schema)
    
    const query = gql`
      query batcidId($batchid: String!) {
        batchIds(batchid: $batchid){
          batchrenewedId 
        }
      }
    `
    
    const mock = tester.mock({
      query,
      variables: {
        batchid: 'exdsfsdfsfdid1234', // As of now hardcoded
      }
    })
    
    console.log(mock)
    

    另外,如果你想拥有一个特定的数据,你可以设置夹具。

    【讨论】:

      猜你喜欢
      • 2022-12-12
      • 1970-01-01
      • 2020-09-19
      • 2019-06-30
      • 2019-06-10
      • 2021-10-12
      • 2021-05-30
      • 2018-02-14
      • 2020-07-22
      相关资源
      最近更新 更多