
Product
Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
graphql-schema-builder
Advanced tools
Builds GraphQL types based on Mongoose-like schema and a resolver list thus separating type schema from resolvers. Simplifies defining mutation arguments based on schema types.
Type's fields
can be used to build Mongoose schema by calling it with mongoose.Schema.Types
or mechanically converted into any other schemas.
NB: All types are assumed to have an implicit id
field.
yarn add graphql-schema-builder
or
npm install --save graphql-schema-builder
const graphql = require('graphql');
const {
buildFields,
buildTypes,
getProjection,
GraphQLJSON
} = require('graphql-schema-builder')(graphql);
function getSchema(resolvers, schema, { customerProvider }) {
// build types based on existing domain schemas and resolvers
const types = buildTypes({
Asset,
Customer,
Sensor
}, resolvers);
const schemaStore = new Map;
function buildSubType({ name, fields }) {
const _name = `${name}Input`;
const newType = new GraphQLInputObjectType({ name: _name, fields });
schemaStore.set(_name, newType);
return newType;
}
function getExistingType(name) {
return schemaStore.get(`${name}Input`);
}
return new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
customer: {
type: types.Customer,
args: {
id: {
name: 'id',
type: new GraphQLNonNull(GraphQLID)
}
},
resolve(_0, { id }, _1, info) {
const projection = getProjection(info);
return customerProvider.findById(id, projection);
}
},
customers: {
type: new GraphQLList(types.Customer),
resolve(_0, {}, _1, info) {
const projection = getProjection(info);
return customerProvider.findAll(projection);
}
}
}
}),
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
createAsset: {
type: types.Asset,
// build arguments based on domain schemas instead of
// specifying them manually
args: buildFields(Asset.fields, {
buildSubType,
getExistingType
}),
resolve(obj, args, source, info) {
// create asset
}
},
/* more mutations, etc. */
}
})
});
}
fields
can be either an object or a function accepting { Mixed, ObjectId }
. See Mongoose Guide for more details about Schema definition.
const schema = {
Asset: {
name: 'Asset',
description: 'An asset.',
fields: ({ Mixed, ObjectId }) => ({
customer: {
description: 'Customer that this asset belongs to.',
type: ObjectId,
ref: 'Customer',
required: true
},
parent: {
type: ObjectId,
ref: 'Asset',
required: false
},
name: {
type: String,
required: true
}
}),
dynamicFields: ({ ObjectId }) => ({
sensors: {
type: [ObjectId],
ref: 'Sensor'
}
})
},
Customer: {
name: 'Customer',
description: 'A customer.',
fields: {
name: {
description: 'The name of the customer.',
type: String,
required: true
},
// Will result in subtype
metadata: {
created: {
type: Date,
required: true
}
}
},
dynamicFields: ({ Mixed, ObjectId }) => ({
assets: {
type: [ObjectId],
ref: 'Asset'
}
})
},
Sensor: {
name: 'Sensor',
description: 'A sensor that must be connected to an asset.',
fields: ({ Mixed, ObjectId }) => ({
externalId: {
type: String,
required: false
},
asset: {
description: 'An asset that this sensor is connected to.',
type: ObjectId,
ref: 'Asset',
required: true
},
name: {
type: String,
required: false
}
})
}
}
A resolver can be either a function or an object. If it's an object it must have a resolve()
function and can have args
field. args
has the same format as a schema. Matching arguments will be passed into the args
argument of a resolver.
See also:
const resolvers = {
Asset: {
customer(obj, {}, _, info) {
const projection = getProjection(info);
return customerProvider.findById(obj.customer, projection);
},
parent(obj, {}, _, info) {
if (obj.parent != null)
return null;
const projection = getProjection(info);
return assetProvider.findById(obj.parent, projection);
},
sensors(obj, {}, _, info) {
const projection = getProjection(info);
return sensorProvider.find({ asset: obj.id }, projection);
},
// resolver as an object
measurements: {
args: {
resolution: {
type: String,
required: true
}
},
resolve(obj, { resolution }, _, info) {
const projection = getProjection(info);
return measurementProvider.find({ resolution }, projection);
}
}
},
Customer: {
assets(obj, {}, _, info) {
const projection = getProjection(info);
return assetProvider.find({ customer: obj.id }, projection);
}
},
Sensor: {
asset(obj, {}, _, info) {
const projection = getProjection(info);
return assetProvider.findById(obj.parent, projection);
}
}
}
buildTypes
through getExistingType
argumentSee GraphQL example in pubsub-store repository.
MIT
FAQs
GraphQL Schema Builder
The npm package graphql-schema-builder receives a total of 4 weekly downloads. As such, graphql-schema-builder popularity was classified as not popular.
We found that graphql-schema-builder demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket Firewall is a free tool that blocks malicious packages at install time, giving developers proactive protection against rising supply chain attacks.
Research
Socket uncovers malicious Rust crates impersonating fast_log to steal Solana and Ethereum wallet keys from source code.
Research
A malicious package uses a QR code as steganography in an innovative technique.