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.
git clone -b graphql-js [email protected]:nikolasburk/plain-graphql.git
cd plain-graphql
yarn install
yarn start
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)
}
}
}
})
})
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
})