Instead of creating multiple endpoints where each endpoint returns a different response (REST), create 1 endpoint which returns a response depending on the request (GraphQL query)
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
// create new HelloWorld object
name: "HelloWorld",
fields: () => ({
message: {
type: GraphQLString,
resolve: () => "hey there",
},
}),
}),
});- We create schemas which define types with fields
- Fields are all the properties of the object we can query and return data from
- Also specifies which queries and mutations are available to the client
const RootQueryType = new GraphQLObjectType({
name: "Query",
description: "Root Query",
fields: () => ({
...
}),
});const schema = new GraphQLSchema({
query: RootQueryType,
mutation: RootMutationType,
});- Create separate
Roottypes for each schema containing their respective query/mutation fields
query {
message
}Response
"data": {
"message": "hey there"
}
}- Very similar to JSON syntax - specify keys and GraphQL will return their respective values
book: {
type: BookType,
description: "A single book",
args: {
id: { type: GraphQLInt },
},
resolve: (parent, args) => books.find((book) => book.id === args.id),
},query {
book(id: 1)
}- We can pass in arguments into queries
addBook: {
type: BookType,
description: "Add a book",
args: {
name: { type: GraphQLNonNull(GraphQLString) },
authorId: { type: GraphQLNonNull(GraphQLInt) },
},
resolve: (parent, args) => {
const book = {
id: books.length + 1,
name: args.name,
authorId: args.authorId,
};
books.push(book);
return book;
},
},- Very similar to creating a new query response
- Updates data in the current instance