apollographql / apollo-ios

📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.
https://www.apollographql.com/docs/ios/
MIT License
3.88k stars 726 forks source link

iOS/Xcode 14.2, Apollo Upgrade from 0.53.0 to 1.0.7 #3002

Closed ArunVicky001 closed 1 year ago

ArunVicky001 commented 1 year ago

Question

Hi I have an existing project using an old Apollo build(0.53.0) w/cocoapods and am trying to upgrade to 1.0.7. I followed all the steps here:

https://www.apollographql.com/docs/ios/migrations/1.0/#breaking-changes

using the Xcode project' set up that is outlined in below steps

  1. Install the Apollo iOS pod: pod 'Apollo', '~> 1.0' 2. Created a GraphQL schema file (usually a .json file) that describes your API. 3.In your Xcode project, create a new build phase to generate code. a. Open your project settings and go to the "Build Phases" tab. b. Click the plus button in the top-left corner and select "New Run Script Phase". c. In the script text field, enter the following: ./Pods/Apollo/apollo-ios-cli generate path/to/schema.graphql --output API --target ios the above command is not working and it throwing me an error like below
Error: Unknown option '--output'
Usage: apollo-ios-cli generate [--path <path>] [--string <string>] [--verbose] [--fetch-schema] [--ignore-version-mismatch]
  See 'apollo-ios-cli generate --help' for more information.
Command PhaseScriptExecution failed with a nonzero exit code

also I tired with ./Pods/Apollo/apollo-ios-cli generate path/to/schema.graphql --outputDirectory API --target ios getting same error below

Error: Unknown option '--outputDirectory'
Usage: apollo-ios-cli generate [--path <path>] [--string <string>] [--verbose] [--fetch-schema] [--ignore-version-mismatch]
  See 'apollo-ios-cli generate --help' for more information.
Command PhaseScriptExecution failed with a nonzero exit code

please suggest me a right way migrate the library from 0.53.0 to 1.0.7

Thank you

BobaFetters commented 1 year ago

Hey @ArunVicky001, so the Run Script build phase setup for codegen is not updated or recommended any longer starting with the 1.0 release. If you check out this section of the migration guide you will see some info on that.

As for getting your code generation working, you will want to check out the 1.0 documentation for code generation for the proper commands to run for code generation. The basics are that you will need to first create a codegen configuration that is used by the generate command. I recommend reading through the codegen documentation to see if you can get things working, and if you have further questions let me know.

ArunVicky001 commented 1 year ago

@BobaFetters Thanks for your response, I have successfully executed the command and it has created the model classes into my project based on the query. I followed the below steps to add the created folders in my project

  1. In Xcode, right-click on your project in the project navigator and select "Add Files to [Your Project]".
  2. Navigate to the folder where your MySchema file is located and select it.
  3. Make sure to check the "Copy items if needed" checkbox and select "Create folder references" as the added folder's "Added folders" option.
  4. Repeat the same process for your queries folder.
  5. Once you have added both folders, make sure to select your project in the project navigator and go to the "Build Phases" tab.
  6. In the "Build Phases" tab, expand the "Compile Sources" section and make sure that all of the generated .swift files are listed here. If any files are missing, click the "+" button and add them manually.
  7. Finally, make sure to import the necessary classes in any view controllers or other files where you want to use them.

but still I can't able to read the files is that something else should I need to do?

ArunVicky001 commented 1 year ago

this is my folder structure

Screenshot 2023-05-11 at 3 07 02 PM

I am trying to access GetInboxUnreadCountQuery class in my viewController

Screenshot 2023-05-11 at 3 07 16 PM

like below in my view controller

Screenshot 2023-05-11 at 3 12 44 PM

it's not showing could please suggest me the right way to solve this issue?

BobaFetters commented 1 year ago

