Skip to content

nikolasburk/plain-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 

Repository files navigation

GraphQL.js

A simple node script to demonstrate the difference in how to build a GraphQLSchema with graphql-js and graphql-tools. Check out the corresponding branch for more info:

Read the full article here.

Usage

Grab code from a branch (e.g. graphql-js)

git clone -b graphql-js git@github.com:nikolasburk/plain-graphql.git

Install dependencies and run the script

cd plain-graphql
yarn install
yarn start

graphql-js vs graphql-tools

Creating the GraphQLSchema with graphql-js

const UserType = new GraphQLObjectType({
  name: 'User',
  fields: {
    id: { type: GraphQLID },
    name: { type: GraphQLString }
  }
})

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: {
      user: {
        type: UserType,
        args: {
          id: { type: GraphQLID }
        },
        resolve: (root, args, context, info) => {
          console.log(`Resolver called: user`)
          return fetchUserById(args.id)
        }
      }
    }
  })
})

Creating the GraphQLSchema with graphql-tools

const { makeExecutableSchema } = require('graphql-tools')

// Define schema in SDL
const typeDefs = `
type Query {
  user(id: ID!): User
}

type User {
  id: ID!
  name: String
}`

const resolvers = {
  Query: {
    user: (root, args, context, info) => {
      console.log(`Resolver called: user`)
      return fetchUserById(args.id)
    }
  }
}

const schema = makeExecutableSchema({
  typeDefs,
  resolvers
})

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published