Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Created December 5, 2016 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thebigredgeek/ac15497e90746753696268e503576bbd to your computer and use it in GitHub Desktop.
Save thebigredgeek/ac15497e90746753696268e503576bbd to your computer and use it in GitHub Desktop.
Apollo / GraphQL basics
type Location implements Resource {
# Primary key
id: ID!
# Google's name for the Location
googleName: String!
# Google's ID for the Location
googleId: ID!
# Google's Place ID for the Location
googlePlaceId: ID!
# Google's Timezone ID for the Location
googleTimezoneId: ID
# Google's Timezone name for the Location
googleTimezoneName: String
# Google's Timezone DST offset for the Location
googleTimezoneDstOffset: Int
# Google's Timezone raw offset for the Location
googleTimezoneRawOffset: Int
# Google's Url<GoogleIcon> for the Location
googleIcon: Url
# Google's latitude coordinate for the Location
googleCoordLat: Float
# Google's longitude coordinate for the Location
googleCoordLng: Float
}
type Mutation {
# Logs in an Existing user object
loginUser (
# LoginUserInput to log in a user via login
user: UserLoginInput!
): User # Returns a User...
}
mutation loginUser($user: UserLoginInput!){
user: loginUser(user: $user) {
id
name
email
CurrentLocation (home: true) {
id
googleName
googlePlaceId
}
}
}
type User {
# Primary key
id: ID!
# The User's name
name: String!
# The Location<Current> attached to the User
CurrentLocation: Location
}
# Used to login an existing User object.
input UserLoginInput {
# The User's email
email: Email
# THe User's password
password: String
}
const loginUser = async (root, { user: { email, password } }, context) => {
const { models: { User } } = context; // You can attach your Models to the context in Apollo's configuration function
const { user, token } = await User.login({ email, password });
if (!user) throw new LoginFailedError();
context.user = user; //attach user to context for subsequent edges etc
return user;
}
export default {
Mutation: {
loginUser // Maps to loginUser in the mutation definition
},
User: {
// Here, we define edges for the User type.
// GraphQL knows that the Login mutation returns a User because of the schema definition
// says so
// CurrentLocation is listed in the User schema as being of type Location
// Like every resolver, our edge resolver receives 3 props
// 1) Root - The node that we are coming from. In the case
// that we just logged in and are attempting to fetch
// the CurrentLocation of the User in the mutation query,
// Root will be the user returned from the Login resolver above
// 2) Args - The arguments specific to this part of the query. For instance, if we did:
// In this case, according to the sample login request below, the args are { home: true }
// 2) Context - A mutable "local" context passed through to all resolvers from Apollo for each request.
CurrentLocation: (user, args, context) => user.getCurrentLocation() // SequelizeJS association getter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment