https://github.com/sgpinkus/graphql-type-typed-map
Simple GraphQL scalar type extension that implements a typed map.
https://github.com/sgpinkus/graphql-type-typed-map
Last synced: about 2 months ago
JSON representation
Simple GraphQL scalar type extension that implements a typed map.
- Host: GitHub
- URL: https://github.com/sgpinkus/graphql-type-typed-map
- Owner: sgpinkus
- Created: 2021-05-17T23:59:40.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-05-22T05:29:14.000Z (about 4 years ago)
- Last Synced: 2025-02-13T14:18:02.857Z (4 months ago)
- Language: TypeScript
- Size: 49.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GraphQLTypedMapType
How do you specify this Typescript in GraphQL?:interface MyType {
[propName: string]: MyOtherType
}AFAIK you can't. You have to define all the fields of an object explicitly if you want to specify
the types of those fields.[graphql-type-json](https://www.npmjs.com/package/graphql-type-json) provides `GraphQLJSONObject`
which allows for an arbitrary object. This simple package provides `GraphQLTypedMapType()` which
produces a `GraphQLScalarType` type that allows you to declare a more constrained type of object like
the Typescript above. It also allows your to optionally constrain which keys can be present at the top
level.Note, the downside of defining custom scalar types is since the type is not part of GraphQL SDL, the
client side can't know in advance whether a type valid for custom scalar types.*Protocol:*
GraphQLTypedMapType = (name: string, allowedKeys?: string[], valueType?: any): GraphQLScalarType
*Usage:*
// name parameter is required and must be unique.
const type1: GraphQLScalarType = GraphQLTypedMapType("MyMap1", undefined, GraphQLString); // no key constraints.
const type2: GraphQLScalarType = GraphQLTypedMapType("MyMap2", ['a','b','c'], GraphQLInt); // some key constraints.