@ArunVicky001 Can you share your apollo-codegen-config.json so I can see how to codegen is configured? Also can you point me to where in the documentation you found the steps you listed for adding the files to the project? In general what you are showing in the screenshots looks as if you would need to access your query type through it's namespace like MyQuery.GetInboxUnreadCount...

ArunVicky001 commented 1 year ago

@BobaFetters apollo-codegen-config.json source code

{
  "schemaName" : "MyQuery",
  "options" : {
    "cocoapodsCompatibleImportStatements" : true
  },
  "input" : {
    "operationSearchPaths" : [
      "**/*.graphql"
    ],
    "schemaSearchPaths" : [
      "/Users/vignesh.arunagiri/Documents/XCode/ClientiOS/Schema/schema.json"
    ]
  },
  "output" : {
    "testMocks" : {
      "none" : {
      }
    },
    "schemaTypes" : {
      "path" : "./MyQuery",
      "moduleType" : {
        "embeddedInTarget" : {
          "name" : "ClientiOS"
        }
      }
    },
    "operations" : {
      "inSchemaModule" : {
      }
    }
  }
}

I don't find any documentation to add files in the project in the apollo client iOS. I have searched it and followed the procedures.

I tried this one too but it's also not accessible MyQuery.GetInboxUnreadCount...

![Uploading Screenshot 2023-05-11 at 10.41.14 PM.png…]()

calvincestari commented 1 year ago

@ArunVicky001, in the code you shared it shows that GetInboxUnreadCountQuery is within an extension on MyQuery; we do this for embedded module types to minimize the chance of a type conflict. You will have to access it through the MyQuery namespace, so you should change your view controller code to [MyQuery.GetInboxUnreadCountQuery.Data.getInboxUnreadCount].

ArunVicky001 commented 1 year ago

@calvincestari @BobaFetters

if you see the screenshot MyQuery folder is not properly adding into the project it's shows "?" beside the folder name

Screenshot 2023-05-11 at 10 45 34 PM
calvincestari commented 1 year ago

if you see the screenshot MyQuery folder is not properly adding into the project it's shows "?" beside the folder name

I believe that is the git status of the files in your repo. It has nothing to do with Apollo iOS.

ArunVicky001 commented 1 year ago

then it's fine, Could you please help me to get the MYQuery class accessible in the ViewController? also please check the sample project and suggest me

calvincestari commented 1 year ago

The files were not correctly added to the project. If you look at 'Compile Sources' in the 'Build Phases' tab of the project, you'll see that none of the .swift files were added to the target. They're not being compiled which means your project doesn't know about them.

Screenshot

Remove the MyQuery folder from your project and add it again, then it should look like this:

Screenshot

Then you can use the MyQuery namespace and GetInboxUnreadCountQuery within it.

ArunVicky001 commented 1 year ago

How did you added the MyQuery folder in the project could you please share the steps? because there I am committing the mistake

ArunVicky001 commented 1 year ago

Got it, it should be Create Groups instead of Create folder references and then manually we need to add those files into the compile sources.

Screenshot 2023-05-11 at 11 20 14 PM
calvincestari commented 1 year ago

and then manually we need to add those files into the compile sources.

No, you do not need to manually add them. Xcode will automatically do it.

calvincestari commented 1 year ago

I'm going to close this issue as there is nothing actionable for us in Apollo iOS. Your project should work fine once the generated files are correctly added and compiled into the target.

calvincestari commented 1 year ago

@calvincestari is that the right way to do this, and it's not mentioned anywhere in the documentation. could you please confirm this.

@ArunVicky001, you need to add the generated files to your project in a way that is suitable for your project and so that they're compiled into the target. We don't include step by step instructions for Xcode because we require that users already have the knowledge to do that.

after added the folder into the project I just ran the application and the files are not being added in the compile resources. please verify it again.

It sounds like you didn't select the ClientiOS target for 'Add to targets:' when adding the files to your project. Adding it to the target is what gets it included in the build step.

Screenshot 2023-05-11 at 11 00 44 AM