1.0.1 • Published 8 years ago
graphql-js-schema-builder v1.0.1
graphql-js-schema-builder
This is javascript tool help to build graphql schema with graphql-js.
Installation
npm install graphql-js-schema-builder --save
Usage
Define Types
const GraphQLString = require('graphql').GraphQLString;
const GraphQLInt = require('graphql').GraphQLInt;
module.exports = GraphQLBuilder.defineObjectType({
name: "Company",
fields: {
id: { type: GraphQLString },
name: { type: GraphQLString },
date: { type: GraphQLString },
label: {
type: GraphQLString,
resolve: function() {
return 'COMPANY'
}
}
}
});
Define Query
const GraphQLString = require('graphql').GraphQLString;
const Company = require('types/Company');
GraphQLBuilder.defineGraphQLQueries([
{
name: 'company', // name of the query
type: Company,
args: {
id: { type: GraphQLString }
},
resolve: function(context, args) {
return {
// json of company data
// {
// id:
// name:
// date:
// label:
// }
}
}
}
])
Create schema
const queries = require('queries/query');
GraphQLBuilder.createSchema('root', queries);
Example
cd example
npm install
npm start
query the data on http://localhost:3000/graphql or debug the graphql query on http://locahost:3000/graphiql
visit http://locahost:3000/graphiql and try the query below:
{
companies(offset: 0, max: 5) {
id
name
date
label
}
people(offset: 0, max: 5) {
id
firstName
surname
label
}
companyWithPeople(companyId: "4DB7D01E-1E1E-9EAA-F314-E8FAB53D6E3B", offset: 0, max: 5){
id
name
date
people {
id
firstName
surname
label
}
}
}