ViggoV
ViggoV

Reputation: 2173

Filter or distinguish built-in and custom graphql types

I'm trying to find a way to distinguish (and filter out) the built-in types from my custom types when running an introspectionQuery against a graphql api. There doesn't seem to be anything reliable in the output to identify which types are built-in (apart from the __ in front of the "system" types).

At this point I can't even seem to find a single official list, so my best option seems to be to go over the introspectionQuery output and make a list for future use, hoping nothing changes.

Are the really no systematic way to distinguish the two?

Upvotes: 0

Views: 360

Answers (2)

Moshe Simantov
Moshe Simantov

Reputation: 4493

You can use the isIntrospectionType function:

import { buildSchema, isIntrospectionType } from 'graphql';


const schema = buildSchema(source);

for (const type of Object.values(schema.getTypeMap())) {
  if (isIntrospectionType(type)) continue;

  // do something...
}

PS. You can use isSpecifiedScalarType as well to filter Scalar nodes such as ID, Int, etc..

Upvotes: 1

Daniel Rearden
Daniel Rearden

Reputation: 84657

While the spec does specify scalar types that every implementation should include and specifies how these scalars should be serialized and parsed, it doesn't really draw a distinction between these built-in scalars and any others a GraphQL service might expose. Functionally, there is no difference between a "built-in" type and a "custom" one.

There are five built-in scalars outlined by the spec. These are very unlikely to change in the future.

  • Int
  • Float
  • String
  • Boolean
  • ID

There are also several "meta" types that are used explicitly for introspection. These are outlined here.

  • __Schema
  • __Type
  • __Field
  • __InputValue
  • __EnumValue
  • __TypeKind
  • __Directive
  • __DirectiveLocation

Names beginning with two underscores (__) are reserved names, so any types with names beginning in this way can safely be classified as "built-in".

It's unclear from your question to what end you are trying to draw the distinction between "built-in" and "custom" types, but I would guess for most purposes we would consider the above to be the "built-in" types and any others "custom".

Upvotes: 0

Related Questions