graphql-schema-builder
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.
Usage
yarn add graphql-schema-builder
or
npm install --save graphql-schema-builder
const graphql = ; const buildFields buildTypes getProjection GraphQLJSON} = graphql; { // build types based on existing domain schemas and resolvers const types = ; const schemaStore = ; { const _name = `Input`; const newType = name: _name fields ; schemaStore; return newType; } { return schemaStore; } return query: name: 'RootQueryType' fields: customer: type: typesCustomer args: id: name: 'id' type: GraphQLID { const projection = ; return customerProvider; } customers: type: typesCustomer { const projection = ; return customerProvider; } mutation: name: 'Mutation' fields: createAsset: type: typesAsset // build arguments based on domain schemas instead of // specifying them manually args: { // create asset } /* more mutations, etc. */ ;}
Schema example
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.' 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 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 assets: type: ObjectId ref: 'Asset' Sensor: name: 'Sensor' description: 'A sensor that must be connected to an asset.' 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
Resolvers example
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: { const projection = ; return customerProvider; } { if objparent != null return null; const projection = ; return assetProvider; } { const projection = ; return sensorProvider; } // resolver as an object measurements: args: resolution: type: String required: true { const projection = ; return measurementProvider; } Customer: { const projection = ; return assetProvider; } Sensor: { const projection = ; return assetProvider; }
Supported schema types
- Array of any of supported types
- Boolean
- Date
- Mixed
- Number
- ObjectId
- String
- Custom GraphQL types passed to
buildTypes
throughgetExistingType
argument
Examples
See GraphQL example in pubsub-store repository.
License
MIT