GraphQL

Example

These example are taken from the article above (they are also little bit updated).

const express = require('express');
const express_graphql = require('express-graphql');
const {buildSchema} = require('graphql');
// GraphQL schema
const schema = buildSchema(`
    type Query {
        message: String
    }
`);
// Root resolver
const root = {
  message: () => 'Hello World!'
};
// Create an express server and a GraphQL endpoint
const app = express();
app.use('/graphql', express_graphql({
  schema: schema,
  rootValue: root,
  graphiql: true
}));
app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));

Now lets call the API using this UI http://localhost:4000/graphql.

Let's do another more complex example.

Now we can play with this in the graphql ui.

Multiple parameters.

Mutation of data on server.

Last updated

Was this helpful?