profile
viewpoint

Ask questionsMockedProvider can't mock queries with client directives

Intended outcome:

When using a MockedProvider and a query with client directives, the client directives should resolve.

Query example:

query Cats {
  cats {
    name @client
    breed
  }
}

Actual outcome:

Error: Network error: No more mocked responses for the query

How to reproduce the issue:

const CATS = gql`
  query Cats {
    cats {
      name @client
      breed
    }
  }
`;

const Container: React.FC<{}> = () => (
  <Query query={CATS}>
    {({ data, loading, error }) => {
      if (error) return <pre>{JSON.stringify(error)}</pre>;
      if (loading) return <pre>Loading</pre>;
      if (data) return <pre>{JSON.stringify(data)}</pre>;
    }}
  </Query>
);

const waitForUpdate = async wrapper => {
  await new Promise(resolve => setTimeout(resolve));
  wrapper.update();
};

const data = {
  cats: { name: 'Fluffy', breed: 'kitten' }
}

it('loads', async () => {
  const wrapper = mount(
    <MockedProvider
      mocks={[
        {
          request: {
            query: CATS
          },
          result: {
            data,
          }
        }
      ]}
    >
      <Container />
    </MockedProvider>
  );

  await waitForUpdate(wrapper);
  expect(wrapper).toHaveLength(1);
  expect(wrapper.find('pre').text()).toEqual(
    JSON.stringify(data)
  );
});

This test fails with:

    Expected value to equal:
      "{\"cats\":{\"name\":\"Fluffy\",\"breed\":\"kitten\"}}"
    Received:
      "{\"graphQLErrors\":[],\"networkError\":{},\"message\":\"Network error: No more mocked responses for the query: query Cats {\\n  cats {\\n    name @client\\n    breed\\n    __typename\\n  }\\n}\\n, variables: {}\"}"

EDIT: The test passes if I remove @client from the query.

Version

System: OS: Linux 5.2 Arch Linux undefined Binaries: Node: 11.4.0 - ~/.nvm/versions/node/v11.4.0/bin/node Yarn: 1.17.3 - /usr/bin/yarn npm: 6.4.1 - ~/.nvm/versions/node/v11.4.0/bin/npm Browsers: Firefox: 68.0.1 npmPackages: apollo-boost: ^0.4.3 => 0.4.3 apollo-client: ^2.6.3 => 2.6.3 react-apollo: ^2.5.8 => 2.5.8

apollographql/react-apollo

Answer questions hwillson

@SimenB I definitely agree - I've been hit by this many times as well. Would you mind creating an FR for this in the https://github.com/apollographql/apollo-feature-requests repo?

useful!
source:https://uonfu.com/
Github User Rank List