Fields is a function that returns an object, which lists the attributes of our Schema, using the types imported above.
When nesting schemas' you use that SchemaType to create a 'relationship' between the data. Similar to how Foreign Keys in SQL works. It creates integrity.
The parent parameter contains the information from the parent SchemaType.
constBookType=newGraphQLObjectType({name: 'Book',// fields are our properties// will help overcome reference errors when we have multiple typesfields: ()=>({id: {type: GraphQLID},// GraphQLID allows you to query by ID as an integer or string => but args.id is still a string typename: {type: GraphQLString},genre: {type: GraphQLString},// nestingauthor: {type: AuthorType,// parent object contains the data that was passed through from the parent query (Book)resolve(parent,args){const{ authorId }=parent;// return _.find(authors, { id: authorID });returnAuthor.findById(authorId);},},}),});
Sample mutation
Fields are essentially functions used to create, update, or delete rows from our database.
Args is an object that lists the parameters, used to query our database.
Resolve is essentially the database queries we run to fetch our data (models)
constMutation=newGraphQLObjectType({name: 'Mutation',fields: {addAuthor: {type: AuthorType,// expect the client to send dataargs: {name: {type: GraphQLString},age: {type: GraphQLInt},},resolve(parent,{ name, age }){constauthor=newAuthor({ name, age });returnauthor.save();},},},});