https://github.com/taion/graphql-type-json
JSON scalar type for GraphQL.js
https://github.com/taion/graphql-type-json
Last synced: 10 months ago
JSON representation
JSON scalar type for GraphQL.js
- Host: GitHub
- URL: https://github.com/taion/graphql-type-json
- Owner: taion
- License: mit
- Created: 2016-04-28T19:48:07.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2025-05-05T12:01:40.000Z (10 months ago)
- Last Synced: 2025-05-05T13:22:35.227Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 1.4 MB
- Stars: 985
- Watchers: 12
- Forks: 55
- Open Issues: 37
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - graphql-type-json
README
# graphql-type-json [![Travis][build-badge]][build] [![npm][npm-badge]][npm]
JSON scalar types for [GraphQL.js](https://github.com/graphql/graphql-js).
[![Codecov][codecov-badge]][codecov]
## Usage
This package exports a JSON value scalar GraphQL.js type:
```js
import GraphQLJSON from 'graphql-type-json';
```
It also exports a JSON object scalar type:
```js
import { GraphQLJSONObject } from 'graphql-type-json';
```
These types can also be imported as follows using CommonJS:
```js
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:
```js
import GraphQLJSON, { GraphQLJSONObject } from 'graphql-type-json';
export default new GraphQLObjectType({
name: 'MyType',
fields: {
myValue: { type: GraphQLJSON },
myObject: { type: GraphQLJSONObject },
},
});
```
### SDL with [GraphQL-tools](https://github.com/apollographql/graphql-tools)
When using the SDL with GraphQL-tools, define `GraphQLJSON` as the resolver for the appropriate scalar type in your schema:
```js
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 });
```
[build-badge]: https://img.shields.io/travis/taion/graphql-type-json/master.svg
[build]: https://travis-ci.org/taion/graphql-type-json
[npm-badge]: https://img.shields.io/npm/v/graphql-type-json.svg
[npm]: https://www.npmjs.com/package/graphql-type-json
[codecov-badge]: https://img.shields.io/codecov/c/github/taion/graphql-type-json/master.svg
[codecov]: https://codecov.io/gh/taion/graphql-type-json