Skip to content

Instantly share code, notes, and snippets.

Avatar

Jordan Last lastmjs

View GitHub Profile
@lastmjs
lastmjs / block-full-stack-mentor-assessment.md
Last active Aug 25, 2019
Full Stack Mentor - Candidate Assessment
View block-full-stack-mentor-assessment.md

JavaScript

Hey Student,

Oh dear, this is a tricky one. It's definitely messed me up in the past. What's happening here is an issue of scoping and closures. A few things to point you in the right direction:

  1. Study block scoping versus function scoping in JavaScript
  2. Study JavaScript's variable declarators, var, let, and const, and understand the differences between their uses
  3. Study up a bit on closures in JavaScript
  4. With your new knowledge, try to make btnNum block scoped instead of function scoped
View index.ts
import {GraphQLServer} from 'graphql-yoga';
import {makeExecutableSchema} from 'graphql-tools';
import {
Prisma,
Query,
Mutation
} from './generated/prisma';
import {readFileSync} from 'fs';
import {parse} from 'graphql';
import {
View dataops.graphql
# This is where we define any custom resolvers, and where we add custom directives to any generated Prisma resolvers
type AuthPayload {
token: String!
user: User!
}
type Mutation {
# Custom resolvers
signup(email: String!, password: String!): AuthPayload!
View ultimate-schema.ts
const ultimateSchemaString = mergeTypes([
readFileSync('./schema/datamodel.graphql').toString(),
readFileSync('./schema/dataops.graphql').toString(),
readFileSync('./schema/directives.graphql').toString(),
readFileSync('./generated/prisma.graphql').toString()
], {
all: true
});
const ultimateSchema = makeExecutableSchema({
typeDefs: ultimateSchemaString,
View user-owns-directive-resolver.ts
export async function userOwnsDirectiveResolver(next, source, args, context) {
if (source[args.field] === await getUserId(context)) {
return await next();
}
else {
throw new Error('Not authorized');
}
}
View directives.graphql
directive @userOwns(field: String) on FIELD | FIELD_DEFINITION
directive @authenticated on FIELD | FIELD_DEFINITION
directive @private on FIELD | FIELD_DEFINITION
View datamodel.graphql
# All types defined here will be fed through the Prisma generation process
# Essentially, all of these types become database tables
type User {
id: ID! @unique
createdAt: DateTime! @private
updatedAt: DateTime! @private
email: String! @unique @userOwns(field: "id")
password: String! @private
}
View private-directive-resolver.ts
export async function privateDirectiveResolver(next, source, args, context) {
throw new Error('Private');
}
View authenticated-directive-resolver.ts
export async function authenticatedDirectiveResolver(next, source, args, context) {
if (getUserId(context)) {
return await next();
}
else {
throw new Error('Not authenticated');
}
}
View directive-resolvers.ts
const directiveResolvers = {
userOwns: userOwnsDirectiveResolver,
authenticated: authenticatedDirectiveResolver,
private: privateDirectiveResolver
};
You can’t perform that action at this time.