# Quick start

GraphQL Yoga is a batteries-included cross-platform [GraphQL over HTTP spec-compliant](https://github.com/enisdenjo/graphql-http/tree/master/implementations/graphql-yoga) GraphQL server powered by [Envelop](https://envelop.dev/) and [GraphQL Tools](https://graphql-tools.com/) that runs anywhere; focused on easy setup, performance and great developer experience.

## Installation [Permalink for this section](/content/graphql/yoga-server/docs#installation/index.html)

```bash
npm i graphql-yoga graphql
```

```bash
pnpm add graphql-yoga graphql
```

```bash
yarn add graphql-yoga graphql
```

```bash
bun add graphql-yoga graphql
```

## Schema [Permalink for this section](/content/graphql/yoga-server/docs#schema/index.html)

You will need to provide a schema to Yoga, there are many ways to assemble a GraphQL schema:
- graphql-yoga
- @graphql-tools/schema
- PothosGraphQL
- Nexus
- gqtx
- graphql-js

Use the `createSchema` function included in Yoga. It actually reuses the [`makeExecutableSchema` from @graphql-tools/schema](https://www.graphql-tools.com/docs/generate-schema).

```javascript
import { createSchema } from 'graphql-yoga'

export const schema = createSchema({
  typeDefs: /* GraphQL */ `
    type Query {
      hello: String
    }
  `,
  resolvers: {
    Query: {
      hello: () => 'world'
    }
  }
})
```

### Tip

**Get the best developer experience!**
GraphQL Code Generator and its server preset can help you build type-safe and scalable GraphQL servers. Check out [GraphQL Code Generator](/content/graphql/codegen/docs/getting-started/index.html), then read [how to set up GraphQL server with server preset](/content/graphql/codegen/docs/guides/graphql-server-apollo-yoga-with-server-preset/index.html).

```bash
npm i @graphql-tools/schema
```

```bash
pnpm add @graphql-tools/schema
```

```bash
yarn add @graphql-tools/schema
```

```bash
bun add @graphql-tools/schema
```

```javascript
import { makeExecutableSchema } from '@graphql-tools/schema'

export const schema = makeExecutableSchema({
  typeDefs: /* GraphQL */ `
    type Query {
      hello: String
    }
  `,
  resolvers: {
    Query: {
      hello: () => 'world'
    }
  }
})
```

### Tip

```bash
npm i @pothos/core
```

```bash
pnpm add @pothos/core
```

```bash
yarn add @pothos/core
```

```bash
bun add @pothos/core
```

```javascript
import SchemaBuilder from '@pothos/core'

const builder = new SchemaBuilder({})

builder.queryType({
  fields: t => ({
    hello: t.string({
      resolve: () => 'world'
    })
  })
})

export const schema = builder.toSchema()
```

You can also create schemas with [Nexus GraphQL](https://nexusjs.org/):

```bash
npm i nexus
```

```bash
pnpm add nexus
```

```bash
yarn add nexus
```

```bash
bun add nexus
```

```javascript
import { makeSchema, queryField } from 'nexus'

const helloField = queryField('hello', {
  type: 'String',
  resolve: () => 'world'
})

export const schema = makeSchema({ types: [helloField] })
```

Using [gqtx](https://github.com/sikanhe/gqtx):

```bash
npm i gqtx
```

```bash
pnpm add gqtx
```

```bash
yarn add gqtx
```

```bash
bun add gqtx
```

```javascript
import { buildGraphQLSchema, Gql } from 'gqtx'

const Query = Gql.Query({
  fields: () => [
    Gql.Field({
      name: 'hello',
      type: Gql.String,
      resolve: () => 'world'
    })
  ]
})

export const schema = buildGraphQLSchema({ query: Query })
```

[graphql-js](https://github.com/graphql/graphql-js) is a reference implementation of GraphQL for JavaScript:

```javascript
import { GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql'

export const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      hello: {
        type: GraphQLString,
        resolve: () => 'world'
      }
    }
  })
})
```

## Server [Permalink for this section](/content/graphql/yoga-server/docs#server/index.html)

After you have created a GraphQL schema, simply pass it into Yoga and liftoff! 🚀

```javascript
import { createServer } from 'node:http'
import { createYoga } from 'graphql-yoga'
import { schema } from './schema.js'

// Create a Yoga instance with a GraphQL schema.
const yoga = createYoga({ schema })

// Pass it into a server to hook into request handlers.
const server = createServer(yoga)

// Start the server and you're done!
server.listen(4000, () => {
  console.info('Server is running on http://localhost:4000/graphql')
})
```

Visit [`http://localhost:4000/graphql`](http://localhost:4000/graphql) to see Yoga in action.
