apollo-link-state icon indicating copy to clipboard operation
apollo-link-state copied to clipboard

Plain object type ?

Open strblr opened this issue 6 years ago • 14 comments

Hi,

I'm wondering if there is anything like an Object type for plain objects, just like you have Int, String, Boolean ?

I'm storing a "session" field in my local apollo state, containing a loggedIn boolean and a currentUser object :

function mutate(cache, payload) {
  cache.writeData({ data: payload })
}

const localState = {
  resolvers: {
    Mutation: {
      updateSession(_, { loggedIn, currentUser }, { cache }) {
        mutate(cache, {
          session: {
            __typename: 'Session',
            loggedIn,
            currentUser
          }
        })
        return null
      }
    }
  },
  defaults: {
    session: {
      __typename: 'Session',
      loggedIn: false,
      currentUser: null
    }
  }
}

// Then :

withClientState({
  cache,
  ...localState
})

I wanna mutate this state just after login :

const login = gql`
  mutation($email: String!, $password: String!) {
    login(email: $email, password: $password) {
      _id
      email
      name
      token
    }
  }
`

const updateSession = gql`
  mutation($loggedIn: Boolean!, $currentUser: CurrentUser!) {
    updateSession(loggedIn: $loggedIn, currentUser: $currentUser) @client
  }
`

// ... then when submitting login form :

       this.props.login({
          variables: {
            email,
            password
          }
        }).then(({ data: { login } }) => {
          localStorage.setItem('liveql-token', login.token)
          this.props.updateSession({
            variables: {
              loggedIn: true,
              currentUser: {
                __typename: 'CurrentUser',
                ...pick(login, ['_id', 'email', 'name'])
              }
            }
          })
        })

Look at the mutation type def for $currentUser : what type should I write ? I found out that it works with any type, but I'm afraid that this behaviour will break in future release since there's nothing in the doc about this (or, is there ?)

Thanks a lot for your insight. Olivier

strblr avatar Feb 14 '18 00:02 strblr

+1

hackdie avatar Feb 14 '18 15:02 hackdie

Seems like there is one pull request adding Customs obj types. https://github.com/apollographql/apollo-link-state/pull/197

hackdie avatar Feb 15 '18 09:02 hackdie

@ostrebler I was able to pass object as variable to mutation in this example https://codesandbox.io/s/ll7xvxq0xq

const setUserMutation = gql`
  mutation setUser($user: User) {
    setUser(user: $user) @client
  }
`;

In this mutation, user is an object and it can have any number of properties and properties can be of any type. User type is not defined anywhere. so type checking for user will not be done.

akiran avatar Feb 16 '18 18:02 akiran

@client doesn't do any type validation, the contract is that you hand it a User, but it doesn't need to understand what a User is in order to pass it along. There is no runtime type validation being done by Apollo here If you gave it something incompatible, the crash will occur inside your setUser resolver.

fbartho avatar Feb 16 '18 19:02 fbartho

Anyway is there any serious plan in near future to store in @client any arbitrary data type? Like React component, any JSON data structure, function, etc...? Without this there is no way get rid of Redux which is capable to store really anything...

My intention is to have only one state manager, I like redux, but if we have Apollo I prefer make everything in Apollo ;)!

zanj2006 avatar Feb 28 '18 14:02 zanj2006

+1 on an arbitrary data type, for example a JSON dump of translations, which we don’t need to stringify and parse each time

smartmike avatar Jun 07 '18 14:06 smartmike

+1

JustinPlute avatar Aug 07 '18 01:08 JustinPlute

+1

apski avatar Oct 07 '18 18:10 apski

+10000

mbaranovski avatar Oct 08 '18 06:10 mbaranovski

+20181031

cncolder avatar Oct 31 '18 07:10 cncolder

@hwillson Hello!

Anyway is there any serious plan in near future to store in @client any arbitrary data type? Like React component, any JSON data structure, function, etc...? Without this there is no way get rid of Redux which is capable to store really anything... My intention is to have only one state manager, I like redux, but if we have Apollo I prefer make everything in Apollo ;)!

Do you have a plan to integrate plain objects in the local storage?

atanych avatar Jan 09 '19 14:01 atanych

This is something that would be incredibly useful. I'm implementing redux which seems like a lot of overhead for this one feature.

martinseanhunt avatar Feb 22 '19 11:02 martinseanhunt

@hwillson has this been added to AC 2.5? thanks

stefanoTron avatar Feb 26 '19 20:02 stefanoTron

I ended up storing the object as a JSON string. Seems a bit unnecessary and I wonder how sustainable that would be with large amounts of data but it's working fine for my current use case.

martinseanhunt avatar Feb 26 '19 20:02 martinseanhunt