Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@context
context;
@field(User)
@description('Get a user from the array.')
add(@arg(ID) @required id, @arg(String) @required name) {
const user = new User({
id,
name,
});
users[id] = user;
return user;
}
@field(Boolean)
@description('Delete a user from the array.')
del(@arg(ID) @required id) {
if (users[id]) {
delete users[id];
return true;
}
return false;
}
}
/**
* Now we are ready to create our ApolloServer and start listening for requests.
*/
const server = new ApolloServer({
schema: createSchema(UserQuery, UserMutation),
// context: new UserQuery()
@field(User)
@description("Fetch details for a user by screen name.")
async user(
@arg(ID)
@required
screen_name: string,
@context context: TwitterContext
) {
const { data } = await context.client.get("/users/show", {
params: { screen_name }
});
return new User(data);
}
@field([Tweet])
@description("Fetch the status timeline for a user by screen name.")
async user_timeline(
@arg(ID)
@required
screen_name: string,
@arg(TimelineInput)
@defaultValue(new TimelineInput())
input: Partial,
@context context: TwitterContext
): Promise {
const args = new TimelineInput(input);
const { data } = await context.client.get(
"/statuses/user_timeline",
{ params: { screen_name: screen_name, ...args.toParams() } }
);
return data.map(makeTweet);
}
* server.
*/
const users = [];
/**
* Our very simple User class just represents the user ID and the name
*/
@type
class User {
@field(ID)
get id() {
return this.data.id;
}
@field(String)
@description('This is the name of the current user')
get name() {
return this.data.name;
}
constructor(data) {
this.data = data;
}
}
/*
* Now that we've define the user class we'll add a few users to
* our users array.
*/
users.push(new User({
id: 0,
name: 'alice',
@description('Get all of the users in the array.')
users() {
return users;
}
}
/**
* This is our mutation class. It makes it possible to make changes to our data.
*/
@type
class UserMutation {
@context
context;
@field(User)
@description('Get a user from the array.')
add(@arg(ID) @required id, @arg(String) @required name) {
const user = new User({
id,
name,
});
users[id] = user;
return user;
}
@field(Boolean)
@description('Delete a user from the array.')
del(@arg(ID) @required id) {
if (users[id]) {
delete users[id];
return true;
}
import { AxiosInstance } from "axios";
import Tweet, { ITweet } from "./types/Tweet";
import SearchInput from "./inputs/SearchInput";
import makeTweet from "./lib/makeTweet";
import TimelineInput from "./inputs/TimelineInput";
export interface TwitterContext {
client: AxiosInstance;
twitter: Twitter;
}
@type
@description("This is the base query for Twitter.")
export default class Twitter {
@field(User)
@description("Fetch details for a user by screen name.")
async user(
@arg(ID)
@required
screen_name: string,
@context context: TwitterContext
) {
const { data } = await context.client.get("/users/show", {
params: { screen_name }
});
return new User(data);
}
@field([Tweet])
@description("Fetch the status timeline for a user by screen name.")
async user_timeline(
@arg(ID)
description
} from "graphql-schema-bindings";
import User, { IUser } from "./types/User";
import { AxiosInstance } from "axios";
import Tweet, { ITweet } from "./types/Tweet";
import SearchInput from "./inputs/SearchInput";
import makeTweet from "./lib/makeTweet";
import TimelineInput from "./inputs/TimelineInput";
export interface TwitterContext {
client: AxiosInstance;
twitter: Twitter;
}
@type
@description("This is the base query for Twitter.")
export default class Twitter {
@field(User)
@description("Fetch details for a user by screen name.")
async user(
@arg(ID)
@required
screen_name: string,
@context context: TwitterContext
) {
const { data } = await context.client.get("/users/show", {
params: { screen_name }
});
return new User(data);
}
@field([Tweet])
screen_name: string;
@field()
name: string;
@field()
followers_count: number;
@field()
friends_count: number;
@field()
statuses_count: number;
@field(() => [Tweet])
@description("Fetch the Tweet timeline for this user.")
async timeline(
@arg(TimelineInput)
@defaultValue(new TimelineInput())
input: Partial
): Promise {
return this.context.twitter.user_timeline(
this.screen_name,
input,
this.context
);
}
/**
* Create an instance of the User output type.
* @param data The response data from the API.
*/
ID,
type
} from "graphql-schema-bindings";
/**
* Our very simple User class just represents the user ID and the name
*/
@type
class User {
@field(ID)
get id() {
return this.data.id;
}
@field(String)
@description('This is the name of the current user')
get name() {
return this.data.name;
}
constructor(data) {
this.data = data;
}
}
/**
* This is our query class. It is the entry point for all queries to our application.
*/
@type
class UserQuery {
@field(User)
async user(@arg(ID) id) {
/**
* Interface for the API response data.
*/
export interface ITweet {
id_str: string;
text: string;
user: IUser;
created_at: string;
retweet_count: number;
favorite_count: number;
is_quote_status: boolean;
}
@type
@description("A Twitter status update (tweet)")
export default class Tweet {
data: ITweet;
@field(ID)
id: string;
@field()
text: string;
@field()
created_at: string;
@field()
retweet_count: number;
@field()
}));
/**
* This is our query class. It is the entry point for all queries to our application.
*/
@type
class UserQuery {
@field(User)
@description('Get a specific user.')
user(@arg(ID) id) {
return users[id];
}
@field([User])
@description('Get all of the users in the array.')
users() {
return users;
}
}
/**
* This is our mutation class. It makes it possible to make changes to our data.
*/
@type
class UserMutation {
@context
context;
@field(User)
@description('Get a user from the array.')
add(@arg(ID) @required id, @arg(String) @required name) {