Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await server.start();
let binding: Binding;
try {
binding = ((await server.getBinding()) as unknown) as Binding;
} catch (error) {
Logger.error(error);
return process.exit(1);
}
let project: Project = (null as unknown) as Project;
try {
project = ((await createProject(binding as any)) as unknown) as Project;
Logger.log(project);
} catch (err) {
const error = getBindingError(err);
Logger.error(error);
}
try {
// Create environment
let environment: Environment;
const environments: Environment[] = [];
environment = await createEnvironment(binding as any, project.key, 'production');
environments.push(environment);
environment = await createEnvironment(binding as any, project.key, 'staging');
environments.push(environment);
environment = await createEnvironment(binding as any, project.key, 'development');
environments.push(environment);
// Create flags
await createFeatureFlag(binding as any, project.key, 'alpha-map-view');
import { BaseModel, Model, OneToMany, StringField } from 'warthog';
import { Environment } from '../environment/environment.model';
import { FeatureFlagSegment } from '../feature-flag-segment/feature-flag-segment.model';
import { FeatureFlagUser } from '../feature-flag-user/feature-flag-user.model';
import { FeatureFlag } from '../feature-flag/feature-flag.model';
import { Segment } from '../segment/segment.model';
import { UserSegment } from '../user-segment/user-segment.model';
import { Matches } from 'class-validator';
@Model()
export class Project extends BaseModel {
@StringField({ maxLength: 50, minLength: 3, nullable: false })
name!: string;
@Matches(/^[a-z0-9]+(-[a-z0-9]+)*$/) // Lowercase sluggified string
@StringField({ maxLength: 20, minLength: 3, nullable: false, unique: true })
key!: string;
@OneToMany(() => Environment, (environment: Environment) => environment.project)
environments?: Environment[];
@OneToMany(() => Segment, (segment: Segment) => segment.project)
segments?: Segment[];
@OneToMany(() => FeatureFlag, (featureFlag: FeatureFlag) => featureFlag.project)
featureFlags?: FeatureFlag[];
import { BaseModel, EmailField, EnumField, Model, OneToMany, StringField } from 'warthog';
import { Post } from './post.model';
// Note: this must be exported and in the same file where it's attached with @EnumField
// Also - must use string enums
export enum UserStatus {
ACTIVE = 'ACTIVE',
INACTIVE = 'INACTIVE'
}
@Model()
export class User extends BaseModel {
@StringField({ maxLength: 30 })
firstName?: string;
@StringField({ maxLength: 50, minLength: 2, nullable: true })
lastName?: string;
@EnumField('UserStatus', UserStatus)
status?: UserStatus;
@EmailField() email?: string;
@OneToMany(() => Post, (post: Post) => post.user)
posts?: Post[];
}
// Note: this must be exported and in the same file where it's attached with @EnumField
// Also - must use string enums
export enum UserStatus {
ACTIVE = 'ACTIVE',
INACTIVE = 'INACTIVE'
}
@Model()
export class User extends BaseModel {
@StringField({ maxLength: 30 })
firstName?: string;
@StringField({ maxLength: 50, minLength: 2, nullable: true })
lastName?: string;
@EnumField('UserStatus', UserStatus)
status?: UserStatus;
@EmailField() email?: string;
@OneToMany(() => Post, (post: Post) => post.user)
posts?: Post[];
}
export function getServer(AppOptions = {}, dbOptions = {}) {
return new Server(
{
// Inject a fake user. In a real app you'd parse a JWT to add the user
context: (request: any) => {
const userId = JSON.stringify(request.headers).length.toString();
return {
user: {
id: `user:${userId}`
}
};
},
introspection: true,
logger: Logger,
...AppOptions
},
dbOptions
static logGraphQLError(error: Error) {
console.error(util.inspect(getBindingError(error), { showHidden: false, depth: null }));
}
}
import { BaseModel, ManyToOne, Model, StringField } from 'warthog';
import { User } from '../user/user.model';
@Model()
export class Post extends BaseModel {
@StringField({ maxLength: 256 })
title?: string;
@ManyToOne(() => User, (user: User) => user.posts, { nullable: false })
user?: User;
}
);
Logger.info(project);
const flagsA = await binding.query.featureFlagsForUser({
where: { projKey: project.key, envKey: 'production', userKey: 'user-a' }
});
Logger.info(flagsA);
const flagsB = await binding.query.featureFlagsForUser({
where: { projKey: project.key, envKey: 'production', userKey: 'user-b' }
});
Logger.info(flagsB);
} catch (err) {
const error = getBindingError(err);
Logger.error(error);
}
return server.stop();
}