Created
December 5, 2016 17:42
-
-
Save thebigredgeek/ac15497e90746753696268e503576bbd to your computer and use it in GitHub Desktop.
Apollo / GraphQL basics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Mutation { | |
# Logs in an Existing user object | |
loginUser ( | |
# LoginUserInput to log in a user via login | |
user: UserLoginInput! | |
): User # Returns a User... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mutation loginUser($user: UserLoginInput!){ | |
user: loginUser(user: $user) { | |
id | |
name | |
CurrentLocation (home: true) { | |
id | |
googleName | |
googlePlaceId | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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