2
votes

I want to add the apollo framework to my iOS project. I'm following the documentation but can't build it.

  1. I install apollo via cocoapods
  2. I add a code generation build step to my target (put it above compile sources...)
  3. I download the schema via apollo schema:download --endpoint=http://localhost:4000/graphql schema.json. I add schema.json file to the root directory in my project.
  4. I build it and get the following error:

    • [email protected] added 416 packages in 20.385s ++ exec /Users/PATH/node_modules/.bin/apollo codegen:generate --queries= --schema=schema.json API.swift › Warning: apollo update available from 1.2.0 to 1.7.0 › Error: Flag --queries expects a value
      Command /bin/sh failed with exit code 2

My schema.grapqhl file looks something like:

type Query {
  """ User """
  users: [User!]!
  user(username: String!): User

  """ Question """
  questions: [Question!]!
  question(id: Int!): Question

  }

  type Mutation {
  """ User """
    createUser(username: String!): User
    updateUser(username: String!, newUsername: String!): [Int!]!
    deleteUser(username: String!): Int!

  """ Question """
    createQuestion(text: String!, category: Int!, active: Boolean!): 
    Question
    updateQuestion(id: Int!, text: String!, category: Int!, active: Boolean!): [Int!]!
    deleteQuestion(id: Int!): Int!
 }

 type Subscription {
   answerAdded: Answer
 }

 type Question {
  id: Int!
  text: String!
  categoryId: QuestionCategory!
  active: Boolean!
  createdAt: String!
  updatedAt: String!
  }
  ......

It seems that the query param doesnt have an input: apollo codegen:generate --queries= --schema=schema.json API.swift

If i try it myself via terminal

apollo codegen:generate --queries=schema.graphql --schema=schema.json API.swift

I get the following error:

Warning: apollo update available from 1.2.0 to 1.7.0
.../schema.graphql: The Query definition is not executable.
.../schema.graphql: The Mutation definition is not executable.
.../schema.graphql: The Subscription definition is not executable.
.../schema.graphql: The Question definition is not executable.
... some more ...
:heavy_check_mark: Scanning for GraphQL queries (1 found)
:heavy_check_mark: Loading GraphQL schema
:heavy_check_mark: Parsing GraphQL schema
:heavy_multiplication_x: Generating query files with 'swift' target
 → Validation of GraphQL query document failed
ToolError: Validation of GraphQL query document failed
at Object.validateQueryDocument 
....

Can you please help to find out why I can't build the project? Is it because my schema.graphl file is wrong?

2

2 Answers

2
votes

as answered by @Sergej Birklin I would like to share another query with you-

Profile.graphql

query MyProfile{
    player {
        me {
            id
            secret
            name
            email
            state
            country
            timezone
            picture
            pictureType
            audio
            rank
        }
    }
}

As simply we're queering the my profile info (where the me is as a player type), the query will return with the information I required like my ID, name, email, state etc.

as you state that it will create MyProfileQuery

Hope this will help you out 👍👍

#swith #GraphQL #iOS

1
votes

Thanks to Martijn Walraven on the open Apollo Slack iOS Channel I solved it.

The problem is that my schema.graphql file is wrong.

your schema.graphql file is a representation of your schema in the schema definition language, not a query document. apollo ios expects you to define the queries your app uses in .graphql files, so it can generate code from them. these are the queries the client sends to the server, similar to what you would put in graphiql for example

... queries need to be named. so if you put this in a .graphql file:

query AllQuestions {
  queries {
    text
  }
}

that will generate a AllQuestionsQuery swift class that you can pass to client.fetch(query:) and it will also generate result types with the exact fields you requested (edited) so you can access text, but not active for example so in contrast to global model types, query-specific types give you complete type safety for your data fetching