graphql-type-json
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

graphql-type-json Travis npm

JSON scalar types for GraphQL.js.

Codecov

Usage

This package exports a JSON value scalar GraphQL.js type:

import GraphQLJSON from 'graphql-type-json';

It also exports a JSON object scalar type:

import { GraphQLJSONObject } from 'graphql-type-json';

These types can also be imported as follows using CommonJS:

const { GraphQLJSON, GraphQLJSONObject } = require('graphql-type-json');

GraphQLJSON can represent any JSON-serializable value, including scalars, arrays, and objects. GraphQLJSONObject represents specifically JSON objects, which covers many practical use cases for JSON scalars.

Programmatically-constructed schemas

You can use this in a programmatically-constructed schema as with any other scalar type:

import GraphQLJSON, { GraphQLJSONObject } from 'graphql-type-json';
 
export default new GraphQLObjectType({
  name: 'MyType',
 
  fields: {
    myValue: { type: GraphQLJSON },
    myObject: { type: GraphQLJSONObject },
  },
});

SDL with GraphQL-tools

When using the SDL with GraphQL-tools, define GraphQLJSON as the resolver for the appropriate scalar type in your schema:

import { makeExecutableSchema } from 'graphql-tools';
import GraphQLJSON, { GraphQLJSONObject } from 'graphql-type-json';
 
const typeDefs = `
scalar JSON
scalar JSONObject
 
type MyType {
  myValue: JSON
  myObject: JSONObject
}
 
# ...
`;
 
const resolvers = {
  JSON: GraphQLJSON,
  JSONObject: GraphQLJSONObject,
};
 
export default makeExecutableSchema({ typeDefs, resolvers });

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.3.2
    1,074,480
    • latest

Version History

Package Sidebar

Install

npm i graphql-type-json

Weekly Downloads

1,155,019

Version

0.3.2

License

MIT

Unpacked Size

11.3 kB

Total Files

9

Last publish

Collaborators

  • taion