Skip to content

Latest commit

History

History

hello-world

Folders and files

NameName
Last commit message
Last commit date

parent directory

..

hello-world

This directory contains a simple "Hello World" example based on graphql-yoga.

Get started

Clone the repository:

git clone https://github.com/graphcool/graphql-yoga.git
cd graphql-yoga/examples/hello-world

Install dependencies and run the app:

yarn install # or npm install
yarn start   # or npm start

Testing

Open your browser at http://localhost:4000 and start sending queries.

Query without name argument:

query {
  hello
}

The server returns the following response:

{
  "data": {
    "hello": "Hello World"
  }
}

Query with name argument:

query {
  hello(name: "Sarah")
}

The server returns the following response:

{
  "data": {
    "hello": "Hello Sarah"
  }
}

Implementation

This is what the implementation looks like:

import { createServer } from '@graphql-yoga/node'
// ... or using `require()`
// const { createServer } = require('@graphql-yoga/node')

const typeDefs = `
  type Query {
    hello(name: String): String!
  }
`

const resolvers = {
  Query: {
    hello: (_, { name }) => `Hello ${name || 'World'}`,
  },
}

const server = createServer({ typeDefs, resolvers })
server.start(() => console.log('Server is running on localhost:4000'))