0
votes

I was trying to learn and comprehend GraphQl.

In order to do so, I went to apollo-graphQL blog and started with getting started launch

From their blogs, in our schema.js file, consider we have something like
onst { gql } = require('apollo-server');
const typeDefs = gql`

type Query {
  launches: [Launch]!
  launch(id: ID!): Launch
  me: User
}

type Launch {
  id: ID!
  site: String
  mission: Mission
  rocket: Rocket
  isBooked: Boolean!
}
module.exports = typeDefs;

Now in tool from where we can query (like graphiqL), there in their example they have done something like this in query

{
  launch(id: 1) {
    site
  }
}

I am unsure- here about the place our site in the above graphiqL object is coming and how can we write it (since in our query, launch is expecting a return type if Launch and only want id launch(id: ID!): Launch)

Why is this query invalid

{
  launch(id: 1) 
}
1

1 Answers

1
votes

You need to specify fields for complex types. For your example ("and only want id").

{
  launch(id: 1) {
    id
  }
}

What goes in (id: 1) is an input for the query (like an argument for a function). But you still have to specify what you want back.

UPD. Just to be clear the same rule applies to nested complex types. For example, if you want to get launch rocket as well you can't simply do

{
  launch(id: 1) {
    id
    rocket # does not work
  }
}

You need to specify which rocket fields you want

{
  launch(id: 1) {
    id
    rocket {
      id
    }
  }
}