Advertisement
stronk_8s

GRAPHQL BASIC API CODE

Aug 27th, 2024 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.15 KB | Source Code | 0 0
  1. const { ApolloServer, gql } = require("apollo-server");
  2.  
  3. // typeDefs tell the GraphQL server what data to expect
  4. // Notice the gql tag, this converts your string into GraphQL strings that can be read by Apollo
  5. const users = [
  6.     {
  7.         firstName: "GraphQL",
  8.         lastName: "isCool",
  9.         email: "GraphQL@isCool.com"
  10.     },
  11. ];
  12.  
  13. const typeDefs = gql`
  14.     # GraphQL enables us to create our own types
  15.     # Notice the "User" type matches the shape of our "database"
  16.     type User {
  17.         firstName: String!
  18.         lastName: String!
  19.         email: String!
  20.     }
  21.     type Query {
  22.         hello: String!
  23.         randomNumber: Int!
  24.         # This query is going to return all the users in our array
  25.         # Since our "database" is an array containing objects, we need to create a "User" type
  26.         # Brackets around the type indicates the query is returning an array
  27.         queryUsers: [User]!
  28.     }
  29.     type Mutation{
  30.         # We are creating a mutation called "addUser" that takes in 3 arguments
  31.         # These arguments will be available to our resolver, which will push the new user to the "users" array
  32.         # Notice that this mutation will return a single User, which will be the one that was created
  33.         addUser(firstName:String!, lastName:String!, email:String!): User!
  34.     }
  35. `;
  36.  
  37.  
  38.  
  39. // the Query type outlines all the queries that can be called by the client
  40. // hello and randomNumber are the names of the queries
  41. // The exclamation mark (!) tells Apollo Server that a result is required
  42.  
  43. // Here, we define two queries, one returns a String and another returns a Int
  44.  
  45. // When a query is called a resolver with the same name is run
  46. // The API returns whatever is returned by the resolver
  47. // We are using arrow functions so the "return" keyword is not required
  48. const resolvers = {
  49.     // The name of the resolver must match the name of the query in the typeDefs
  50.     Query: {
  51.         // When the hello query is invoked "Hello world" should be returned
  52.         hello: () => "Hello world!",
  53.         // When we call the randomNumber query, it should return a number between 0 and 10
  54.         randomNumber: () => Math.round(Math.random() * 10),
  55.         // queryUsers simply returns our users array
  56.         queryUsers: () => users,
  57.     },
  58.     Mutation: {
  59.         // Once again notice the name of the resolver matches what we defined in our typeDefs
  60.         // The first argument to any resolver is the parent, which is not important to us here
  61.         // The second argument, args, is an object containing all the arguments passed to the resolver
  62.         addUser: (parent, args) => {
  63.             users.push(args); // Push the new user to the users array
  64.             return args; // Returns the arguments provided, this is the new user we just added
  65.         },
  66.     },
  67. };
  68.  
  69. // Create an instance of ApolloServer and pass in our typeDefs and resolvers
  70. const server = new ApolloServer({
  71.     // If the object key and value have the same name, you can omit the key
  72.     typeDefs,
  73.     resolvers,
  74. });
  75.  
  76.  
  77.  
  78. // Start the server at port 8080
  79. server.listen({ port: 8080 }).then(({ url }) => console.log(`GraphQL server running at ${url}`));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement