Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Plain object type ? #205

Open
strblr opened this issue Feb 14, 2018 · 14 comments
Open

Plain object type ? #205

strblr opened this issue Feb 14, 2018 · 14 comments
Assignees
Labels
ac-local-state-todo feature New addition or enhancement to existing solutions question

Comments

@strblr
Copy link

strblr commented Feb 14, 2018

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

@hackdie
Copy link

hackdie commented Feb 14, 2018

+1

@hackdie
Copy link

hackdie commented Feb 15, 2018

Seems like there is one pull request adding Customs obj types.
#197

@akiran
Copy link
Contributor

akiran commented Feb 16, 2018

@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.

@fbartho
Copy link
Contributor

fbartho commented Feb 16, 2018

@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.

@zanj2006
Copy link

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 ;)!

@fbartho fbartho added question feature New addition or enhancement to existing solutions labels Mar 27, 2018
@smartmike
Copy link

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

@JustinPlute
Copy link

+1

1 similar comment
@apski
Copy link

apski commented Oct 7, 2018

+1

@mbaranovski
Copy link

+10000

@cncolder
Copy link

+20181031

@atanych
Copy link

atanych commented Jan 9, 2019

@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?

@martinseanhunt
Copy link

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

@stefanoTron
Copy link

@hwillson has this been added to AC 2.5? thanks

@martinseanhunt
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ac-local-state-todo feature New addition or enhancement to existing solutions question
Projects
None yet
Development

No branches or pull requests