Skip to content

Instantly share code, notes, and snippets.

@mechmillan

mechmillan/schema.js

Created Mar 18, 2018
Embed
What would you like to do?
const {
GraphQLSchema,
GraphQLObjectType,
GraphQLInt,
GraphQLString,
GraphQLList
} = require('graphql'); // Reference implementation of graphQL for JavaScript
// Creating a new Object Type, note self-documenting nature of the code
// static-types allow GraphiQL GUI to update with correct fields
// and returned value types
const PokemonType = new GraphQLObjectType({
name: 'Pokemon',
description: 'Basic Pokemon Object',
// What data do we care about?
// Go to: http://pokeapi.co/api/v2/pokemon/150 to see a sample JSON response
// We can now selectively target what data we want the client to have access to
// Resolve functions specify how the types and fields in the schema are
// connected to potentially different backends, “How do I get the data for Pokemon?”
// GraphQL resolve functions can contain any code
// A GraphQL server can to talk other backends, even other GraphQL servers
fields: () => ({
id: {
type: GraphQLInt,
resolve: json => json.id
},
name: {
type: GraphQLString,
resolve: json => json.name
},
weight: {
type: GraphQLInt,
resolve: json => json.weight
},
height: {
type: GraphQLInt,
resolve: json => json.height
},
moves: {
type: new GraphQLList(GraphQLString), // return moves as a list of strings
args: {
// what if only care about first 5 or last 5 moves?
limit: { type: GraphQLInt } // allow client to limit the returned results
},
resolve: (json, args) =>
json.moves.slice(0, args.limit).map(key => key.move.name)
}
})
});
// Exporting our schema object
module.exports = new GraphQLSchema({
// demonstrating query operation but can also define mutation
// and subscription operations
query: new GraphQLObjectType({
name: 'Query',
description: 'Query for basic Pokemon information',
fields: () => ({
Pokemon: {
type: PokemonType, // using our custom defined type
args: {
id: { type: GraphQLInt } // defines the structure of accepted args
},
resolve: (root, args, context) => context.pokemonLoader.load(args.id)
}
})
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment