Registry / development / graphql-compose-json

graphql-compose-json

JSON →
library6.2.0jsnpmunverified

Plugin for `graphql-compose` that generates GraphQL types from JSON objects. Current stable version is 6.2.0. Release cadence is irregular; latest update in 2023. Key differentiators: automatic type inference from JSON fields, support for custom field resolvers, and dual output/input type generation. Requires peer dependency graphql-compose ^7–9. Ships TypeScript definitions. Actively maintained by the graphql-compose organization.

npm install graphql-compose-json
INSTALL
IMPORT
SIG · GRAPHQL-COMPOSE-JS
G
graphql-compose-json
developmentjavascriptv6.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

composeWithJson
import { composeWithJson } from 'graphql-compose-json'
const { composeWithJson } = require('graphql-compose-json')
CommonJS require works but ESM is preferred. TypeScript included.
composeInputWithJson
import { composeInputWithJson } from 'graphql-compose-json'
import composeInputWithJson from 'graphql-compose-json'
Named export, not default export.
default import
import gql from 'graphql-tag'
import gql from 'graphql-compose-json'
graphql-compose-json does not have a default export; must use named exports.
TypeScript type
import type { ComposeWithJson } from 'graphql-compose-json'
import { ComposeWithJson } from 'graphql-compose-json'
Type-only imports require `type` modifier to avoid runtime errors.

Demonstrates generating GraphQL output and input types from a JSON object using composeWithJson and composeInputWithJson, then adding to a query field.

import { composeWithJson, composeInputWithJson } from 'graphql-compose-json'; import { schemaComposer } from 'graphql-compose'; const restApiResponse = { name: 'Anakin Skywalker', birth_year: '41.9BBY', gender: 'male', mass: 77, homeworld: 'https://swapi.co/api/planets/1/', films: ['https://swapi.co/api/films/5/', 'https://swapi.co/api/films/4/', 'https://swapi.co/api/films/6/'], species: ['https://swapi.co/api/species/1/'], starships: ['https://swapi.co/api/starships/59/', 'https://swapi.co/api/starships/65/', 'https://swapi.co/api/starships/39/'], }; const PersonTC = composeWithJson('Person', restApiResponse); const PersonGraphQLType = PersonTC.getType(); const PersonITC = composeInputWithJson('PersonInput', restApiResponse); const PersonGraphQLInput = PersonITC.getType(); schemaComposer.Query.addFields({ person: PersonTC }); const schema = schemaComposer.buildSchema(); console.log(require('graphql').printSchema(schema));
Debug
Known issues
breakingBreaking change in v6: The function signature for custom field resolvers changed. Earlier versions allowed direct function for type; now it must be a function returning a string with type or an object with type and resolve.
fix
Update field definitions from `field: () => 'Int'` to `field: () => 'Int!'` or `field: () => ({ type: 'Int', resolve: source => ... })`
affects: 5.x
deprecatedSupport for graphql-compose v6 and below is dropped; need at least v7.
fix
Upgrade graphql-compose to ^7.0.4 || ^8.0.0 || ^9.0.0
affects: 6.x
gotchaNested objects are NOT automatically resolved into GraphQL types; they remain as JSON scalar unless manually expanded using additional composeWithJson calls.
fix
For nested objects, create separate types via composeWithJson and use field resolvers to reference them.
affects: >=6.0.0
gotchaArray fields with mixed types (e.g., array of strings and numbers) will cause errors at runtime.
fix
Ensure arrays contain homogeneous element types, or provide custom field config with a resolve function to transform data.
affects: >=6.0.0
Errors
Common errors & fixes
Cannot find module 'graphql-compose-json'
Package not installed or missing from node_modules.
fix
npm install graphql-compose-json graphql-compose graphql
TypeError: composeWithJson is not a function
Default import used instead of named import.
fix
Use `import { composeWithJson } from 'graphql-compose-json'`
Field 'mass' must return type 'Int' but value is 77 (Float)
JSON numbers are inferred as Float by default; custom field config needed for Int.
fix
Use `mass: () => 'Int!'` in the response object to override type.
Upgrade
Version history
6.2.0latest on npm
Audit
Dependencies
graphql-composerequiredPeer dependency required at version ^7.0.4 || ^8.0.0 || ^9.0.0
graphqlrequiredPeer dependency for GraphQL runtime; not listed in package.json but required in README
Agent activity
54 hits · last 30 days
node
48
OpenAI (training)
1
Resources
graphql-compose-json — npm install graphql-compose-json · libregistry