vuex-orm / plugin-graphql

Vuex ORM persistence plugin to sync the store against a GraphQL API.
https://vuex-orm.github.io/plugin-graphql/
MIT License
226 stars 52 forks source link

persist/push: Avoid sending object properties not declared in input types #120

Closed johnrix closed 4 years ago

johnrix commented 5 years ago

Currently when pushing an object back to my GraphQL API, I can control the name of the input type for the update request handily using the adapter method getInputTypeName.

However, despite returning the name of an input type that is defined in my schema, the plugin encodes all values of my object into the push request rather than just those defined in the input type.

Using the example from the documentation, if my updatePost mutation only allowed changes to the content and title, but not the user, my schema might look something like this:

type Post {
    id: ID!
    userId: ID!
    title: String
    content: String
    ....
}

input PostInput {
    title: String
    content: String
}

type Mutation {
    updatePost(id: Int!, post: PostInput!): Post
}

However, if I tried to $push() an existing post, I will get an error from the GraphQL server, since all of the Post properties are getting encoded in the Input object, despite the input type only having title and content fields.

cwirz commented 5 years ago

@johnrix how bout when you are able to mark fields as read only in the model structure? currently having the same issue and i dont want to define the fields in the backend mutation without usage.

johnrix commented 5 years ago

@cwirz, is there a way to flag a field as read-only within Vuex-ORM or using the graphql plugin? That would likely be one way to get around the problem if so. I can't find it right now if it exists though.

Currently, my only option is to use a model-related custom mutation.

cwirz commented 5 years ago

@johnrix ok ill try to implement that when i have time

johnrix commented 5 years ago

Sorry, I thought you were suggesting there was some way to do that already.

As I said, it would be one way around the issue. It seems sub-optimal to me though. Is it not possible simply to only include fields that are defined in the mutation Input type? This would then require no specific effort to flag read-only fields within the client-side model definitions.

cwirz commented 5 years ago

@johnrix yeah agree since the system anyway requests the schema from the server. but for me the virtual field functionality is not even working: https://vuex-orm.github.io/plugin-graphql/guide/virtual-fields.html and i can find the code where the check if a field is present in the schema. does it work for you?

johnrix commented 5 years ago

To be honest, I haven't tried the virtual field functionality, since I want the data to be fetched, but just not sent back in mutations. I'd be happy to have a go at this update myself also, if somebody can point me to the right location!

ghost commented 4 years ago

Don't mean to bump this old thread, was this ever looked into @phortx or is there a way in the current configuration that allows for selecting which model fields are included in a mutation?

phortx commented 4 years ago

That's a good point. I don't think this is possible currently. Maybe I can take a look in the future Feel free to create a Pull Request :)

phortx commented 4 years ago

Good News:

This is an issue in my current project too. The best reason to tackle that ;)

I'll provide a patch this week.

phortx commented 4 years ago

This should be fixed in version 1.0.0-rc.39.