Created
September 13, 2018 16:18
-
-
Save mcollina/bc151c6a339e12539e63edb14cd043e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const { | |
graphql, | |
parse, | |
buildSchema, | |
extendSchema, | |
buildASTSchema | |
} = require('graphql') | |
// const schema = buildSchema(` | |
// type Person { | |
// name: String | |
// } | |
// | |
// query names { | |
// person { | |
// name | |
// } | |
// } | |
// | |
// `) | |
const q1 = parse(` | |
type Person { | |
name: String | |
age: Int | |
friends: [Person] | |
} | |
type Query { | |
add(x: Int, y: Int): Int | |
people: [Person] | |
} | |
`) | |
const q2 = parse(` | |
extend type Query { | |
add2(x: Int, y: Int): Int | |
} | |
`) | |
const schema = extendSchema(buildASTSchema(q1), q2); | |
const root = { | |
add: async ({ x, y }) => x + y, | |
add2: async ({ x, y }) => 2 * x + y, | |
people: async () => { | |
return [{ name: 'Matteo', age: 34, friends }] | |
} | |
}; | |
async function friends () { | |
console.log(this, arguments) | |
return [{ name:'XYZ', age: 42 }] | |
} | |
// var schema = new GraphQLSchema({ | |
// query: new GraphQLObjectType({ | |
// name: 'RootQueryType', | |
// fields: { | |
// hello: { | |
// type: GraphQLString, | |
// resolve() { | |
// return 'world'; | |
// } | |
// } | |
// } | |
// }) | |
// }); | |
{ | |
var query = '{ add2(x: 2, y: 2) }'; | |
graphql(schema, query, root).then(result => { | |
// Prints | |
// { | |
// data: { hello: "world" } | |
// } | |
console.log(result); | |
}); | |
} | |
{ | |
var query = '{ people { name, friends { name } } }'; | |
graphql(schema, query, root).then(result => { | |
// Prints | |
// { | |
// data: { hello: "world" } | |
// } | |
console.log(JSON.stringify(result, null, 2)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment