How to define an arbitrary scalar in apollo gql?

Viewed 216

I have a type that needs to be like the following

type ActivityPayload {
  action: String!
  extra: AnythingAtAll
}

Where AnythingAtAll is an arbitrary JSON format. So that's as far as I get because all the tutorials I see expect you to have a type AnythingAtAll with fields defined inside of it. How do I allow {}, or {any properties in json format no matter what the property names and values are}

1 Answers

Use graphql-scalars:

A library of custom GraphQL scalar types for creating precise type-safe GraphQL schemas.

This library offers also the scalar type JSON:

The JSON scalar type represents JSON values as specified by ECMA-404.

Then you can do the following:

type ActivityPayload {
  action: String!
  extra: JSON
}

See also graphql-type-json (JSON is based on this one).

Related