-
Notifications
You must be signed in to change notification settings - Fork 787
MockedProvider query with Fragments throwing __typename warning #1747
Comments
@justinlevi I am running into this exact same issue, and haven't been able to find a solution anywhere. Did you happen to ever find anything? |
Following this blog post from Apollo also getting these errors/warnings. I'm also using fragments in my queries. I do have the prop in |
I'm running into the same issue too, tried to resolve using |
@mptap I ended up solving this by hand rolling my own
Hope this helps! |
Any updates here? The only solution for me, and for now is to add
But... why I need to add typename only for fragments but not to queries and mutations? :) |
Having the same issue in testing. Not sure what the best solution is, as |
Hey any update on this? I'm having the same issue testing. Here's my test file. /* eslint-env jest */
import React from 'react';
import { mount } from 'enzyme';
import { MockedProvider } from 'react-apollo/test-utils';
import ServiceSection from '../../components/ServiceSection';
import { GET_SERVICES } from '../../queries';
const servicesMock = {
request: {
query: GET_SERVICES
},
result: {
data: {
services: {
all: [
{
title: 'Servie 1',
slug: 'service-1',
excerpt: 'Some Excerpt',
content: '<p>Some Content</p>',
featuredImage: {
sourceUrl: 'http://test.com/service-1.png',
mediaDetails: {
sizes: [
{
name: 'medium_large',
sourceUrl: 'http://test.com/service-1.png',
width: 700
},
{
name: 'large',
sourceUrl: 'http://test.com/service-1.png',
width: 700
}
]
}
}
},
{
title: 'Servie 2',
slug: 'service-2',
excerpt: 'Some Excerpt 2',
content: '<p>Some Content 2</p>',
featuredImage: {
sourceUrl: 'http://test.com/service-2.png',
mediaDetails: {
sizes: [
{
name: 'medium_large',
sourceUrl: 'http://test.com/service-2.png',
width: 700
},
{
name: 'large',
sourceUrl: 'http://test.com/service-2.png',
width: 800
}
]
}
}
}
]
}
}
}
};
describe('Services Section', () => {
it('renders when given query', async () => {
const wrapper = mount(
<MockedProvider mocks={[servicesMock]} addTypename={false}>
<ServiceSection />
</MockedProvider>
);
await new Promise(resolve => setTimeout(resolve));
wrapper.update();
});
}); And here's the errors I get yarn run v1.10.1
warning ../../../package.json: No license field
$ NODE_ENV=test jest --verbose __tests__/components/services.test.js
PASS __tests__/components/services.test.js
Services Section
✓ renders when given query (56ms)
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:51
You're using fragments in your queries, but either don't have the addTypename:
true option set in Apollo Client, or you are trying to write a fragment to the store without the __typename.
Please turn on the addTypename option and include __typename when writing fragments so that Apollo Client
can accurately match fragments.
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:52
Could not find __typename on Fragment Service { title: 'Servie 1',
slug: 'service-1',
excerpt: 'Some Excerpt',
content: '<p>Some Content</p>',
featuredImage:
{ sourceUrl: 'http://test.com/service-1.png',
mediaDetails: { sizes: [Array] } } }
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:53
DEPRECATION WARNING: using fragments without __typename is unsupported behavior and will be removed in future versions of Apollo client. You should fix this and set addTypename to true now.
console.error node_modules/apollo-cache-inmemory/lib/bundle.umd.js:786
WARNING: heuristic fragment matching going on!
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:761
Missing field __typename in {
"title": "Servie 1",
"slug": "service-1",
"excerpt": "Some Excerpt",
"content": "<p>Some C
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:761
Missing field id in {
"title": "Servie 1",
"slug": "service-1",
"excerpt": "Some Excerpt",
"content": "<p>Some C
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:51
You're using fragments in your queries, but either don't have the addTypename:
true option set in Apollo Client, or you are trying to write a fragment to the store without the __typename.
Please turn on the addTypename option and include __typename when writing fragments so that Apollo Client
can accurately match fragments.
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:52
Could not find __typename on Fragment Service { title: 'Servie 2',
slug: 'service-2',
excerpt: 'Some Excerpt 2',
content: '<p>Some Content 2</p>',
featuredImage:
{ sourceUrl: 'http://test.com/service-2.png',
mediaDetails: { sizes: [Array] } } }
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:53
DEPRECATION WARNING: using fragments without __typename is unsupported behavior and will be removed in future versions of Apollo client. You should fix this and set addTypename to true now.
console.error node_modules/apollo-cache-inmemory/lib/bundle.umd.js:786
WARNING: heuristic fragment matching going on!
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:761
Missing field __typename in {
"title": "Servie 2",
"slug": "service-2",
"excerpt": "Some Excerpt 2",
"content": "<p>Some
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:761
Missing field id in {
"title": "Servie 2",
"slug": "service-2",
"excerpt": "Some Excerpt 2",
"content": "<p>Some
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:51
You're using fragments in your queries, but either don't have the addTypename:
true option set in Apollo Client, or you are trying to write a fragment to the store without the __typename.
Please turn on the addTypename option and include __typename when writing fragments so that Apollo Client
can accurately match fragments.
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:52
Could not find __typename on Fragment Service { title: 'Servie 1',
slug: 'service-1',
excerpt: 'Some Excerpt',
featuredImage:
{ type: 'id',
generated: true,
id: '$ROOT_QUERY.services.nodes.0.featuredImage',
typename: undefined } }
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:53
DEPRECATION WARNING: using fragments without __typename is unsupported behavior and will be removed in future versions of Apollo client. You should fix this and set addTypename to true now.
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:51
You're using fragments in your queries, but either don't have the addTypename:
true option set in Apollo Client, or you are trying to write a fragment to the store without the __typename.
Please turn on the addTypename option and include __typename when writing fragments so that Apollo Client
can accurately match fragments.
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:52
Could not find __typename on Fragment Service { title: 'Servie 2',
slug: 'service-2',
excerpt: 'Some Excerpt 2',
featuredImage:
{ type: 'id',
generated: true,
id: '$ROOT_QUERY.services.nodes.1.featuredImage',
typename: undefined } }
console.warn node_modules/apollo-cache-inmemory/lib/bundle.umd.js:53
DEPRECATION WARNING: using fragments without __typename is unsupported behavior and will be removed in future versions of Apollo client. You should fix this and set addTypename to true now. |
This is quite an annoying problem, what is the update on this? |
This looks to be an accurate warning about misusing fragments without a Here is an example bit of code I use to setup my provider: <MockedProvider cache={createApolloCache(require('../../../../fragmentTypes.json')}> with import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'
import { IntrospectionResultData } from 'apollo-cache-inmemory/lib/types'
export default function createApolloCache(introspectionQueryResultData: object) {
return new InMemoryCache({
fragmentMatcher: new IntrospectionFragmentMatcher({
introspectionQueryResultData: introspectionQueryResultData as IntrospectionResultData,
}),
})
} |
Thanks for the response. Is there any way we could get this requirement reflected/further explained in documentation somewhere? |
Please feel free to PR a change to the component docs or the site docs and add me as a reviewer, I'll be happy to take a look at what you come up with. |
So I've read the comments but I don't see the solution. I tried @rosskevin's solution I also tried adding
but had same issue |
We use I found a solution to avoid updating all the stubs with import {
IntrospectionFragmentMatcher,
InMemoryCache,
} from 'apollo-cache-inmemory';
import introspectionQueryResultData from '../../constants/fragmentTypes.json';
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData,
});
// ...then in the test:
<MockedProvider
mocks={mocks}
addTypename={false}
cache={
new InMemoryCache({
addTypename: false,
fragmentMatcher,
})
}
> Setting Hope that helps 😃 |
@madisonbullard where does |
May be a little bit dangerous and I haven't test it extensivelly, but instead of putting
|
@ryanirilli You can read about GraphQL introspection here: https://graphql.org/learn/introspection/ We copy/paste the results of a
|
I tried this example. I generated the |
Same as above - if I remove |
Perhaps there's something missing in this answer but it's not working as is. I tried @madisonbullard's suggestion to set
|
I'm having the same as @zhouzi, worth to say; the MockProvider now does return (generate?) some data, but it never reaches the component due to the failed write. |
I also had the same as @zhouzi and @smejier, is there a solution for this? |
Also tried to pass |
I just disabled my caching since cache takes the __typename and id <MockedProvider
mocks={ ... }
addTypename={false}
defaultOptions={{
watchQuery: { fetchPolicy: 'no-cache' },
query: { fetchPolicy: 'no-cache' },
}}
> ... |
I confirm that adding the |
Intended outcome:
When I run my tests I expect them to pass without an error .
Actual outcome:
I get a massive warning about using fragments without __typename
Here are my tests:
apolloProxy.test.js
Here are my queries:
apolloProxy.js
Here is my mock data.js:
Version
I have tried adding __typename throughout my fragment and my query but the warning persists. My tests pass, but I'd like to get rid of the warnings if possible.
Reference where I found the above approach:
https://stackoverflow.com/questions/45700550/how-to-use-a-mocked-data-with-react-apollo-for-tests#
Wondering if I need to add an optimisticResponse???
Thanks for any direction
The text was updated successfully, but these errors were encountered: