Giter VIP home page Giter VIP logo

Comments (5)

AnthonyMDev avatar AnthonyMDev commented on May 25, 2024 1

Thanks for the feedback @BrentMifsud. We've had a lot of similar feedback from people on this feature, so you are not alone. We do intend to deprecate LocalCacheMutation and allow you to make any selection set mutable in a future update.

In the mean time, the easiest way to work with this is to put the @apollo_client_ios_localCacheMutation directive on the shared AnnouncementFragment itself. You can use this fragment in a query, but it will also be usable for local cache mutations.

from apollo-ios.

github-actions avatar github-actions commented on May 25, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

from apollo-ios.

BrentMifsud avatar BrentMifsud commented on May 25, 2024

Thanks for the feedback @BrentMifsud. We've had a lot of similar feedback from people on this feature, so you are not alone. We do intend to deprecate LocalCacheMutation and allow you to make any selection set mutable in a future update.

In the mean time, the easiest way to work with this is to put the @apollo_client_ios_localCacheMutation directive on the shared AnnouncementFragment itself. You can use this fragment in a query, but it will also be usable for local cache mutations.

@AnthonyMDev Thanks for the response.

Do you have any examples of this in practice? I have added the local cache mutation directive on my fragment, but there does not seem to be any option to insert a fragment.

// from graphqlclient
func updateCachedQuery<CacheMutation: LocalCacheMutation>(
        cacheMutation: CacheMutation,
        mutator: @escaping (inout CacheMutation.Data) -> Void
    ) {
        store.withinReadWriteTransaction { transaction in
            try transaction.update(cacheMutation) { selectionSet in
                mutator(&selectionSet)
            }
        }
    }

image

Here is my codegen config (in case im missing something):

{
  "schemaName" : "Redacted",
  "input" : {
    "operationSearchPaths" : [
      "Redacted/Modules/GraphQL/Fragments/*.graphql",
      "Redacted/Modules/GraphQL/Mutations/*.graphql",
      "Redacted/Modules/GraphQL/Queries/*.graphql"
    ],
    "schemaSearchPaths" : [
      "Redacted/Modules/GraphQL/schema.graphqls"
    ]
  },
  "output" : {
    "testMocks" : {
      "none" : {}
    },
    "schemaTypes": {
      "moduleType": {
          "embeddedInTarget": {
            "name": "Redacted"
          }
      },
      "path": "Redacted/Modules/GraphQL/Generated/"
    },
    "operations" : {
      "inSchemaModule": {}
    }
  },
  "options": {
      "selectionSetInitializers" : {
        "operations": true,
        "namedFragments": true,
        "localCacheMutations" : true
      },
      "deprecatedEnumCases": "include",
      "schemaDocumentation": "include",
      "warningsOnDeprecatedUsage": "include",
      "conversionStrategies": {
          "enumCases": "camelCase"
      },
      "pruneGeneratedFiles": true
   }
}

from apollo-ios.

AnthonyMDev avatar AnthonyMDev commented on May 25, 2024

Ah, I see. This is still a bit difficult to do. Which is why we want to make the changes I mentioned above.

Check out the docs here. You'll want to use the updateObject(ofType:withKey:) function. Since you are actually trying to append the new announcement to a list of announcements, you'll want the announcements` field in your mutable fragment.

fragment AnnouncementsListFragment @apollo_client_ios_localCacheMutation on Query {
    announcements(locationId: $locationID) {
        ...AnnouncementFragment
    }
}

When mutating the cache, you'll need to know the cache ID of the entity that has the announcements field on it. But in your case, it's just on the root query, which has the cache key QUERY_ROOT. You'll also need to pass in the correct locationID variable.

Try this:

var myNewAnnouncement: AnnouncementFragment

store.withinReadWriteTransaction { transaction in 
  transaction.updateObject(
    ofType: AnnouncementsListFragment.self,
    withKey: "QUERY_ROOT",
    variables: ["locationID": "myLocationID"]
  ) { (announcements inout AnnouncementsListFragment)
    announcements.append(myNewAnnouncement)
  }
}

There is definitely a lot to be desired with the current APIs for this. Hopefully this will get you where you need to be for now, but we definitely want to improve on this.

from apollo-ios.

BrentMifsud avatar BrentMifsud commented on May 25, 2024

Ahhh I see. I never thought of making an array into a fragment. Will try that.

from apollo-ios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.