SHARE
TWEET

Untitled

a guest Jan 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const graphql = require('graphql');
  2.  
  3. const { GraphQLObjectType, GraphQLString,
  4.        GraphQLID, GraphQLInt, GraphQLSchema } = graphql;
  5.  
  6. //Schema defines data on the Graph like object types(book type), relation between
  7. //these object types and descibes how it can reach into the graph to interact with
  8. //the data to retrieve or mutate the data  
  9.  
  10. var books = [
  11.     { name:"Book 1", pages:432 , id:1},
  12.     { name: "Book 2", pages: 32, id: 2},
  13.     { name: "Book 3", pages: 532, id: 3 }
  14. ]
  15.  
  16. const BookType = new GraphQLObjectType({
  17.     name: 'Book',
  18.     fields: () => ({
  19.         id: { type: GraphQLID  },
  20.         name: { type: GraphQLString },
  21.         pages: { type: GraphQLInt }
  22.     })
  23. });
  24.  
  25. //RootQuery describe how users can use the graph and grab data.
  26. //E.g Root query to get all authors, get all books, get a particular book
  27. //or get a particular author.
  28. const RootQuery = new GraphQLObjectType({
  29.     name: 'RootQueryType',
  30.     fields: {
  31.         book: {
  32.             type: BookType,
  33.             //argument passed by the user while making the query
  34.             args: { id: { type: GraphQLID } },
  35.             resolve(parent, args) {
  36.                 //Here we define how to get data from database source
  37.  
  38.                 //this will return the book with id passed in argument by the user
  39.                 return books.find((item) => { return item.id == args.id});
  40.             }
  41.         }
  42.     }
  43. });
  44.  
  45. //Creating a new GraphQL Schema, with options query which defines query
  46. //we will allow users to use when they are making request.
  47. module.exports = new GraphQLSchema({
  48.     query: RootQuery
  49. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top