javascript - Apollo Client query ​Missing field __typename - Stack Overflow

I am trying to use apollo-link-rest with the Star Wars API and I am getting some errors.import { InMemo

I am trying to use apollo-link-rest with the Star Wars API and I am getting some errors.

import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloClient } from "apollo-client";
import { RestLink } from "apollo-link-rest";
import gql from "graphql-tag";

// node environment?
const fetch = require("node-fetch");
global.fetch = fetch;
global.Headers = fetch.Headers;

const restLink = new RestLink({
  endpoints: { swapi: "/" }
});

const client = new ApolloClient({
  link: restLink,
  cache: new InMemoryCache()
});

const query = gql`
  query people {
    search
      @rest(type: "Search", path: "people/?search=skywalker", endpoint: swapi) {
      count
      results {
        name
      }
    }
  }
`;

client
  .query({ query })
  .then(response => console.log(JSON.stringify(response)))
  .catch(err => console.log(err));

Errors:

​​​Missing field __typename in {​​​
​​​  "name": "Luke Skywalker"​​​
​​​}​​​
​​​​​​
​​​Missing field __typename in {​​​
​​​  "name": "Anakin Skywalker"​​​
​​​}​​​
​​​​​​
​​​Missing field __typename in {​​​
​​​  "name": "Shmi Skywalker"​​​
​​​}​​​

I know I can change set this InMemoryCache({ addTypename: false }) to remove the errors, but I don't know what the impact on caching will be if I set addTypename to false.

Can someone point me in the right direction on this?

Cheers!

I am trying to use apollo-link-rest with the Star Wars API and I am getting some errors.

import { InMemoryCache } from "apollo-cache-inmemory";
import { ApolloClient } from "apollo-client";
import { RestLink } from "apollo-link-rest";
import gql from "graphql-tag";

// node environment?
const fetch = require("node-fetch");
global.fetch = fetch;
global.Headers = fetch.Headers;

const restLink = new RestLink({
  endpoints: { swapi: "https://swapi.co/api/" }
});

const client = new ApolloClient({
  link: restLink,
  cache: new InMemoryCache()
});

const query = gql`
  query people {
    search
      @rest(type: "Search", path: "people/?search=skywalker", endpoint: swapi) {
      count
      results {
        name
      }
    }
  }
`;

client
  .query({ query })
  .then(response => console.log(JSON.stringify(response)))
  .catch(err => console.log(err));

Errors:

​​​Missing field __typename in {​​​
​​​  "name": "Luke Skywalker"​​​
​​​}​​​
​​​​​​
​​​Missing field __typename in {​​​
​​​  "name": "Anakin Skywalker"​​​
​​​}​​​
​​​​​​
​​​Missing field __typename in {​​​
​​​  "name": "Shmi Skywalker"​​​
​​​}​​​

I know I can change set this InMemoryCache({ addTypename: false }) to remove the errors, but I don't know what the impact on caching will be if I set addTypename to false.

Can someone point me in the right direction on this?

Cheers!

Share Improve this question asked Nov 15, 2018 at 18:28 joelnetjoelnet 14.3k6 gold badges38 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Check out what the docs have to say about typename patching.

Your @rest directive tells the client what typename to expect for the search field, but doesn't say anything about any types inside the field's selection set. There's two ways to fix that. You can use a @type directive:

query people {
  search @rest(type: "Search", path: "people/?search=skywalker", endpoint: swapi) {
    count
    results @type(name: "Person") {
      name
    }
  }
}

Or configure a typePatcher. Something like:

const restLink = new RestLink({
  uri: 'https://swapi.co/api/',
  typePatcher: {
    Search: (data, outerType, patchDeeper) => {
      if (data.results != null) {
        data.results = data.results.map(person => {
          return {__typename: "Person", ...person }
        });
      }
      return data
    },
  },
})

More generic answer:

to avoid the missing __typename error ensure any arrays in your GQL model has a corresponding field __typename for EACH object in the array.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744190901a4562428.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信