Missing selection set for object GraphQL+Apollo error

前端 未结 3 2154
無奈伤痛
無奈伤痛 2021-02-14 14:17

I have a set of mutations that trigger the local state of certain types of popups. They\'re generally set up like this:

  openDialog: (_, variables, { cache }) =         


        
相关标签:
3条回答
  • 2021-02-14 14:41

    Answer to the actual problem seems lay in the query. Initially Apollo client was not validating types for @client queries/mutations so your mutation could look like you wrote it in the question:

    mutation AlertOpenDialog($type: String!) {
      openDialog(type: $type) @client
    }
    

    the correct way of writing above is to specify (select) all the simple type (scalar) fields you wanna get in a response. So in regard to the @Vic answer the mutation should look more like:

    mutation AlertOpenDialog($type: String!) {
      openDialog(type: $type) @client {
        dialog {
          id
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-14 14:43

    Solution is to add fields to the query (vs. declaring the top-level object you want to fetch without specifying the fields to fetch). If you have something like:

    {
      popups @client {
        id
        dialog
      }
    }
    

    you must declare some fields to fetch inside dialog, for example id:

    {
      popups @client {
        id
        dialog {
          id
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-14 14:49

    The fix for this turned out to work by treating the dialog field as a string instead of an object. Changing the function to this did the trick and made the errors go away:

      openDialog: (_, variables, { cache }) => {
        const data = {
          popups: {
            ...popups,
            dialog: variables.type
          }
        };
    
        cache.writeData({
          data: data
        });
        return null;
      }
    
    0 讨论(0)
提交回复
热议问题