Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Setup iOS SPM #49

Closed
oliverfoggin opened this issue Oct 31, 2022 · 11 comments
Closed

Initial Setup iOS SPM #49

oliverfoggin opened this issue Oct 31, 2022 · 11 comments

Comments

@oliverfoggin
Copy link

oliverfoggin commented Oct 31, 2022

I've been trying to work with the Getting Started documentation here... https://www.apollographql.com/docs/ios/get-started

But I'm struggling to work out what I need to do. I don't have any experience with setting up any GraphQL API and I feel like the docs assume quite a bit of knowledge. Either that or there are possibly missing a few steps?

I don't yet have a schema for the project we're working on. I'm just involved in the initial exploration of the Apollo SDK in the project and so I decided to use the StarWars schema mentioned here... https://www.apollographql.com/docs/ios/code-generation/downloading-schema

It mentions using the drop down menu on the right of the Apollo Studio screen to download the JSON file rather than the raw file.

But then it drops any mention of using the Star Wars schema. I have a [email protected] file in my project but I'm not sure what to do with it.

It says to run this line... swift package --allow-writing-to-package-directory apollo-initialize-codegen-config --schema-name ${MySchemaName}... Should I be using the downloaded star wars file to find the schema name? Is star-wars-swapi the schema name? Or is the schema name contained inside that file?

I also have an apollo-codegen-config file. But I'm also not sure what to do with that.

I've added the apollo-ios-cli to the package and I can right click and see the initialise, fetch, generate options but they don't seem to do anything.

I'm trying to follow the instructions for using a Package.swift file with SPM rather than doing it through Xcode.

I have a package and target in the SPM package called GraphQL located at ./Sources/GraphQL.

I'm stuck right now with trying to get hold of a .graphql or .graphqls file. I think I'm supposed to have some of those in the project but I can't find where I am supposed to get them from.

Thanks

@oliverfoggin
Copy link
Author

Hmm... I've found the logs in Xcode that show I'm still hitting on the error with the test schema files and types causing issues.

I saw this comment... apollographql/apollo-ios#2543 (comment)

But I don't have a graphql or graphqls file to move.

@oliverfoggin
Copy link
Author

OK, I got past that I think. All our code files live inside ./Sources/ so I just added that to the search paths. I think the errors have gone now. But I'm not sure anything is being generated. Thanks

@oliverfoggin
Copy link
Author

oliverfoggin commented Oct 31, 2022

Ah ok, I'm getting closer. From this comment... apollographql/apollo-ios#2543 (comment)

It looks like the docs tell you to download the schema in JSON but the generated file starts looking for .graphqls (which doesn't exist). But nothing in the docs mentions this.

Now it's telling me "The schema definition is not executable". I could chmod it but it's a JOSN file and I'm not sure that's what it means? 🤔

@calvincestari
Copy link
Member

Hey @oliverfoggin, there's a couple things here I can help with

But I'm struggling to work out what I need to do. I don't have any experience with setting up any GraphQL API and I feel like the docs assume quite a bit of knowledge. Either that or there are possibly missing a few steps?

Our documentation can certainly improve and better illustrate all things needed in order to begin. Thanks for the feedback.

It mentions using the drop down menu on the right of the Apollo Studio screen to download the JSON file rather than the raw file.

It's not incorrect but I think it should be changed to guide users to download the SDL format file, it's far more human-readable and works well with the config file defaults.

But then it drops any mention of using the Star Wars schema. I have a [email protected] file in my project but I'm not sure what to do with it.

Think of it this way schema + operation = code. The code generation engine will product Swift-based models in the shape of your operations, and in order to product type-safe code it needs to fully understand from the schema what types are available and being used.

It says to run this line... swift package --allow-writing-to-package-directory apollo-initialize-codegen-config --schema-name ${MySchemaName}... Should I be using the downloaded star wars file to find the schema name? Is star-wars-swapi the schema name? Or is the schema name contained inside that file?

schema name is simply what you want to refer to your schema as. For example if I were using the GitHub GraphQL API I would likely name it GitHubSchema or GitHubGraphQL. It is not the name of your schema file.

I also have an apollo-codegen-config file. But I'm also not sure what to do with that.

Code generation can either be configured and executed through code in a standalone macOS project, or using the CLI which requires the use of the JSON configuration file.

I'm stuck right now with trying to get hold of a .graphql or .graphqls file. I think I'm supposed to have some of those in the project but I can't find where I am supposed to get them from.

Apollo Studio supports downloading in either JSON or SDL format. SDL files typically have the .graphqls extension.

Now it's telling me "The schema definition is not executable". I could chmod it but it's a JOSN file and I'm not sure that's what it means? 🤔

Sounds like you may not have any operations defined? A schema defines what is possible via the API, but your operations define what it is you actually want.

@calvincestari
Copy link
Member

We can continue the conversation here but I'm going to close this as it's not a bug or feature request, etc.

@oliverfoggin
Copy link
Author

Thanks for the replies to this. I think a lot of this will definitely help. I'll be working on this again today so hopefully I'll be able to get moving on it 😄

Thanks

@oliverfoggin
Copy link
Author

I got a generated file! Thanks for your help!

The key things were switching to the SDL schema, and creating a query (but then also pointing the config file to the correct places).

Thanks @calvincestari

@oliverfoggin
Copy link
Author

I feel like I'm very close to getting this working now. However, after getting the types and stuff generated I'm getting a compile error in the Apollo package itself...

Cannot find type 'ApolloCodegen' in scope.

This is in files like FragmentFileGenerator.swift so I feel like I need another import or something. Thanks

@calvincestari
Copy link
Member

Looks like you got this working, from what I can read in your community post.

The key things were switching to the SDL schema, and creating a query (but then also pointing the config file to the correct places).

Code generation does support JSON-based schema files, you will need to update the schemaSearchPaths pattern in the configuration file to look for .json extension files though, i.e.: "**/*/schema.json". There are far more files that use the .json extension though so you'll need to make sure that you scope the search path narrow enough so that it doesn't find any unintended .json extension files.

@oliverfoggin
Copy link
Author

@calvincestari thanks, I'll make sure to keep that in mind for the future.

One thing I have noticed when using the SDL file is that it seems to be references in the docs as .graphqls. But when downloading it it is actually just .graphql.

I've got it working with this but I had to update the config to use the correct path and file extension.

Thanks

@calvincestari
Copy link
Member

One thing I have noticed when using the SDL file is that it seems to be references in the docs as .graphqls. But when downloading it it is actually just .graphql.

That is a quirk of Apollo Studio. We should add a note about that to the Apollo iOS documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants