This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//required objects | |
import { | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLID, | |
GraphQLString, | |
GraphQLNonNull, | |
GraphQLList, | |
GraphQLInterfaceType, | |
} from 'graphql'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Schema = new GraphQLSchema({ | |
query: Query //defined above | |
}); | |
export default Schema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
pokemon(id: "1") { | |
name | |
thumbnail | |
id | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const pokemonType = new GraphQLObjectType({ | |
name: 'pokemon', | |
fields: () => ({ | |
id: {type: GraphQLString}, | |
name: {type: GraphQLString}, | |
thumbnail: {type: GraphQLString} | |
}), | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
} | |
... } |
OlderNewer