Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
dungeon2567 committed Jul 18, 2019
1 parent 5fe3e1e commit 4c728b5
Show file tree
Hide file tree
Showing 5,407 changed files with 467,725 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
141 changes: 141 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
const { ApolloServer, gql } = require('apollo-server');
const { SchemaDirectiveVisitor } = require('graphql-tools');

const { createError } = require('apollo-errors');

const { ApolloProjector, IncludeAll, IgnoreField, default: makeProjection } = require('graphql-db-projection');

const AuthorizationError = createError('AuthorizationError', {
message: 'You are not authorized.'
});

const { DirectiveLocation, GraphQLDirective, GraphQLList, GraphQLString, GraphQLNonNull } = require('graphql');


class AuthDirective extends SchemaDirectiveVisitor {
static getDirectiveDeclaration(directiveName, schema) {
return new GraphQLDirective({
name: 'auth',
locations: [ DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.OBJECT ],
args: {
roles: {
type: new GraphQLList(new GraphQLNonNull(schema.getType('Role'))),
defaultValue: 'reader'
}
}
});
}

visitFieldDefinition(field, x) {
const { resolve = defaultFieldResolver } = field;

field.resolve = async (root, args, context, info) => {
const allowedRoles = this.args.roles || [];

const user = await context.getUser();

if (!user || !user.roles.some((role) => allowedRoles.includes(role))) {
throw new Error('Unauthorized');
}

return resolve.call(this, root, args, context, info);
};
}

visitObject(obj) {
const fields = obj.getFields();
const expectedRoles = this.args.roles;

Object.keys(fields).forEach((fieldName) => {
const field = fields[fieldName];
const next = field.resolve;

this.visitFieldDefinition(field);
});
}
}

class ModelDirective extends SchemaDirectiveVisitor {
static getDirectiveDeclaration(directiveName, schema) {
return new GraphQLDirective({
name: 'model',
locations: [ DirectiveLocation.FIELD_DEFINITION ],
args: {
table: {
type: GraphQLString,
defaultValue: null
}
}
});
}

visitFieldDefinition(field) {
for (const [ name, subfield ] of Object.entries(field.type.ofType._fields)) {
field.args.push({
name,
description: '',
type: subfield.type instanceof GraphQLNonNull ? subfield.type.ofType : subfield.type,
defaultValue: undefined
});
}
const { resolve = defaultFieldResolver } = field;

field.resolve = async (root, args, context, info) => {
const { table } = this.args;

const projection = makeProjection(info);

debugger;

return resolve.call(this, root, args, context, info);
};
}
}
// The GraphQL schema
const typeDefs = gql`
enum Role {
ADMIN
USER
}
type Hero {
name: String!
}
directive @auth(roles: [Role!]) on FIELD_DEFINITION | OBJECT
directive @model(table: String!) on FIELD_DEFINITION
directive @project(projection: String, projections: [String], nameInDB: String) on FIELD_DEFINITION
directive @all on FIELD_DEFINITION
directive @ignore on FIELD_DEFINITION
type Query {
heroes(x: Int): [Hero] @model(table: "hero")
}
`;

// A map of functions which return data for the schema.
const resolvers = {
Query: {
heroes: () => 'world'
}
};

const server = new ApolloServer({
typeDefs,
resolvers,
schemaDirectives: {
project: ApolloProjector,
all: IncludeAll,
ignore: IgnoreField,
auth: AuthDirective,
model: ModelDirective
}
});

server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
1 change: 1 addition & 0 deletions node_modules/.bin/atob

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/is-ci

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/mime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/nodemon

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/nodetouch

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/nopt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/pbjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/pbts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/rc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/semver

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/sha.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/uuid

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node_modules/.bin/which

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions node_modules/@apollographql/apollo-tools/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

160 changes: 160 additions & 0 deletions node_modules/@apollographql/apollo-tools/lib/buildServiceDefinition.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions node_modules/@apollographql/apollo-tools/lib/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4c728b5

Please sign in to comment.