Apollo Client query ​Missing field __typename












0















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



























    0















    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

























      0












      0








      0








      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














      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!







      javascript graphql apollo-client apollo-link-rest






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 18:28









      joelnetjoelnet

      8,46442744




      8,46442744
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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
          },
          },
          })





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325787%2fapollo-client-query-missing-field-typename%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            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
            },
            },
            })





            share|improve this answer




























              1














              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
              },
              },
              })





              share|improve this answer


























                1












                1








                1







                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
                },
                },
                })





                share|improve this answer













                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
                },
                },
                })






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 19:31









                Daniel ReardenDaniel Rearden

                15.9k11742




                15.9k11742
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325787%2fapollo-client-query-missing-field-typename%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly