Enterprise Framework

Software Solutions in the Enterprise

GraphQL - Dice Example without the GraphQL schema language:

GraphQL - Dice Example without the GraphQL schema language:

Using GraphQL schema language, you would use root and set it to the schema.



var express = require('express')
var graphqlHTTP = require('express-graphql')
var graphql = require('graphql')
const { GraphQLInt, GraphQLList, GraphQLObjectType, GraphQLString } = require('graphql')

class RandomDie {
  constructor (numSides) {
    this.numSides = numSides;
  }

  rollOnce () {
    return 1 + Math.floor(Math.random() * this.numSides);
  }

  roll ({numRolls}) {
    var output = [];
    for (var i = 0; i < numRolls; i++) {
      output.push(this.rollOnce())
    }
    return output
  }
}

// Define the Query type
var RandomDieType = new GraphQLObjectType({
  name: 'RandomDie',
  fields: {
    numSides: { type: GraphQLInt },
    rollOnce: { type: GraphQLInt },
    otherValue: { type: GraphQLString },
    roll: {
      type: new GraphQLList(graphql.GraphQLInt),
      args: {
        numRolls: { type: graphql.GraphQLInt }
      },
      resolve: function (_, {numRolls}, context) {
        // "_" represents the parent node in this case "getDie Type".  
        // It's the result of this.fields bound to the parent nodes
        // resolve data.
        // _ = {
        //   numSides: numSides,
        //   rollOnce: randomDie.rollOnce(),
        //   randomDie: randomDie
        // }
        console.log(_)
        console.log(`hello ${_.otherValue}`)
        var randomDie = new RandomDie(6)
        return randomDie.roll({
          numRolls: numRolls
        })
      }
    }
  }
})

var getDie = new graphql.GraphQLObjectType({
  name: 'Query',
  fields: {
    getDie: {
      type: RandomDieType,
      args: {
        numSides: { type: graphql.GraphQLInt }
      },
      resolve: function (_, {numSides}, context) {
        var randomDie = new RandomDie(numSides || 6)
        // THIS INFORMATION WILL BE PASSED TO "type: RandomDieType" RESOLVE
        // WITH THE FIELDS SET TO _
        return {
          numSides: numSides,
          rollOnce: randomDie.rollOnce(),
          randomDie: randomDie
        }
      }
    }
  }
})

var schema = new graphql.GraphQLSchema(
  {
    query: getDie
  }
)

var app = express()
app.use('/graphql', graphqlHTTP({
  schema: schema,
  rootValue: root,
  graphiql: true
}))
app.listen(4000)
console.log('Running a GraphQL API server at localhost:4000/graphql')

Request

{ getDie(numSides: 6) { numSides rollOnce roll(numRolls: 3) } }

Outputs

{ "data": { "getDie": { "numSides": 6, "rollOnce": 3, "roll": [ 1, 6, 3 ] } } }


Node JS - Standard JS -> Errors ('describe' is not defined, 'it' is not defined, 'jest' is not defined, 'expect' is not defined)

Running standard --fix on node shows errors ('describe' is not defined, 'it' is not defined, 'jest' is not defined, 'expect' is not defined)

$ standard --fix

standard: Use JavaScript Standard Style (https://standardjs.com)

  /test/getUser.test.js:7:1: 'jest' is not defined.

  /test/getUser.test.js:12:1: 'describe' is not defined.

  /test/getUser.test.js:13:3: 'it' is not defined.

  /test/getUser.test.js:15:5: 'expect' is not defined.

Add line to package.json
  "standard": {
    "env": [
      "jest"
    ]
  }


or add comment to the unit test file 

/* eslint-env mocha */