Skip to content

Instantly share code, notes, and snippets.

View rohan-varma's full-sized avatar

Rohan Varma rohan-varma

View GitHub Profile
//required objects
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLNonNull,
GraphQLList,
GraphQLInterfaceType,
} from 'graphql';
const Query = new GraphQLObjectType({
name: 'Query',
//our server can return pokemon and moves.
fields: {
pokemon: {
type: pokemonType, //the type we defined earlier
args: {
id: {type: GraphQLString} //an id can be passed in to the query to select the pokemon needed.
},
resolve: (_,args) => data[args.id], //uses the id as a key to fulfill the request. Note that if there's no id passed in,
const Schema = new GraphQLSchema({
query: Query //defined above
});
export default Schema;
{
pokemon(id: "1") {
name
thumbnail
id
}
}
const pokemonType = new GraphQLObjectType({
name: 'pokemon',
fields: () => ({
id: {type: GraphQLString},
name: {type: GraphQLString},
thumbnail: {type: GraphQLString},
//our favoriteMove is a moveType, which was defined earlier.
favoriteMove: {
type: moveType,
resolve: (parent, _) => moves[parent.id], //defines how this query is fulfilled.
ajax.get('http://website.com/api/v1/pokemonlightweight/1')
.done((data) => {
const moveIDString = getMoveString(data); //parse the move id from the response
ajax.get('http://website.com/api/v1/moves/${moveIDString}')
.done((data) => { //process the favorite move data
});
});
const pokemonType = new GraphQLObjectType({
name: 'pokemon',
fields: () => ({
id: {type: GraphQLString},
name: {type: GraphQLString},
thumbnail: {type: GraphQLString}
}),
});
int* getProductsOfAllIntsExceptAtIndex(int* arr, int len) {
int* x = new int[len];
for(int i = 0 ; i < len ; i++) {
x[i] = getProduct(arr, len, i);
}
return x;
}
int getProduct(int* arr, int len, int index) {
//return product of all ints except at that index
int prod = 1;
for(int i = 0 ; i < len; i++) {
if(i!=index) {
prod*=arr[i];
}
}
return prod;
}
int* getProductsOfAllIntsExceptAtIndexFaster(int* arr, int len) {
int* beforeIndex = new int[len]; //array that keeps track of products before that index
beforeIndex[0] = 1; // no products before the 0th index
for(int i = 1; i < len ; i ++) {
beforeIndex[i] = beforeIndex[i-1] * arr[i-1]; //multiply by the element at the index before the current one.
}
... }