Express GraphQL Server

GraphQL Types

  • String
  • Int
  • Float
  • Boolean
  • ID
  • Query
  • Mutation
  • ???

Query Type


const typeDefs = `
    type Query {
    greeting: String!
    }
`;

        

Mutation Type


const typeDefs = `
type Mutation {
    setGreetingMessage(message: String!): String 
    }
`;

        

Resolvers


let greetingMsg = "Hello, world!";

        

const resolvers = {
    Query: {
        greeting () => greetingMsg;
    }
},

            

Mutation: {
    setGreetingMessage
}

                

function setGreetingMessage(_, { message }) {
    return greetingMsg = message;
}

                    

function(obj, args, context, info)

                        

Express/Apollo Server


const express = require('express');
const { ApolloServer } = require('apollo-server-express');

// import typeDefs and reslovers or define here...

const app = express();

const server = new ApolloServer({
    typeDefs,
    resolvers,
});

server.applyMiddleware({ app, path: '/graphql' });

app.listen(3000, function () {
    console.log('App Started on port 3000);
});

        

DEMO

Thank You!