question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi all,

First of all, I love Apollo Client; big thanks to the dev team.

Form responses:

Intended outcome: upgrade from react-apollo 1.2 to 2.0 Actual outcome: I have no idea how to mock anything anymore How to reproduce the issue: Attempt to find information about mocking with 2.0 Versions: as above

Discursive account:

I’ve had to upgrade to 2.0 (react-apollo) by the lack of (remaining) documentation on how to silence the heuristic fragment matcher warning. (The documentation I can find concerns 2.0).

After having carefully rewritten my initialization code to allow use createHttpLink and friends, I am mystified (and cannot find documentation) on how to do mocking. I have several hundred lines of code specifying a mock NetworkInterface, as per the old docs.

What’s the new equivalent to mockNetworkInterfaceSchema and friends? Is this documented anywhere?

Anyway, grateful for all your hard work and look forward to learning how I can unfsck my codebase, even as I sit anxiously by 😃

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
luisgrasescommented, Feb 8, 2018

You can Mock using: https://www.apollographql.com/docs/graphql-tools/mocking.html

You can use that to create a HOC that passes the response to a component through a data prop.

Something like:

import React from 'react'
import schema from 'gql-schema.js'
import { graphql } from 'graphql';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';

const executableSchema = makeExecutableSchema({ typeDefs: schema });
addMockFunctionsToSchema({ schema: executableSchema });

const GQLMockHOC = (query) => (Component) => 
  class GQLMock extends React.Component {

    constructor(props) {
      super(props);
      this.state = { data: null}
    }

    async componentWillMount() {
      const response = await graphql(executableSchema, query)
      this.setState({ data: response.data })
    }

    render() {
      const { data } = this.state
      return (data) ? <Component data={data} {...this.props} /> : <div>Loading</div>
  }

}

export default  GQLMockHOC

Take in account that you need to change the schema import to actually import your own schema.

Then you can use it like:

const query = `
  query Books {
    books { id, title, description }
  }
`;

const ComponentWithData = GQLMockHOC(query)(MyComponent)

ComponentWithData will have a data prop with the fake response.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mocking 2.0 - Proxytool
Mocking is a developer tool that allows you to simulate the behavior of real objects in your code. It's a way to test...
Read more >
Mockito (Mockito 2.0.2-beta API) - javadoc.io
Creates mock with a specified strategy for its answers to interactions. It's quite advanced feature and typically you don't need it to write...
Read more >
Moq Mocking with Identity 2.0 Database - Stack Overflow
I successfully set up an Integration Test using mocking with Moq against my BusAct controller in an MVC 5, Entity Framework 6 app...
Read more >
10.9 Integrating Mock Objects into Your Testing with NMock 2.0
10.9 Integrating Mock Objects into Your Testing with NMock 2.0 Developer testing can become very difficult when a project has external dependencies such...
Read more >
Simple.Mocking 2.0.0 - NuGet Gallery
Easy to use framework for creating mock objects, fakes, stubs with an intuitive and fluent API. ... Mocking -Version 2.0.0.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found