Skip to content

Latest commit

 

History

History
48 lines (36 loc) · 1.2 KB

key-value-object.md

File metadata and controls

48 lines (36 loc) · 1.2 KB

Is there a way to represent an object of key-value pairs in GraphQL

The GraphQL specification includes default scalar types Int, Float, String, Boolean, and ID. If you need to support other data types (e.g. Date), you can define custom scalar types.

Define a scalar type:

# types.graphql

scalar JSONObject

Implement a type (or use one from the package, like graphql-type-json):

# graphql-type-json.ts

export const GraphQLJSONObject = new GraphQLScalarType({
  name: 'JSONObject',
  description: 'The `JSONObject` scalar type...',
  serialize(value) {
    ...
  },
  parseValue(value) {
    ...
  },
  parseLiteral(ast) {
    ...
  },
});

Make the type a part of the resolvers:

# resolvers.ts

import { GraphQLJSONObject } from './graphql-type-json.ts';

export const resolvers = {
  JSONObject: GraphQLJSONObject
}

References: