Basics Flashcards
What is a schema definition?
A schema is a collection ofobject typesthat containfields. Each field has a type of its own. A field’s type can bescalar(such as anIntor aString), or it can beanother object type.
How declare a type?
We declare a type using the type keyword, followed by the name of the type (PascalCase is best practice), then opening brackets to hold its contained fields:
type SpaceCat {
# Fields go here
}
How define a field?
Fields are declared by their name (camelCase), a colon, and then the type of the field (scalar or object). A field can also contain a list, indicated by square brackets:
type SpaceCat {
age: Int
missions: [Mission]
}
Unlike Javascript objects (which look very similar), fields are not separated by commas. In addition, we can indicate whether each field value is nullable or non-nullable. If a field should never be null, we add an exclamation mark after its type.
Which of the following are valid field types?
Int
String
String!
[Int]
All of them
Define a SpaceCat type with the following fields: name of type String (non null), age of type Int, and missions of type List of Mission
type SpaceCat {
name: String!
age: Int
missions: [Mission]
}
How to add schema descriptions?
To do that, the SDL lets you add descriptions to both types and fields by writing strings (in quotation marks) directly above them.
“I’m a regular description”
Triple “double quotes” allow you to add line breaks for clearer formatting of lengthier comments.
”””
I’m a block description
with a line break
”””
How define an endpoint to get data?
Creating a type Query:
type Query {
# Fields go here
}
The fields of this type are entry points into the rest of our schema. These are the top-level fields that our client can query for.
[Code Challange] Create a full schema with: a type Query containing a field spaceCats to fetch a List of SpaceCat. A type SpaceCat with its subfields: id of type ID!, name of type String!, age of type Int and missions of type List of Mission. Finally define the Mission type with its subfields: id of type ID!, name of type String!, and description of type String!.
type Query {
“Get Space Cats”
spaceCats: [SpaceCat]
}
type SpaceCat {
id: ID!
name: String!
age: Int
missions: [Mission]
}
type Mission {
id: ID!
name: String!
description: String!
}
Which of these are purposes of a GraphQL server?
1 - Receiving incoming GraphQL queries
2 - Validating GraphQL queries against our schema
3 - Creating GraphQL queries
4 - Exposing a separate endpoint for each schema type
5 - Returning populated schema fields as a response
(V) 1 - Receiving incoming GraphQL queries
(V) 2 - Validating GraphQL queries against our schema
(F) 3 - Creating GraphQL queries
(F) 4 - Exposing a separate endpoint for each schema type
(V) 5 - Returning populated schema fields as a response
Which of these are purposes of a GraphQL server?
1 - Receiving incoming GraphQL queries
2 - Validating GraphQL queries against our schema
3 - Creating GraphQL queries
4 - Exposing a separate endpoint for each schema type
5 - Returning populated schema fields as a response
(V) 1 - Receiving incoming GraphQL queries
(V) 2 - Validating GraphQL queries against our schema
(F) 3 - Creating GraphQL queries
(F) 4 - Exposing a separate endpoint for each schema type
(V) 5 - Returning populated schema fields as a response
Which of these are benefits of using the Apollo Studio Explorer?
You can step through your schema to discover available types and fields
You can build and iterate on queries faster
How do we make Apollo Client available to our app’s React components?
We wrap our React component tree in the ApolloProvider component
https://www.apollographql.com/tutorials/lift-off-part1/08-apollo-client-setup
[Code Challange] Create a ListSpaceCats query with a spaceCats query field and its name, age and missions selection set in that order. For the missions field, select name and description
query ListSpaceCats {
spaceCats {
name
age
missions {
name
description
}
}
}
What are the best practices when creating client queries?
Include only the fields that the client requires.
Wrap each query in the gql template literal.
Test out queries in the Apollo Studio Explorer and copy then over
Assign each query string to a constant with an ALL_CAPS name