Repository: FormidableLabs/urql
Pull request #893 (2020-07-05 16:53:16)

(graphcache) - add special case for fetching an introspection result in graphcache
<!--
  Thanks for opening a pull request! We appreciate your dedication and help!
  Before submitting your pull request, please make sure to read our CONTRIBUTING guide.

  The best contribution is always a PR! However, if you're starting to work on a large
  change, it's best to make sure to open an issue first.

  If this PR is already related to an issue, please reference it like so:
  Resolves #123
-->

## Summary

Fixes: https://github.com/FormidableLabs/urql/issues/889

This adds a special case to our schemaPredicates to ignore the `__schema` field

## Set of changes

- ads a special case to our schemaPredicates to ignore the `__schema` field


changeset-bot[bot]: ###  🦋  Changeset is good to go

Latest commit: 8df02edb61e0cd9b0e3c18c295d00e2ec4c7cdb8

**We got this.**

<details><summary>This PR includes changesets to release 1 package</summary>

  | Name                      | Type  |
| ------------------------- | ----- |
| @urql/exchange-graphcache | Patch |

</details>

Not sure what this means? [Click here  to learn what changesets are](https://github.com/changesets/changesets/blob/master/docs/adding-a-changeset.md).

[Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/FormidableLabs/urql/new/fix-introspection-fetch?filename=.changeset/afraid-carrots-applaud.md&value=---%0A%22%40urql%2Fexchange-graphcache%22%3A%20patch%0A---%0A%0A(graphcache)%20-%20add%20special%20case%20for%20fetching%20an%20introspection%20result%20in%20graphcache%0A)



Patch:
diff --git a/.changeset/afraid-dragons-train.md b/.changeset/afraid-dragons-train.md
new file mode 100644
index 00000000..bf06b23c
--- /dev/null
+++ b/.changeset/afraid-dragons-train.md
@@ -0,0 +1,5 @@
+---
+'@urql/exchange-graphcache': patch
+---
+
+Add special-case for fetching an introspection result in our schema-checking, this avoids an error when urql-devtools fetches the backend graphql schema.
diff --git a/docs/advanced/auto-populate-mutations.md b/docs/advanced/auto-populate-mutations.md
index c1a6d666..6677a1cd 100644
--- a/docs/advanced/auto-populate-mutations.md
+++ b/docs/advanced/auto-populate-mutations.md
@@ -28,7 +28,7 @@ Afterwards we can set the `populateExchange` up by adding it to our list of `exc
 client options.
 
 ```ts
-import { createClient, dedupExchange, populateExchange, fetchExchange } from '@urql/core';
+import { createClient, dedupExchange, fetchExchange } from '@urql/core';
 import { populateExchange } from '@urql/exchange-populate';
 
 const client = createClient({
diff --git a/exchanges/graphcache/src/ast/schemaPredicates.ts b/exchanges/graphcache/src/ast/schemaPredicates.ts
index 3fedb774..f936711d 100644
--- a/exchanges/graphcache/src/ast/schemaPredicates.ts
+++ b/exchanges/graphcache/src/ast/schemaPredicates.ts
@@ -22,6 +22,7 @@ export const isFieldNullable = (
   typename: string,
   fieldName: string
 ): boolean => {
+  if (fieldName === '__schema') return true;
   const field = getField(schema, typename, fieldName);
   return !!field && isNullableType(field.type);
 };
@@ -42,6 +43,7 @@ export const isFieldAvailableOnType = (
   typename: string,
   fieldName: string
 ): boolean => {
+  if (fieldName === '__schema') return true;
   return !!getField(schema, typename, fieldName);
 };
 


Test patch:
diff --git a/exchanges/graphcache/src/store/store.test.ts b/exchanges/graphcache/src/store/store.test.ts
index ee687fdf..13c487f2 100644
--- a/exchanges/graphcache/src/store/store.test.ts
+++ b/exchanges/graphcache/src/store/store.test.ts
@@ -6,6 +6,7 @@ import { write, writeOptimistic } from '../operations/write';
 import * as InMemoryData from './data';
 import { Store } from './store';
 import { noop } from '../test-utils/utils';
+import { getIntrospectionQuery, parse } from 'graphql';
 
 const Appointment = gql`
   query appointment($id: String) {
@@ -385,10 +386,9 @@ describe('Store with OptimisticMutationConfig', () => {
       {
         query: connection,
       },
-      // @ts-ignore
       {
         exercisesConnection: null,
-      }
+      } as any
     );
     let { data } = query(store, { query: connection });
 
@@ -803,4 +803,13 @@ describe('Store with storage', () => {
     );
     expect(warnMessage).toContain('https://bit.ly/2XbVrpR#24');
   });
+
+  it('should not warn for an introspection result root', function () {
+    // eslint-disable-next-line
+    const schema = require('../test-utils/simple_schema.json');
+    const store = new Store({ schema });
+
+    query(store, { query: parse(getIntrospectionQuery()) }, schema);
+    expect(console.warn).toBeCalledTimes(0);
+  });
 });