Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@field()
text: string;
@field()
created_at: string;
@field()
retweet_count: number;
@field()
favorite_count: number;
// Due to the circular reference between Tweet and User
// the resolution of User needs to be deferred (via thunk function).
@field(() => User)
get user(): User {
return new User(this.data.user);
}
/**
* Create an instance of the Tweet output type.
* @param data Tweet data from the API.
*/
constructor(data: ITweet) {
this.data = data;
this.id = data.id_str;
this.text = data.text;
this.created_at = data.created_at;
this.retweet_count = data.retweet_count;
this.favorite_count = data.favorite_count;
}
@type
class XKCDQuery {
@field(Comic)
async comic(@arg(ID) id) {
const url = id ? `/${id}` : "";
const { data } = await axios.get(`https://xkcd.com${url}/info.0.json`);
return new Comic(data);
}
@field(Comic)
latest() {
return this.comic();
}
@field(Comic)
async random() {
const { number } = await this.comic();
return this.comic(Math.floor(Math.random() * number));
}
}
const server = new ApolloServer({
schema: createSchema(XKCDQuery),
context: new XKCDQuery()
});
server.listen().then(({ url }) => console.log(`Server ready at ${url}`));
@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) {
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 {
arg,
context,
createSchema,
description,
field,
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;
}
}
/**
@field(String)
get transcript() {
return this.data.transcript || this.data.alt;
}
@field(String)
get date() {
return new Date(
this.data.year,
this.data.month,
this.data.day
).toLocaleDateString();
}
@field(String)
get link() {
return `https://xkcd.com/${this.number}`;
}
@field(Comic)
async previous() {
return this.context.comic(this.number - 1);
}
@field(Comic)
async next() {
return this.context.comic(this.number + 1);
}
constructor(data) {
this.data = data;
@field(String)
get title() {
return this.data.title;
}
@field(String)
get image() {
return this.data.img;
}
@field(String)
get transcript() {
return this.data.transcript || this.data.alt;
}
@field(String)
get date() {
return new Date(
this.data.year,
this.data.month,
this.data.day
).toLocaleDateString();
}
@field(String)
get link() {
return `https://xkcd.com/${this.number}`;
}
@field(Comic)
async previous() {
return this.context.comic(this.number - 1);
@field(String)
get date() {
return new Date(
this.data.year,
this.data.month,
this.data.day
).toLocaleDateString();
}
@field(String)
get link() {
return `https://xkcd.com/${this.number}`;
}
@field(Comic)
async previous() {
return this.context.comic(this.number - 1);
}
@field(Comic)
async next() {
return this.context.comic(this.number + 1);
}
constructor(data) {
this.data = data;
}
}
@type
class XKCDQuery {
name: 'carol',
}));
/**
* 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.')
this.data.month,
this.data.day
).toLocaleDateString();
}
@field(String)
get link() {
return `https://xkcd.com/${this.number}`;
}
@field(Comic)
async previous() {
return this.context.comic(this.number - 1);
}
@field(Comic)
async next() {
return this.context.comic(this.number + 1);
}
constructor(data) {
this.data = data;
}
}
@type
class XKCDQuery {
@field(Comic)
async comic(@arg(ID) id) {
const url = id ? `/${id}` : "";
const { data } = await axios.get(`https://xkcd.com${url}/info.0.json`);
return new Comic(data);
field,
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)