#GraphQL
can be used with several popular libraries for Node.js: Express, Koa, Hapi. Apollo itself is kept library agnostic, so it is possible to connect it with a lot of third-party libraries in client but also server applications.
An abstraction around apollo, graphql, express to give you everything you need to create a graphql server
Middleware library for Node.js.
Two parts to a graqph ql server: Schema and resolvers. GraphQL is a type based thin layer over an api. Think typescript for your api. A schema is a description of what your api should look like.
input type is a specidal type that can only be used for arguments
# input example
input NewPersonInput {
name: String!
}
type mutation {
newPerson(input: NewPersonInput!): Person!
}
A type is a custom shape that describles fields and what types of data those fields have "!" means can never be null
The GraphQL schema is all the available data for reading (and writing) data via GraphQL. It can happen from any client who consumes the GraphQL API. The schema consists of type definitions, starting with a mandatory top level Query type for reading data (type Query), and then followed by fields and nested fields.
const schema = gql`
# Mandatory root Query type
type Query {
# me field with type User (Which means it must have a username subfield with type String
me: User
}
# Custom type definition
type User {
username: String!
}
`;
The counterpart of the GraphQL schema for setting up a Apollo Server are the resolvers which are used to return data for your fields from the schema. The data source doesn’t matter, because the data can be hardcoded , can come from a database, or from another (RESTful) API endpoint. That’s why GraphQL shouldn’t be mistaken for a database query language. Resolvers are only functions which resolve data for your GraphQL fields in the schema.
Query: {
# Resolver function name must match the field name in the schema query (me)
me: () => {
# the return value of the function must match the schema of the query return type (User)
return {
username: 'kdipaolo',
};
},
},
In the client side query we can write:
{
me {
username
}
}
and get the following data returned:
{
"data": {
"me": {
"username": "kdipaolo"
}
}
}
Also called Enums, enumeration types are a special kind of scalar that is restricted to a particular set of allowed values.
enum FoodTypes {
burger
nachos
tacos
}
type Person {
id: ID!
name: String!
favoriteFood: FoodTypes
}
`