Last active
June 6, 2023 06:43
-
-
Save infysumanta/f648647aea219f4bf0ddfe012c92d98a to your computer and use it in GitHub Desktop.
a graphql query
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 express = require("express"); | |
const { graphqlHTTP } = require("express-graphql"); | |
const { | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLString, | |
GraphQLList, | |
GraphQLInt, | |
GraphQLNonNull, | |
} = require("graphql"); | |
const app = express(); | |
const authors = [ | |
{ id: 1, name: "Author One" }, | |
{ id: 2, name: "Author Two" }, | |
{ id: 3, name: "Author Three" }, | |
]; | |
const books = [ | |
{ id: 1, name: "Books One", authorId: 1 }, | |
{ id: 2, name: "Books Two", authorId: 1 }, | |
{ id: 3, name: "Books Three", authorId: 1 }, | |
{ id: 4, name: "Books Four", authorId: 2 }, | |
{ id: 5, name: "Books Five", authorId: 2 }, | |
{ id: 6, name: "Books Six", authorId: 3 }, | |
{ id: 7, name: "Books Seven", authorId: 3 }, | |
{ id: 8, name: "Books Eight", authorId: 2 }, | |
{ id: 9, name: "Books Night", authorId: 2 }, | |
{ id: 10, name: "Books Ten", authorId: 1 }, | |
]; | |
const BookType = new GraphQLObjectType({ | |
name: "Book", | |
description: "Single Books Types", | |
fields: () => ({ | |
id: { type: new GraphQLNonNull(GraphQLInt) }, | |
name: { type: new GraphQLNonNull(GraphQLString) }, | |
authorId: { type: new GraphQLNonNull(GraphQLInt) }, | |
author: { | |
type: AuthorType, | |
resolve: (book) => { | |
return authors.find((author) => author.id === book.authorId); | |
}, | |
}, | |
}), | |
}); | |
const AuthorType = new GraphQLObjectType({ | |
name: "Author", | |
description: "This represents a author of a book", | |
fields: () => ({ | |
id: { type: new GraphQLNonNull(GraphQLInt) }, | |
name: { type: new GraphQLNonNull(GraphQLString) }, | |
books: { | |
type: new GraphQLList(BookType), | |
resolve: (author) => { | |
return books.filter((book) => book.authorId === author.id); | |
}, | |
}, | |
}), | |
}); | |
const rootQueryType = new GraphQLObjectType({ | |
name: "Query", | |
description: "Root Query", | |
fields: () => ({ | |
book: { | |
type: BookType, | |
description: "A Single Book", | |
args: { | |
id: { type: GraphQLInt }, | |
}, | |
resolve: (parent, args) => books.find((book) => book.id === args.id), | |
}, | |
books: { | |
type: new GraphQLList(BookType), | |
description: "All the Books", | |
resolve: () => books, | |
}, | |
author: { | |
type: AuthorType, | |
description: "A Single Author", | |
args: { | |
id: { type: GraphQLInt }, | |
}, | |
resolve: (parent, args) => | |
authors.find((author) => author.id === args.id), | |
}, | |
authors: { | |
type: new GraphQLList(AuthorType), | |
description: "All the Authors", | |
resolve: () => authors, | |
}, | |
}), | |
}); | |
const rootMutationType = new GraphQLObjectType({ | |
name: "Mutation", | |
description: "Root Mutation", | |
fields: () => ({ | |
addBook: { | |
type: BookType, | |
description: "Add a Book", | |
args: { | |
name: { type: new GraphQLNonNull(GraphQLString) }, | |
authorId: { type: new GraphQLNonNull(GraphQLInt) }, | |
}, | |
resolve: (parent, args) => { | |
const book = { | |
id: books.length + 1, | |
name: args.name, | |
authorId: args.authorId, | |
}; | |
books.push(book); | |
return book; | |
}, | |
}, | |
addAuthor: { | |
type: AuthorType, | |
description: "Add a Author", | |
args: { | |
name: { type: new GraphQLNonNull(GraphQLString) }, | |
}, | |
resolve: (parent, args) => { | |
const author = { | |
id: authors.length + 1, | |
name: args.name, | |
}; | |
authors.push(author); | |
return author; | |
}, | |
}, | |
}), | |
}); | |
const schema = new GraphQLSchema({ | |
query: rootQueryType, | |
mutation: rootMutationType, | |
}); | |
app.use( | |
"/graphql", | |
graphqlHTTP({ | |
schema: schema, | |
graphiql: true, | |
}) | |
); | |
module.exports = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment